Inheritance
Ms.VISHNU PRIYA N R
AP/CSE
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
AGNI COLLEGE OF TECHNOLOGY
Inheritance
• It is the process by which object of one class
acquires the properties of object of another
class.
• The class from which properties are inherited
is called base class and the class to which
properties are inherited is called derived
class.
• Inheritance is the process of creating a new
Class, called the Derived Class , from the
existing class, called the Base Class .
• The Inheritance has many advantages, the
most important of them being the
reusability of code.
• Rather than developing new Objects from
scratch, new code can be based on the work
of other developers, adding only the new
features that are needed.
• The reuse of existing classes saves time and
effort. 3
4
Why Inheritance ?
process by which object of one class acquires the properties
of object of another class. The class from which
properties are inherited is called base class and the class
to which properties are inherited is called derived class
• building class types from existing class types
• defining new class types to be a
– specialization
– augmentation
of existing types
5
What to inherit?
• In principle, every member of a base class is
inherited by a derived class
– just with different access permission
Public Inheritance (is a subtype)
– Publicly inherited remain public
– Private members can't be inherited
– Protected members remain protected ##
student
grad_student
shape
ellipse polygon
Types of Inheritance:
• Single Inheritance
• Multi Level Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
• Multiple Inheritance
•Single Inheritance: It is the inheritance hierarchy wherein one derived class
inherits from one base class.
• Multiple Inheritance: It is the inheritance hierarchy wherein one derived class
inherits from multiple base class(es)
• Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple
subclasses inherit from one base class.
• Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a
base class for other classes.
• Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination
of other four types of inheritance.
Benefits of Inheritance
– Code is reused
grad_student uses tested code from student
– Reflects relationship in problem domain
Special grouping grad student outgrowth of real world
and treatment of this group
– Polymorphic mechanisms allow client code to
treat inherited class as subtype of base class
Simplifies code, maintains subtype distinctions ##
10
11
12
13
14
19.2 Base and Derived Classes
• Often an object from a derived class (subclass) “is an”
object of a base class (superclass)
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
Fig. 19.1 Some simple inheritance examples.
is-a relationship
• Inheritance is an is-a relationship. We use
inheritance only if an is-a relationship is
present between the two classes.
• Here are some examples:
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal. 16
• Inheritance is usually declared as follows:
class subclassname : accessspecifier superclassname
{
//class specific code;
};
17
Given below is a complete
Example of Inheritance.
#include <iostream>
#include <string>
using namespace std;
class Animal
{
string name="";
public:
int tail=1;
int legs=4;
};
class Dog : public Animal
{
public:
void voiceAction()
{
cout<<"Barks!!!";
}
};
int main()
{
Dog dog;
cout<<"Dog has "<<dog.legs<<" legs"<<endl;
cout<<"Dog has "<<dog.tail<<" tail"<<endl;
cout<<"Dog ";
dog.voiceAction();
}
18
Output:
Dog has 4 legs
Dog has 1 tail
Dog Barks!!!
• We have a class Animal as a base class from which we have derived a
subclass dog. Class dog inherits all the members of Animal class and
can be extended to include its own properties, as seen from the output
19

INHERITANCES.pptx

  • 1.
    Inheritance Ms.VISHNU PRIYA NR AP/CSE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING AGNI COLLEGE OF TECHNOLOGY
  • 2.
    Inheritance • It isthe process by which object of one class acquires the properties of object of another class. • The class from which properties are inherited is called base class and the class to which properties are inherited is called derived class.
  • 3.
    • Inheritance isthe process of creating a new Class, called the Derived Class , from the existing class, called the Base Class . • The Inheritance has many advantages, the most important of them being the reusability of code. • Rather than developing new Objects from scratch, new code can be based on the work of other developers, adding only the new features that are needed. • The reuse of existing classes saves time and effort. 3
  • 4.
    4 Why Inheritance ? processby which object of one class acquires the properties of object of another class. The class from which properties are inherited is called base class and the class to which properties are inherited is called derived class • building class types from existing class types • defining new class types to be a – specialization – augmentation of existing types
  • 5.
    5 What to inherit? •In principle, every member of a base class is inherited by a derived class – just with different access permission
  • 6.
    Public Inheritance (isa subtype) – Publicly inherited remain public – Private members can't be inherited – Protected members remain protected ## student grad_student shape ellipse polygon
  • 7.
    Types of Inheritance: •Single Inheritance • Multi Level Inheritance • Hierarchical Inheritance • Hybrid Inheritance • Multiple Inheritance
  • 8.
    •Single Inheritance: Itis the inheritance hierarchy wherein one derived class inherits from one base class. • Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) • Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. • Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. • Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.
  • 9.
    Benefits of Inheritance –Code is reused grad_student uses tested code from student – Reflects relationship in problem domain Special grouping grad student outgrowth of real world and treatment of this group – Polymorphic mechanisms allow client code to treat inherited class as subtype of base class Simplifies code, maintains subtype distinctions ##
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    19.2 Base andDerived Classes • Often an object from a derived class (subclass) “is an” object of a base class (superclass) Base class Derived classes Student GraduateStudent UndergraduateStudent Shape Circle Triangle Rectangle Loan CarLoan HomeImprovementLoan MortgageLoan Employee FacultyMember StaffMember Account CheckingAccount SavingsAccount Fig. 19.1 Some simple inheritance examples.
  • 16.
    is-a relationship • Inheritanceis an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes. • Here are some examples: • A car is a vehicle. • Orange is a fruit. • A surgeon is a doctor. • A dog is an animal. 16
  • 17.
    • Inheritance isusually declared as follows: class subclassname : accessspecifier superclassname { //class specific code; }; 17
  • 18.
    Given below isa complete Example of Inheritance. #include <iostream> #include <string> using namespace std; class Animal { string name=""; public: int tail=1; int legs=4; }; class Dog : public Animal { public: void voiceAction() { cout<<"Barks!!!"; } }; int main() { Dog dog; cout<<"Dog has "<<dog.legs<<" legs"<<endl; cout<<"Dog has "<<dog.tail<<" tail"<<endl; cout<<"Dog "; dog.voiceAction(); } 18
  • 19.
    Output: Dog has 4legs Dog has 1 tail Dog Barks!!! • We have a class Animal as a base class from which we have derived a subclass dog. Class dog inherits all the members of Animal class and can be extended to include its own properties, as seen from the output 19