Tuesday, March 6, 2012
What is the difference between Struct and class in C++?
The basic difference between struct and class in C++ is that by default all data member functions and member variabels are private in class where as in struct it is public. Except this every thing is same for struct and class. All C++ concepts like polymorphism, inheritance etc works fine with struct also.
Basic example:
Example for Inheritance:
Basic example:
struct structClass { int x; // defualt public public: structClass(); }; structClass::structClass() // constructor { x=0; } class classC { int x; //defualt private public: classC(); }; classC::classC() // constructor { x=0; } int main() { structClass sC; sC.x =10; // Legal as it is a struct and not a class classC cC; // cC.x = 10; // Illegal as it is a class and default specifier is private }
Example for Inheritance:
struct base { public: virtual void display() { cout<<"this is base class\n"; } }; class derived: public base { public: void display() { cout<<"this is derived class\n"; } }; int main() { base b1; derived d; base &b = d; b.display(); }In the above sample code, base class is of type structure and derived class is of type c++ class. This also works fine in C++.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment