You are on page 1of 28

INHERITANCE

By, Girish Kumar N G

OOPS

INHERITANCE
Reusability is yet another important feature of oops.

Inheritance is the process of creating new classes from the existing classes. The new classes are called derived classes. The existing classes are called base class.
The derived classes inherit all the properties of the base classes plus & its own properties. The process of inheritance does not affect the base classes.

FLOW DIAGRAM

ADVANTAGE
New classes can be derived by the user from the existing classes without any modification. It saves time & money. It reduces program coding time. It increases the reliability of the program

DEFINING DERIVED CLASSES:


SYNTAX: class derived _class name : mode base_class_name { New member variable declaration; _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; New member function declaration & definition; _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; };

Where Class keyword. derived_class_name name of the new class. Mode private or public. The default is private. Base_class_name name of the already existing class.

TYPES OF INHERITANCE:
Single inheritance Multilevel inheritance Multiple inheritance Hierarchical inheritance

Hybrid inheritance

SINGLE INHERITANCE: If the derived class is derived from a single base class, then it is called single inheritance. Single inheritance can be derived publicly or privately.

PROGRAM:
#include <iostream.h> Class student { Private: int reg_no; protected: char name[20]; public: void read() { Cout<<\n Enter the reg_no;; Cin>>reg_no; Cout<<\n Enter the name;; Cin>>name; } Void print() { Cout<<\n reg_no=<<reg_no; } }; Class computer : public student { Private: int roll_no; public: void read1() { Read();// accessing public member in base class Cout<<\n Enter the roll.no; Cin>>roll_no; } Void print1() { Cout<< \n Roll-no=<<roll_no; Cout<<\n Name=<<name; } };

Void main() { Computer S1; S1.read1 (); S1.print (); S1.print1 (); }
EXECUTION PROCEDURE

When S1.read1 () is executed Enter the reg_no: 1 Enter the name: Ramu Enter the roll_no: 101 When s1.print() is Executed Reg -no=1 When s1.print() is executed Roll-no=101 Name=Ramu

MULTILEVEL INHERITANCE
Multilevel inheritance is a process to derive classes in a sequential order. A Base class is treated as level 1 class from this class we can derive a new class is treated as level 2 class. From the level 2

class we can derive another new class and is treated as level 3


class and so on. The lower level class inherits the higher level classes along the inheritance path.

FLOW DIAGRAM

SYNTAX FOR MULTILEVEL INHERITANCE:


Class base { _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; }; Class derive1: mode base { _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; }; Class derive2: mode derive1 { _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; }; Where Mode visibility modifier and can be private or public.

PROGRAM:
#include<iostream.h> Class two_wheeler : public vehicle Class vehicle // base class //derived class1 { { Protected: Protected: Char reg_no[12]; Int no_gear; Int model; Int power; Public: Public: Void read( ) Void read1( ) { { Cout<<\n enter the number and model:; Read( ); Cin>>reg_no>>model; cout<<\n enter the number of gear ,power:; } cin>>no_gear>>power; Void print() } { Void print1( ) Cout<<\n reg_no:<<reg_no; { Cout<<\n model=<<model; Print( ); } Cout<<\n No gear=<<no_gear; }; Cout<<\n power=<<power } };

Class scooter : public two_wheeler //derived class 2 { Protected: Char manufacture[20]; Char owner; Public: Void read2 ( ) { Read1( ); Cout<<\n enter the name of manufacture and owner; Cin>> manufacture>>owner; } Void print2( ) { Print1 ( ); Cout<<\n manufacture nam,e=:<< manufacture; Cout<<\n owner name=<<owner; } };

Void main( )
{

OUTPUT : enter the number and model: KA -74-7272 2009 enter the number of gear ,power:4 100 enter the name of manufacture and owner: TVS gopi reg-no = TN -74-7272

Scooter S1;
S1. Read2( );

model=2009 no_gear=4

S1.print2( );
}

power=100 manufacturer name= TVS owner=gopi

MULTIPLE INHERITANCES
A class derived from more tan one classes is called multiple inheritance.
FLOW DIAGRAM

SYNTAX:
Class base1 { _ _ _ __; }; Class base2 { _ _ _ _ _; }; Class base3 { _ _ _ _ _ _; }; Class derived : mode base1,mode base2,mode base3 { _ _ _ _ _ _ _ _ _ _ _ _ _ _; _ _ _ _ _ _ _ _ _ _ _ _ _ _; }; Where Mode visibility modifier and can be private or public

PROGRAM:
#include<iostream.h> Class income //base class { Protected: Float iamount; Public: Void read( ) { Cout<<\n give the iamount:; Cin>>iamount; } }; Class expenditure //base class { Protected: Float eamount; Public: Void read1( ) { Cout<<\n give the eamount:; Cin>>eamount; } };

Class n_income : public income, public expenditure { Private: Float namount; Public: Void print ( ) { Cout<<\n income=<<iamount; Cout<<\n expenditure=<<eamount; Cout<<\n Net amount=<<namount; } Void read2 ( ) { read( ) read1( ) namount=iamount-eamount; } };

Void main( ) { n_income n1; n1.read2( ); n1.print( ); } OUTPUT: give the iamount:1000.00 give the eamount:750.00 income:1000.00 expenditure:750.00 net amount:250.00

EXAMPLE:

HIERARCHY INHERITANCE

hierarchy inheritance is a process to drive classes in hierarchical order. a base class is treated as level1class. from this class we can derive new classes and is treated as level 2 classes. from a level 2 class we can derive another classes is treated as level 3 and so on The lower level classes inherits the higher level classes along inheritance path.

EXAMPLE

HYBRID INHERITANCE: An inheritance class derived from a base class and an inheritance class or two inheritances is called hybrid inheritance.

Example:

PROGRAM
#include<iostream.h> Class vehicle //base class { Protected: Char reg_no[10]; Int model; Public: Void read() { Cout<<\n enter the reg-no and model:; Cin>>reg-no>>model; } }; Class four_wheeler :public vehicle //inheritance class { Protected: Int no_cylinder; Public: Void read1( ) { Read( ); Cout<<\n enter the no.of cylinder:; Cin>>no_cylinder; } };

Class car : public four wheeler //inheritance class { Protected: Char name[10]; Char owner[20]; Int no_seats; Float price1( ); Public: Void read2( ) { Read1( ); Cout<<\n enter the name,owner,no_seat,price1; cin>>name>>owner>>no_seats>>pri ce1; } };

Class ac //base class { Protected: Float capacity; Char make[20]; Float price2; Public: Void read3( ) { Cout<<\n enter capacity, make, price2:; Cin>>capacity>>make>>price 2; } };

Class ac_car : public car,public ac //hybrid inheritance { Protected: Float total_price; Float fitting_charge; Public: ac_car() { Fitting_charge=10000.00; } Void read4( ) { Read2( ); Read3( ); Total_price=price1+price2; }

Void print( ) { Cout<<\n Reg_no=<<reg_no; Cout<<\n Model=<<model; Cout<<\n no_cylinder=<<no_cylinder; Cout<<\n Name=<<name; Cout<<\n Owner=<<Owner; Cout<<\n No_seats=<<no_seats; Cout<<\n Total_price=<<total_price+fit ting charge; } };

Void main( ) { ac_car a1; a1.read4( ); a1.print( ); } OUTPUT: enter the reg-no and model: TMM4002 2008 enter the no.of cylinder: 4 enterthename,owner,no_seat,price 1:Toyota gopi 6 100000 enter capacity, make, price2: 1.5 National 25000

Reg_no=TMM 4002 Model=2008 no_cylinder=4 Name=Toyota Owner=gopi No_seats=6 Total_price=125000

Abstract Class
It should be act as a base class. It should not be used to create any objects. The previous example vehicle is a abstract class. Because we creating object for ac_car class &also it is base class.

You might also like