Unit 3 - Inheritance.ppt
Unit 3 - Inheritance.ppt
What is Inheritance?
3
Saves Time and Effort: The concept of
reusability achieved by inheritance saves the
programmer time and effort. Since the main code
written can be reused in various situations as
needed.
Increases Program Structure which results in
greater reliability.
Polymorphism
4
• General Format for implementing the concept of
Inheritance:
class derived_classname: access specifier baseclassname
5
6
class Foo { };
class Bar : public Foo { };
class More : public Foo, public Bar { };
Public vs private inheritance
■ The public keyword in the inheritance syntax
means that publicly accessible members inherited
from the base class stay publicly accessible in the
derived class
■ But sometimes its preferable to inherit the public
members of a parent in such a way that they
become private in the child
Private inheritance
■ Public members of base class become
private members of derived class
■ Public and protected members are only
available to derived-class member
functions - not to a derived object.
class Motherboard {
public:
void Foo();
protected:
void MooCow(); };
class Computer : private Motherboard
{ public:
void Bar() {
Foo(); // this is okay. It calls Motherboard::Foo()
MooCow(); // this is also okay because Computer can access protected
// Motherboard members
} };
int main()
{ Computer comp;
comp.Foo(); // this is a compiler error.
Return 0; }
Public Inheritance
* Public members of base class remain public
in derived class.
* protected members inherited as
protected and available for further
inheritance.
*private members are not inherited.
class DerivedClass : public BaseClass
#include <iostream> {
using namespace std; int k;
class BaseClass { public:
protected: void setk() {
int i, j; k = i*j; }
public: void showk() {
void set(int a, int b) { cout << k << endl; } };
i = a; int main()
j = b; { DerivedClass ob;
} ob.set(2, 3);
void show() { ob.show();
cout << i << " " << j << endl; ob.setk();
} ob.showk();
}; Return 0; }
Protected inheritance
■ private members of the base class are not
accessible in the derived class (to preserve
encapsulation)
■ Protected qualification allows encapsulated data
members which are not publicly accessible to be
accessible by derived classes
■ ("Protected" members are not accessible from
outside the class, except in derived classes)
#include <iostream>
using namespace std;
class BaseClass {
int i;
i nt main()
Protected: int j;
{
Public: int k;
DerivedClass ob;
void setInt(int a) { i = a; }
int getInt() { return i; }
ob.setk(10);
};
cout << ob.getk() << ' ';
class DerivedClass : protected
BaseClass { ob.setj(12);
void setj(int a) {
j = a; } return 0;
void setk(int a) { k = a; } }
int getj() { return j; }
int getk() { return k; } };
Multilevel Inheritance
• ALGORITHM:
• • Start the process
• • Invoke the base class student
• • Invoke the derived class test which in inherited by the class
student
• • Invoke the derived class result which in inherited by the class
test
• • Create an object student1 for the result class
• • Call student1.getno(),assign the value of rno in student class
• • Call student1.getmarks(),assign the marks in test class
• • Call student1.display(),for displaying the result
• • Stop the process
• PROGRAM
• #include
• #include
• class student
• { protected:
• int rno;
• public:
• void getno(int);
• void putno(void);
• };
• void student::getno(int a)
• { rno=a; }
• void student ::putno()
• {cout<<"rollno="<<<<"marks in sub1="<<<<"marks in sub2="<<<<"total="<<
• Class test :public student
• { protected : float sub1,sub2;
• public : void get_marks( float,float);
• void put_marks(void);};
• void test:: get_marks(float x,float y)
• { sub1=x; Int main()
• sub2=y;} { result student1;
student1.get_number(90);
• void test::put_marks()
student1.get_marks(35.6,76
• { cout<<sub1<<sub2;} .5);
• class result :public test { stuydent1.display();
• float total; Return 9;
• public: }
• void display(void);
• };
• void result :: display(void)
• { total=sub1+sub2;
• put_number();
• put_marks();
• cout<<”total=”<<total;
• }
Multiple inheritance
• You can derive a class from any number of base classes.
Deriving a class from more than one direct base class is
called multiple inheritance.
• In the following example, classes A, B, and C are direct
base classes for the derived class X:
• class A { /* ... */ };
• class B { /* ... */ };
• class C { /* ... */ };
• class X : public A, private B, public C { /* ... */ };
Multiple inheritance
Sports
test
result
Hybrid inheritance
#include<iostream.h>
#include<conio.h>
class stu
{ protected: int rno;
public: void get_no(int a) {
rno=a; }
void put_no(void)
{cout<<"Roll no"<<rno<<"\n"; } };
class test : public stu
{ protected: float part1,part2;
public: void get_mark(float x,float y)
{ part1=x; part2=y; }
void put_marks() {
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n"; } };
class sports {
protected: float score;
public: void getscore(float s) { score=s; }
void putscore(void) { cout<<"sports:"<<score<<"\n"; }
};
class result: public test, public sports
{ float total;
public: void display(void); };
void result::display(void) { total=part1+part2+score;
put_no(); put_marks(); putscore();
cout<<"Total Score="<<total<<"\n"; }
int main()
{ clrscr();
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0; }
OUTPUT : Roll no 123
Marks obtained : part1=27.5
Part2=33
Sports=6
Total score = 66.5
Virtual base classes
Grandparent
Parent 1 Parent 2
child
Virtual base classes
•Virtual base class is a base class acts as an indirect base .
•EX: suppose we have a base class A , which have a data
member x as integer.
•class b: virtual public A
•{ };
•class C : virtual public A
•{ };
•class d: public B,public C
•{ };
•without making classes A and B as virtual class d had two
copies of x.This will arised ambguity problem.
Multiple access (C++ only)
In an inheritance graph containing virtual base classes, a
name that can be reached through more than one path is
accessed through the path that gives the most access.
For example:
class L { public: void f(); };
class B1 : private virtual L { };
class B2 : public virtual L { };
class D : public B1, public B2 {
public:
the function f() is accessed through
void f() {
class B2. Because class B2 is inherited
L::f(); } publicly and class B1 is inherited
}; privately,..
zThe problem
class Animal
{ public:
void eat(); };
class Mammal : public Animal
{ public: void walk(); };
class WingedAnimal : public Animal
{ public: void flap(); };
// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {};
The solution
class Animal
{ public:
void eat(); };
class Mammal : public virtual Animal
{ public: void walk(); };
class WingedAnimal : virtual public Animal
{ public: void flap(); };
// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {};
Abstract classes
• Abstract class is a class that can not be instantiated, it
exists extensively for inheritance and it must be
inherited.
• There are scenarios in which it is useful to define classes
that is not intended to instantiate
• Abstract classes cannot be used to instantiate objects;
because abstract classes are incomplete, it may contain
only definition of the properties or methods and
derived classes that inherit this implements it's
properties or methods.
Constructors in derived classes
};
•Initialization list.
Class alpha { };
Class beta { };
Class gamma {
alpha a;
gamma( int x,int y, float z) :
beta b; a(x), b(x,z)
Public: { assignment section ( for
gamma(arglist): a(arglist1), b(arglist2) ordinary other members) }
{ }
};