Inheritance:
The mechanism of deriving a new class from an old class is called inheritance. The
old class is named as base class and the new class is named as derived class.
Person(Base/Parent)->Student(Derived/Child)
{ {
Name, { Name
Address, Address
DOB, DOB
Email Email
} } Coming from Person class
{
Rollno,
CourseName
}
Advantage-
1) Reusability
2) Modularity (Generalization/Specialization)
Types of Inheritance:
1) Single Inheritance
2) Multiple Inheritance
3) Hierarchical Inheritance
4) Multilevel Inheritance
5) Hybrid Inheritance
Specification of defining a child class:
class <derived-className> : <visibility-mode/access specifier> <base-className>
{
members of the derived class
}
Example:
class student : public person
{
char rollno[10];
char coursename[30];
}
Single Inheritance Example:
class B //parent class/base class
{
int a;
public:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : public B //child class/derived class
{
int c;
public:
void mul(void);
void display(void);
/* int b;
void set_ab();
int get_a(void);
void show_a(void);
*/ available because of inheritance
};
void B:: set_ab(void){ a=5;b=10;}
int B::get_a(){ return a;}
void B::show_a(){ cout<<"a="<<a<<"\n";}
void D::mul(){ c=b*get_a();}
void D::display(){ cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";}
int main()
{
D d;
d.set_ab();// possible because of inheritance
d.mul();
d.display();
d.b=20; //possible because of inheritance and public member becomes public in
derived class in public inheritance
d.mul();
d.display();
return 0;
}
Example 2:
class B //parent class/base class
{
int a;
public:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : private B //child class/derived class
{
int c;
/* int b;
void set_ab();
int get_a(void);
void show_a(void);
*/ available because of inheritance
public:
void mul(void);
void display(void);
};
void B:: set_ab(void){ a=5;b=10;}
int B::get_a(){ return a;}
void B::show_a(){ cout<<"a="<<a<<"\n";}
void D::mul(){ set_ab(); c=b*get_a();}
void D::display(){ cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";}
int main()
{
D d;
//d.set_ab();// through error
d.mul();
d.display();
return 0;
}
Example 3: Base class without protected member behaves similar to private
derivation
class B //parent class/base class
{
int a;
public:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : protected B //child class/derived class
{
int c;
public:
void mul(void);
void display(void);
/* protected:
int b;
void set_ab();
int get_a(void);
void show_a(void);
*/ available because of inheritance
};
void B:: set_ab(void){ a=5;b=10;}
int B::get_a(){ return a;}
void B::show_a(){ cout<<"a="<<a<<"\n";}
void D::mul(){ set_ab(); c=b*get_a();}
void D::display(){ cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";}
int main()
{
D d;
//d.set_ab();// through error
d.mul();
d.display();
return 0;
}
private:
1) It can be accessed inside the class only.
2) It doesn't take part in inheritance.
protected:
1) It can be accessed inside the class only.
2) Takes part in inheritance.
Example 4: Base class with protected member protected derivation
class B //parent class/base class
{
int a;
protected:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : protected B //child class/derived class
{
int c;
public:
void mul(void);
void display(void);
/* protected:
int b;
void set_ab();
int get_a(void);
void show_a(void);
*/ available because of inheritance
};
void B:: set_ab(void){ a=5;b=10;}
int B::get_a(){ return a;}
void B::show_a(){ cout<<"a="<<a<<"\n";}
void D::mul(){ set_ab(); c=b*get_a();}
void D::display(){ cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";}
int main()
{
D d;
//d.set_ab();// through error
d.mul();
d.display();
return 0;
}
Example 5: Base class with protected member in private derivation
class B //parent class/base class
{
int a;
protected:
int b;
void set_ab();
int get_a(void);
void show_a(void);
};
class D : private B //child class/derived class
{
int c;
/* int b;
void set_ab();
int get_a(void);
void show_a(void);
*/ available because of inheritance
public:
void mul(void);
void display(void);
};
void B:: set_ab(void){ a=5;b=10;}
int B::get_a(){ return a;}
void B::show_a(){ cout<<"a="<<a<<"\n";}
void D::mul(){ set_ab(); c=b*get_a();}
void D::display(){ cout<<"a="<<get_a()<<"\n"; cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";}
int main()
{
D d;
//d.set_ab();// through error
d.mul();
d.display();
return 0;
}