7
Prepared By : Rakesh Kumar Inheritance Inheritance :Inheritance is the process of creating a new class(derived classes) from existing classes( Base Classes). The derived classes not only inherit the capabilities of the base class abut also can add new features of their own. The process of inheritance does not affect the base class. The most important features of inheritance is that it allows reusability of code, and also a debugged class can be used to work in different situations. Reusability of code saves money as well as time and increase program reliability. Inheritance is very useful in original conceptualization and design of a programming problem. Types of inheritance Single level Inheritance Multi Level Inheritance Hierarchical Inheritance Multiple Inheritance Hybrid Inheritance Visibility Under Different Modes X X X Base Class Private Public Protected Privately Derived Derived Class X Private Private D.A.V. Centenary Public School Chander Nagar A C B A B A B C A B C A D B C Private Member Object Derived Class Protected Member Object Derived Class Public Member Object Derived Class

Inheritance

Embed Size (px)

Citation preview

Page 1: Inheritance

Prepared By : Rakesh Kumar

InheritanceInheritance :Inheritance is the process of creating a new class(derived classes) from existing classes( Base Classes). The derived classes not only inherit the capabilities of the base class abut also can add new features of their own. The process of inheritance does not affect the base class. The most important features of inheritance is that it allows reusability of code, and also a debugged class can be used to work in different situations. Reusability of code saves money as well as time and increase program reliability. Inheritance is very useful in original conceptualization and design of a programming problem.

Types of inheritanceSingle levelInheritance

Multi LevelInheritance

Hierarchical Inheritance

Multiple Inheritance

Hybrid Inheritance

C

Visibility Under Different Modes

X

X

X

Base Class Private Public Protected

Privately Derived Derived Class XPrivatePrivate

D.A.V. Centenary Public School Chander Nagar

A

C

B

A

B

A

B C

A B

C

A

D

B C

Private MemberObject

Derived Class

Protected Member Object

Derived Class

Public Member Object

Derived Class

Page 2: Inheritance

Prepared By : Rakesh Kumar

Base ClassPrivatePublicProtected

Publicly Derived Derived ClasXPublicProtected

Base ClassPrivatePublicProtected

Protected Derived Derived ClassXProtectedProtected

Single Inheritanceclass ClassName { <classmember;>; protected:

<classmember>; public: <class member>; };

class <Derived class name>:<Access specifier> <Base Class Name> { <class member>; protected: <class member>; public: <class member>; };

Example of single Level Inheritance

Class Country { int armyfunds; protected: Base Class

int infrastructure;public:

void provide(); void collect(); };class state :public country {

int no_of_farms;public: Derived Class

void Get( ); void Put ( ); };

D.A.V. Centenary Public School Chander Nagar

SYNTAXSingle Level Inheritance

Country

State

Page 3: Inheritance

Prepared By : Rakesh Kumar

Multi Level Inheritanceclass ClassName { <classmember;>; protected:

<classmember>; public: <class member>; };

class <Derived class name>:<Access specifier> <Base Class Name> { <class member>; protected: <class member>; public: <class member>; };

class <Devied class Name> :<Access Specifier> <Base Class Name> { <Class Member >;

protected:<Class member>;

public:<Class member>;

};

Example of Multi Level InheritanceClass Country { int armyfunds; protected: Base Class

int infrastructure;public:

void provide(); void collect(); };class state :public country {

int no_of_farms;public: Derived Class

void Get( ); void Put ( ); }; class District : public state { int no_of_offices;

public: Derived Classvoid input( ); void output( );

};

D.A.V. Centenary Public School Chander Nagar

SYNTAXMulti Level Inheritance

Country

State

District

Page 4: Inheritance

Prepared By : Rakesh Kumar

Multiple Inheritanceclass ClassName { <classmember;>; protected:

<classmember>; public: <class member>; };class ClassName { <classmember;>; protected:

<classmember>; public: <class member>; };class <Derived Class Name>:<Access Specifier><Base Class Name> , <Access Specifier>

<Base Class Name>{

<Class Member>;protected:

<class Member>;public:

<class member>;};

Example of multiple InheritanceClass Country { int armyfunds; protected: Base Class 1

int infrastructure;public:

void provide(); void collect(); };class Givernment{

int articleno; Base Class 2public:

void Get( ); void Put( );};Class Rules: public Country , public Government{

char description[80]; Derived Class public:

void input( ); void output( );}

D.A.V. Centenary Public School Chander Nagar

SYNTAXMultiple Inheritance

Country Government

Rules

Page 5: Inheritance

Prepared By : Rakesh Kumar

Hierarchical Inheritanceclass ClassName { <classmember;>; protected:

<classmember>; public: <class member>; };Class <Derived Class Name1>:<Access Specifier><Base Class Name>{ <classmember;>; protected:

<classmember>; public: <class member>; };Class <Derived Class Name2>:<Access Specifier><Base Class Name>{ <classmember;>; protected:

<classmember>; public: <class member>; };

Example of Hierarchical Inheritanceclass Country { int armyfunds; protected: Base Class

int infrastructure;public:

void provide(); void collect(); };class state :public country {

int no_of_farms;public: Derived Class

void Get( ); void Put ( ); };

class UnionTerritory : public Country{

int no_of_offices; Derived Classpublic:

void Input( ); void Output( );};

D.A.V. Centenary Public School Chander Nagar

SyntaxHierarchical Inheritance

Country

State Union Territory

Page 6: Inheritance

Prepared By : Rakesh KumarQ1. Answer the question with the help of the portion of program shown belowclass U{

int u;protected:

int y;public:

void haveit( );void showit();

};class T : public U{

int x1;void calc ( );

protected:int y1;void doit( );

public:int z1;void ShowIt();void Addit( );

};class P: private T{

int x2;protected:

int y2;public:

int z2;void Display();void Change( );

};

a) Name the member function(s), which can be accessed from the object of , class P.b) Name the data member(s), which can be accessed from the member function of class T.c) Name the data member(s), which can be accessed from the object of class U.d) is the data member Y declared in class U accessible to the member function Change( ) of class P.e) How many bytes will be required by an object of class P.f) Write the definition of member function display ( ) of class P to display all data members which

are accessible from it.g) Write the name of base class and derived class of class T.h) Name the inheritance shown in this example.

D.A.V. Centenary Public School Chander Nagar

Page 7: Inheritance

Prepared By : Rakesh KumarObject Oriented Programming & Procedural Programming

Object Oriented Programming Procedural ProgrammingEmphasis on data Emphasis on doing things( function)Follows bottom-up approach in program design Follow top down approach in program designConcept of data Hiding prevents accidental change in the data

Due to presence of global variables, there is a possibility of accidental change in data

Polymorphism, Inheritance , Data Encapsulation possible

Not Applicable

Data Encapsulation: Wrapping up of data and function in a single unit is known as data encapsulation. In a class , we encapsulate the data and function together in a single unit.

Data Hiding : Keeping the data in private/ Protected visibility mode of a class to prevent it from accidental modification ( Change) is known as data Hiding

Additional Definition____________________________________________________________________

Run Time Error : A run time error occurs during the execution of the program, when the program perform an illegal /unexpected operationFor example ( Division by Zero)X = U*H-P;y = Z/X ; // will be run time error if X becomes zero from the first statement

Syntax Error : A syntax error occurs when the compiler is unable to translate the program to machine language due to violation of rules of the programming language. For example ( in C++, if statement is not enclosed in brackets)

if x> 90cout<<””\n Value of x is 90”;

Logical Error : A logical error occurs when the program contains wrong formula or wrong manipulation, which may me syntactically correct. The program having logical error may give some output but not the expected one.For Example// int sum;float average;../…….,………

Average = sum /5;

D.A.V. Centenary Public School Chander Nagar