Inheritance
Inheritance
Inheritance
- The ability of one class, called child class, to acquire properties from another class, called as parent class is called as inheritance.
- The parent class is also called as base class and child class is also called as derived class.
- Inheritance is used to achieve reusability of code, which is an important concept of object oriented programming.
Example of single inheritance
class a //base class or parent class
{
public:
void fun()
{
cout<<“fun”;
}
};
class b:public a //child class or derived class
{;}
void main()
{
b ob;
ob.fun(); /*fun of class a called with derived class b’s object, as fun is inherited in b class*/
}
Output: fun
Protected class member
- When a class member is declared to be protected, then the protected class member can be accessed within the class and in the immediately derived class also.
- The visibility of class member as protected is more than private and less than public.
Example of protected class member
class a
{
protected:
int x; /*protected class member x can be accessed in derived class b*/
};
class b : public a
{
public:
void fun()
{
x=10; /*protected class member x accessed in derived
class b*/
cout<<x;
}
};
void main()
{
b ob;
ob.fun();
}
Output: 10
The use of many visibility operators we can easily define it.
Comments
Post a Comment