Inheritance
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
Example:
class student : public person
{
char rollno[10];
char coursename[30];
}
Single Inheritance Example:
return 0;
}
Example 2:
return 0;
}
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
return 0;
}
return 0;
}