Tuesday, 24 February 2015

Protected Keyword in C++

#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
};
class Derived:private Base
{
public:
void GetPut()
{
cout<<"\n Enter A Value :";
cin>>a;
cout<<"\n Enter B Value :";
cin>>b;
cout<<"\n A =  "<<a;
cout<<"\n B =  "<<b;
}
};
class Derived1:public Derived /* Derived.a & Derived.b is not accessible */
{
};
void main()
{
Derived d;
d.GetPut();
}
==================
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
};
class Derived:public Base
{
public:
void GetPut()
{
cout<<"\n Enter A Value :";
cin>>a;
cout<<"\n Enter B Value :";
cin>>b;
cout<<"\n A =  "<<a;
cout<<"\n B =  "<<b;
}
};
void main()
{
Derived d;
d.GetPut();
}
=====================
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
public:
Base()
{
a=10;
b=20;
}
void Put()
{
cout<<"\n A value is :"<<a;
cout<<"\n B value is :"<<b;
}
};
class Derived:public Base
{
private:
int c;
public:
Derived()
{
c=100;
}
void Put()
{
cout<<"\n C =  "<<c;
}
};
void main()
{
Derived d;
clrscr();
d.Put();
d.Base::Put();
getch();
}

No comments:

Post a Comment