Friday, August 7, 2015

Private & Public Data Members of a class

class Dummy
{
private:int a,b;         // PRIVATE DATA MEMBERS
public: int c,d;          // PUBLIC DATA MEMBERS
public:
void get_privatedata()          // MEMBER FUNCTIONS
{
cout<<"Enter the values";
cin>>a;
cin>>b;
}
void get_publicdata()          // MEMBER FUNCTIONS
{
cout<<"Enter the values";
cin>>c;
cin>>d;
}
void display()
{
cout<<"The value of a is"<<a;
cout<<"The value of b is"<<b;
cout<<"The value of c is"<<c;
cout<<"The value of d is"<<d;
}
};//closure of the class
void main()
{
Dummy D;// class instantiation
D.a=100;       //ERROR
D.b=200;       //ERROR
D.c=300;
D.d=400;
D.get_privatedata();// invoking the member function of the class via an Object of the class
D.get_publicdata();// invoking the member function of the class via an Object of the class
D.display();
}

No comments:

Post a Comment