C++ struct vs class

This is an example to show the similarity between a struct and a class

  #include <iostream>using namespace std;

int main()

{

struct employee

{char *name;

int age;

double salary;

};

employee e257 = {”raja“,25,23519.78};

cout<< e257.name<<”\t”<<e257.age

<<”\t”<<e257.salary <<endl;

class Employee { public:      char *name;
     int age;double salary; };

Employee E257 = {”raja”,25,23519.78};

cout<< E257.name<<”\t”<<E257.age

<<”\t”<<E257.salary <<endl;

} 
Note:
1) All members of a struct are public by default 
2) All members of a class are private by default 
3) For a class member to be public, it has be 
declared public with the keyword "public".  
Advantage of using classes 
1) Member variables are private and protected. 
2) Constructors functions(declared public) can be 
used  to initialize and modify values of that private variables. 
See this page for Constructors:  Constructors & Destructors </pre
 Download here.struct.doc
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s