Program on Access specifier
(public,private,protected)
| WITHIN CLASS | OUTSIDE CLASS | DERIVED CLASS |
PUBLIC | YES | YES | YES |
PRIVATE | YES | NO | NO |
PROTECTED | YES | NO | YES |
Program on Access specifier (public)
#include<iostream.h>
#include<conio.h>
class accspe
{
public:
int x;
private:
int y;
protected:
int z;
};
void main()
{
clrscr();
accspe as;
cout<<"enter x public value:"<<endl;
cin>>as.x;
cout<<"x public value:"<<as.x<<endl;
getch();
}
Program on Access specifier (private)
#include<iostream.h>
#include<conio.h>
class accspe
{
public:
int x;
private:
int y;
protected:
int z;
public:
void display()
{
y=20;
cout<< "y value can be seen if
it is inside the class private:"<<y<<endl;
}
};
void main()
{
clrscr();
accspe as;
cout<<"enter x public value:"<<endl;
cin>>as.x;
cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
cin>>as.y;
cout<<"y private value:"<<as.y<<endl;
*/ we will get error if we remove comment
as.display();
getch();
}
Program on Access specifier (protected)
#include<iostream.h>
#include<conio.h>
class accspe
{
public:
int x;
private:
int y;
protected:
int z;
public:
void display()
{
y=20;
cout<< "y value can be seen if it is inside the class private:"<<y<<endl;
}
};
class derived : public accspe
{
public:
void show()
{
z=30;
cout<< "z value Potected is "<<z<<endl;
}
};
void main()
{
clrscr();
accspe as;
cout<<"enter x public value:"<<endl;
cin>>as.x;
cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
cin>>as.y;
cout<<"y private value:"<<as.y<<endl;
*/
as.display();
derived d;
d.show();
getch();
}
No comments:
Post a Comment