0% found this document useful (0 votes)
26 views42 pages

Wa0007.

Inheritance in programming allows the creation of derived classes from base classes, enabling code reusability and the addition of new features. There are various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, each with unique structures. Member access specifiers determine how base class members are accessed in derived classes, and concepts such as function overriding and virtual base classes help manage complexities in inheritance relationships.

Uploaded by

Venomous Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views42 pages

Wa0007.

Inheritance in programming allows the creation of derived classes from base classes, enabling code reusability and the addition of new features. There are various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, each with unique structures. Member access specifiers determine how base class members are accessed in derived classes, and concepts such as function overriding and virtual base classes help manage complexities in inheritance relationships.

Uploaded by

Venomous Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Inheritance

Chapter 5
• Inheritance is the process of creating new
classes, called derived classes, from existing or
base classes.
• The derived class inherits all the capabilities
(data and functions) of the base class and can
add features and refinements of its own.
• The base class is unchanged by this process.
• Its big advantage is that it permits code
reusability.
• A class can be derived from more than one
classes, which means it can inherit data and
functions from multiple base classes.
• Syntax:
class derived_class_name : access specifier
base_class_name
– Where access-specifier can be public or
protected or private.
– If the access-specifier is not used, then it is
private by default.
• Eg.
i. class Rectangle: public Shape
ii. class C: public A, public B
• Eg. class Person
{ protected:
char name[10];
int adhar_id , age ;
public:
: };
class Teacher : public Person
{ int teacher_id;
float sal;
public:
:
};
Types of Inheritance:
1. Single Inheritance:
– It is the inheritance hierarchy wherein one
derived class inherits from one base class.
– Eg.
class B : public A
2. Multiple Inheritance:
– One derived class inherits from multiple base
classes.
– Eg.
class C : public A, public B
3. Multilevel Inheritance:
– It is the inheritance hierarchy wherein subclass
acts as a base class for other classes.
– Eg.
class B : public A
class C : public B
4. Hierarchical Inheritance:
– Here multiple subclasses inherit from one base
class.
– Eg. class B: public A
class C: public A
class D: public A
5. Hybrid Inheritance:
– It reflects any legal combination of other four
types of inheritance.
– Eg.
class B : public A
class C : public A
class D : public B, public C
Member Access How Members of the Base Class
Specifier Appear in the Derived Class

Private members of the base class are


inaccessible to the derived class.

Protected members of the base class


become private members of the
Private derived class.

Public members of the base class


become private members of the
derived class.
Member Access How Members of the Base Class Appear
Specifier in the Derived Class

Private members of the base class are


inaccessible to the derived class.

Protected members of the base class


Protected become protected members of the
derived class.

Public members of the base class become


protected members of the derived class.
Member Access How Members of the Base Class Appear in
Specifier the Derived Class

Private members of the base class are


inaccessible to the derived class.

Protected members of the base class


public become protected members of the derived
class.

members of the base class become public


members of the derived class.
class Shape
{ protected:
float width, height;
public:
void set_data (float a, float b)
{ width = a; height = b; }
};
class Rectangle: public Shape
{ public:
float area ()
{ return (width * height); }
};
class Triangle: public Shape
{ public:
float area ()
{ return (width * height / 2);}
};
int main ()
{ Rectangle rect; Triangle tri;
rect.set_data (5,3);
tri.set_data (2,5);
cout << rect.area() << endl;
cout << tri.area() << endl;
return 0; }
• Eg Multiple Inheritance
#include <iostream.h>
class Shape
{ protected:
float width, height;
public:
void setWidth_Height(float w, float h)
{ width = w;
height = h;
}
};
class PaintCost
{ public:
float getCost(float area)
{ return area * 70; }
};
class Rectangle: public Shape, public PaintCost
{ public:
int getArea()
{ return (width * height); }
};
int main()
{
Rectangle Rect; int area;
Rect.setWidth_Height (5.0,7.0);
area = Rect.getArea();
cout<<"area: "<< Rect.getArea()<<endl;
cout << "paint cost: "<< Rect.getCost(area) <<
endl;
return 0;
}
• Eg. Multilevel Inheritance.
#include <iostream.h>
class A
{ public:
void display()
{ cout<<"Base class content."; }
};
class B : public A { };
class C : public B { };
int main()
{ C obj;
obj.display();
return 0; }
Derived Class Constructors
• Base class constructors are automatically
called for you if they have no argument.
• If you want to call a base class constructor
with an argument, you must call it from the
subclass's constructor.
• Once the base constructor has finished
constructing the base portion of the class,
control returns to the derived constructor
and it executes as normal.
• Destructors
- When a derived class is destroyed, each
destructor is called in the reverse order of
construction.
Eg. Consider this multilevel
Inheritance A->B->C

-When Object of Class C is destroyed, the C


destructor is called first, then the B destructor, then
the A destructor.
-For Constructors the execution order will be A then
B and then C constructor
• Eg.
#include <iostream.h>
class A
{ int nValue;
public:
A(int Value)
{ nValue=Value;}
void display()
{ cout<<"A: <<nValue<<endl;}
};
class B: public A
{ double dValue;
public:
B(int Value,double dVal) : A(Value)
{ dValue =dVal;}
void B_display()
{ display();
cout<<"B: "<<dValue<< endl;}
};
class C: public B
{ char chValue;
public:
C(int Value, double dVal,char chVal) : B(Value, dVal)
{ chValue=chVal;}
void C_display()
{ B_display();
cout <<"C: "<<chValue<<endl;}
};
int main()
{ C cobj(5, 4.3, 'R'); cobj.C_display(); return 0; }
Overriding Member Functions
• You can use member functions in a derived class
that override (have the same name and parameters
as) the function of base class.
• If we inherit a class into the derived class and
provide a definition for one of the base class’s
function again inside the derived class, then that
function is said to be overridden, and this
mechanism is called Function Overriding.
• Connecting the function call to the function body is
called Binding. When it is done before the program
is run, its called Early Binding or Static Binding
or Compile-time Binding.
• Eg.
class Base
{
public:
void show()
{
cout<<“Base class”<<endl;
}
};
class Derived: public Base
{
public:
void show()
{
cout<<“Derived Class”<<endl;
}
};
int main()
{
Base b; Derived d;
b.show(); //Early Binding Occurs
d.show(); return 0;
}
• O/P
Base class
Derived Class

• If you create an object of the derived class and call


the member function which exists in both classes
(base and derived i.e. overridden function), then
member function of the derived class is invoked
and the function of the base class is ignored.
Scope resolution with overridden functions
• Eg.
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
void set_a(int val)
{
a=val;
}
void show()
{
cout<<"A is "<<a<<endl;
}
};
class B:public A
{ double b;
public:
void set_b(int val,double bval)
{
set_a(val);
b=bval;
}
void show()
{ A:: show()
cout<<"B is "<<b<<endl;
}
};
class C:public B
{ char c;
public:
void set_c(int val,double b,char ch)
{
set_b(val,b);
c=ch;
}
void show()
{ B::show();
cout<<"C is "<<c<<endl;
}
};
void main()
{
C c1;
clrscr();
c1.set_c(10,200.555,'S');
c1.show(); }
Difference between Function
Overloading and Function overriding
• Function overloading and Function overriding both
are examples of polymorphism but they are
completely different.
• Function overloading is a feature that allows us to
have same function more than once in a program.
• Overloaded functions have same name but their
signature/ prototype must be different.
• Function overriding (in Inheritance) is a feature
of OOP that allows us to override a function of
parent class in child class.
Virtual Base Class
• Suppose you have two derived
classes B and C that have a common base
class A, and you also have another class D that
inherits from B and C.
• Each nonvirtual object contains a copy of the
data members defined in the base class.
• This duplication wastes space and requires you
to specify which copy of the base class
members you want whenever you access
them.
• An ambiguity can arise when several paths
exist to a class from the same base class.
• That is, a child class could have duplicate
sets of members inherited from a single
base class.
• C++ solves this issue by introducing a
virtual base class.
• When a class is made virtual, necessary
care is taken so that the duplication is
avoided regardless of the number of paths
that exist to the child class.
• When a base class is specified as a virtual
base, it can act as an indirect base more
than once without duplication of its data
members.
• A single copy of its data members is
shared by all the base classes that use it
as a virtual base.
• When declaring a virtual base class,
the virtual keyword appears in the base
lists of the derived classes.
• Eg.
class A
{
public:
int i;
};

class B : virtual public A


{
public:
int j;
};
class C: virtual public A
{
public:
int k;
};

class D: public B, public C


{
public:
int sum;
};
int main()
{
D ob;
ob.i = 10; //unambiguous as only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << “Value of i is : ”<< ob.i<<”\n”;
cout << “Value of j is : ”<< ob.j<<”\n”;
cout << “Value of k is :”<< ob.k<<”\n”;
cout << “Sum is : ”<< ob.sum <<”\n”;
return 0; }
• Eg.
#include<iostream.h>
#include<conio.h>
class student
{ protected:
int rno;
public:
void getnumber()
{ cout<<"Enter Roll No:";
cin>>rno; }
void putnumber()
{ cout<<"RollNo:"<<rno<<endl; }
};
class test: virtual public student
{ public:
int sem1,sem2;
void getmarks()
{ cout<<"Enter Marks\n";
cout<<“Sem1:";
cin>>sem1;
cout<<“Sem2:";
cin>>sem2; }
void putmarks()
{ cout<<"\tMarks Obtained\n";
cout<<"\n\tSem1:"<<sem1;
cout<<"\n\tSem2:"<<sem2; }
};
class sports: public virtual student
{ public:
int score;
void getscore()
{ cout<<"Enter Sports score:";
cin>>score;
}
void putscore()
{
cout<<"Sports score is:"<<score;
}
};
class result:public test,public sports
{
int total;
public:
void display()
{
total=sem1+sem2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}

You might also like