inheritance

40
Inheritance

Upload: munsif-ullah

Post on 12-Jul-2015

47 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Inheritance

Inheritance

Page 2: Inheritance

Inheritance• A programming technique that is used to reuse

an existing class to build a new class is known as inheritance.

• The new class inherits all the behavior of the original class.

• The existing class that is reused to create a new class is known as super class or parent class.

• The new class that inherits the properties and functions of an existing class is known as subclass, derived class or child class.

• The inheritance relationship between the classes of a program is called a class hierarchy.

Page 3: Inheritance

• Inheritance is one of the most powerful features of object-oriented programming.

• The basic principal of inheritance is that each subclass shares common properties with the class from which it is derived.

• The child class inherits all capabilities of the parent class and can add its own capabilities.

Page 4: Inheritance

• Suppose we have a class named vehicle.

• The subclasses of this class may share similar properties such as wheels and motor etc.

• Additionally, a subclass may have its own particular characteristics.

• For example, a subclass Bus may have seats for people but another subclass Truck may have space to carry goods.

• A class of animals can be divided into sub classes like mammals, insects etc.

• A class of vehicles can be divided into cars, trucks, buses, and motorcycles.

Page 5: Inheritance

• The above figure shows that Vehicle is parent class and Bus, Truck and Motorcycle are three sub classes. The upward arrows indicate that the subclasses are derived from parent Vehicle class.

Vehical

Truck MotorcycleBus

Page 6: Inheritance

Advantages of InheritanceSome important advantages of inheritance are as

follows:1. Reusability:

Inheritance allows the developer to reuse existing code in many situations. A class can be created once and it can be reused again and again to create many sub classes.

2. Saves Time and Effort:inheritance saves a lot of time and effort to write the same classes again.

3. Increases Program Structure and Reliability:A super class is already compiled and tested properly. This class can be used in a new application without compiling it again. The use of existing class increases program reliability.

Page 7: Inheritance

Categories of Inheritance• There are two categories of inheritance

1. Single Inheritance:A type of inheritance in which a child class is derived from single parent class is known as single inheritance. The child class in this inheritance inherits all data members and member functions of the parent class. It can also add further capabilities of its own.

Parent

Child

Page 8: Inheritance

2. Multiple Inheritance:

A type of inheritance in which a child class is derived from multiple parent classes is known as multiple inheritance. The child class in this inheritance inherits all data members and member functions of all parent classes. It can also add further capabilities of its own.

Parent2

Child

Parent1

Page 9: Inheritance

Protected Access Specifier

• The private data members of a class are only accessible in the class in which they are declared.

• The public data members are accessible from anywhere in the program.

• The protected access specifier is different from private and public access specifiers.

• It is specially used in inheritance.• It allows a protected data member to be accessed from

all derived classes but not from anywhere else in the program.

• It means that child class can access all protected data members of its parent class.

Page 10: Inheritance

Specifying a Derived Class• The process of specifying derived class is same as

specifying simple class.

• Additionally, the reference of parent is specified along with derived class name to inherit the capabilities of parent class.

• Syntax:class sub_class : specifier parent_class{

body of the class};

Page 11: Inheritance

class It is keyword that is used to declare a class.

sub_class It is the name of derived/child/sub class.

: It creates a relationship between derived class and super/parent/base class.

specifier It indicates the type of inheritance. It can be private, public or protected.

parent_class It indicates the name of parent/super/base class that is being inherited.

Page 12: Inheritance

class Move {protected:int position;public:Move() {position = 0 ; }void forward() {

position++; }void show() {

cout<<“Position = “<< position <<endl; }

};

class Move2 : public Move {public:void backward() {

position--;}

};

main() {Move2 m;m.show();m.forward();m.show();m.backward();m.show();

}

Page 13: Inheritance

Accessing Members of Parent Class• An important issue in inheritance is the

accessibility of base class members by the objects of derived class.

• It is known as accessibility.

• The objects of derived class can access certain members of parent class.

– Accessing Constructors of Parent Class

– Accessing Member Functions of Parent Class

Page 14: Inheritance

Accessing Constructors of Parent Class• The objects of derived class can access certain

constructors of parent class.

• If there is no constructor in derived class the compiler automatically uses the constructor of parent class.

• The objects of derived class can automatically access the constructors of parent class with no parameter.

• The derived class can also access constructors of parent class with parameters by passing values to them.

Page 15: Inheritance

Syntax:The syntax of accessing the constructor of parent class in derived class is as follows:

child_constrctr (parameters): parent_constrctr(parameters){

body of constructor}

child_constrctr Name of constructor of derived class.

parent_constrctr Name of constructor of parent class.

parameters List of parameters passed to the constructor of parent class.

Page 16: Inheritance

class Parent {protected:int n;public:Parent() {n=0;}Parent(int p) {n=p;}void show() {cout<<“ n = “<<n << endl;}

};

class Child : public Parent{private:char ch;public:Child() : Parent() {ch = ‘x’;}Child(char c, int m) : Parent(m) {ch=c;}void display(){cout<<“ch = “<<ch<<endl;}

};

Page 17: Inheritance

main() {Child obj1, obj2(‘$’, 100);cout<<“Obj1 is as follow”<<endl;obj1.show();obj1.display();cout<<“Obj2 is as follows”<<endl;obj2.show();obj2.display();

}

Page 18: Inheritance

Accessing Member Functions of Parent Class

• The objects of derived class can access all member functions of parent class that are declared as protected or public.

Page 19: Inheritance

Write a class Person that has the attributes of id, name and address. It has a constructor to initialize, a member function to input and a member function to display data members. Create another class Student that inherits Person class. It has additional attributes of roll number and marks. It also has member function to input and display its data members.

Page 20: Inheritance

class Person {protected:int id;string name, address;public:Person() {id=0;name = “Name”;address =“address”}void getInfo() {cout<<“Enter your id”;cin>>id;cout<<“Enter your name”;cin>>name;cout<<“Enter your address”;cin>>address;}

void showInfo() {cout<<“your personal information”cout<<“ id = “<<id<<endl;cout<<“Name = “<<name<<endl;cout<<“Address = “<<address<<endl;}

};class Student : public Person {private:int rno, marks;public:Student() {Person:: Person();rno=marks=0;}

Page 21: Inheritance

void getEdu() {cout<<“Enter your roll no”;cin>>rno;cout<<“Enter your marks”;cin>>marks;void showEdu() {cout<<“Your education information is as follows”;cout<<“Roll no = “<<rno<<endl;cout<<“Marks = “<<marks<<endl;}

};main() {Student s;s.getInfo():s.getEdu();s.showEdu();}

Page 22: Inheritance

Function Overriding

• The process of declaring member function in derived class with same name and same signature as in parent class is known as function overriding.

• Function overriding allows the user to use same names for calling the member functions of different class.

• When a member function is overridden in the derived class, the object of derived class cannot access the function of parent class.

• However, the function of parent class can be accessed by using scope resolution operator.

Page 23: Inheritance

class Parent {protected:int n;public:Parent(int p){n=p;}void show() {cout<<“n = “<<n<<endl;}

};

class Child : public Parent {private:char ch;

Public:Child(char c, int m) : Parent(m) {ch=c;}void show() {Parent::show();cout<<“ch = “<<ch<<endl;}

};main()

{Child obj(‘$’, 100);obj.show();

}

Page 24: Inheritance

Types of Inheritance

• A parent class can be inherited using public, protected or private type of inheritance.

• The type of inheritance defines the access status of parent class members in the derived class.

• Different types of inheritance are as follows:

– Public Inheritance

– Protected Inheritance

– Private Inheritance

Page 25: Inheritance

Public Inheritance• In public inheritance, the access status of parent class

members in the derived class remains the same.

• The public members of parent class become publicmembers of derived class.

• The protected members of parent class become protected members of derived class.

• The private members of parent class become privatemembers of derived class.

• The syntax of defining public inheritance is as follows:

class child_class : public parent_class {body of the class}

Page 26: Inheritance

The accessibility of derived class in public inheritance is as follows:

• The derived class can access the public members of parent class.

• The derived class can access the protected members of parent class.

• The derived class cannot access the private members of parent class.

The accessibility of an object of derived class is as follows:

• The object of derived class can access the public members of parent class.

• The object of derived class cannot access the protected members of parent class.

• The object of derived class cannot access the private members of parent class.

Page 27: Inheritance

Write a program that declares two classes and defines a relationship between them using public members.

Page 28: Inheritance

class parent {public:int a;protected:int b;private:int c;};class child : public parent{public:void in() {cout<< “Enter a “;cin>>a;cout<<“Enter b “;cin>>b;}

void out() {cout<<“a = “<<a<<endl;cout<<“b = “<<b<<endl;}

};

main() {child obj;obj.in();obj.out();

}

Page 29: Inheritance

Protected Inheritance

• In Protected inheritance, the access status of parent class members in the derived class is restricted.

• The public members of parent class become protected members of derived class.

• The protected members of parent class become protected members of derived class.

• The private members of parent class become private members of derived class.

Page 30: Inheritance

• The syntax of defining protected inheritance is as follows:class child_class : protected parent_class{body of the class}

Page 31: Inheritance

The accessibility of derived class in protected inheritance is as follows:

• The derived class can access the public members of parent class.

• The derived class can access the protected members of parent class.

• The derived class cannot access the private members of parent class.

The accessibility of an object of derived class is as follows:• The object of derived class cannot access the public

members of parent class.• The object of derived class cannot access the protected

members of parent class.• The object of derived class cannot access the private

members of parent class.

Page 32: Inheritance

class parent {public:int a;protected:int b;private:int c;};class child : protected parent{public:void in() {cout<< “Enter a “;cin>>a;cout<<“Enter b “;cin>>b;}

void out() {cout<<“a = “<<a<<endl;cout<<“b = “<<b<<endl;}

};

main() {child obj;obj.in();obj.out();

}

Page 33: Inheritance

Private Inheritance• In private inheritance, the access status of parent

class members in the derived class is restricted.• The private, protected and public members of

parent class all become the private members of derived class.

• The syntax of defining private inheritance is as follows:

class child_class : private parent_class{body of the class}

Page 34: Inheritance

The accessibility of derived class in private inheritance is as follows:

• The derived class can access the public members of parent class.

• The derived class can access the protected members of parent class.

• The derived class cannot access the private members of parent class.

The accessibility of an object of derived class is as follows:

• The object of derived class cannot access the public members of parent class.

• The object of derived class cannot access the protected members of parent class.

• The object of derived class cannot access the private members of parent class.

Page 35: Inheritance

Multilevel Inheritance

• A type of inheritance in which a class is derived from another derived class is called multilevel inheritance.

• In multilevel inheritance, the members of parent class are inherited to the child class and the members of child class are inherited to the grand child class.

• In this way, the members of parent class and child class are combined in grand child class.

Page 36: Inheritance

• Example:class A {body of the class};class B : public A {body of the class};class C : public B {body of the class };

Page 37: Inheritance

class A {private:int a;public:void in() {cout<< “Enter a “;cin>>a;}void out() {cout<< “The value of a is “<<a<<endl;} };class B : public A {private:int b;public:void in() {A::in();cout<<“Enter b “;cin>>b;}

void out() {A::out();cout<<“The value of b is “<<b <<endl;} };class C : public B {private :int c :public :void in(); {B::in();cout<<“Enter c “;cin>>c;}void out() {B::out();cout<<“The value of c is“<<c<<endl;} };main() {C obj;obj.in();obj.out();}

Page 38: Inheritance

Multiple Inheritance

• A type of inheritance in which a derived class inherit multiple base classes is known as multiple inheritance.

• In multiple inheritance, the derived class combines the members of all base classes.

• Syntax:class child_class : specifier Parent_class1, specifier parent class2…………….. {body of the class }

Page 39: Inheritance

• Example:class A {body of the class};class B {body of the class};class c : public A, public B {body of the class};

Page 40: Inheritance

class A {private:int a;public:void in() {cout<<“Enter a “;cin>>a;}void out() {cout<<“a = “a<<endl;} };class B {private:int b;public:void input() {cout<<“Enter b “;cin>>b;}void output() {cout<<“b = “<<b<<endl; } };

class C : public A, public B {private:int c;public:void get() {A::in();b::input();cout<<“Enter c “;cin>>c;}void show() {A::out();B::output();cout<<“c = “<<c<<endl;} };C obj;obj.get();obj.show();}