+ All Categories
Home > Documents > Inheritance - Multilevel

Inheritance - Multilevel

Date post: 22-Feb-2016
Category:
Upload: nevin
View: 71 times
Download: 1 times
Share this document with a friend
Description:
Inheritance - Multilevel. MEENA RAWAT PGT (COMP) KV ONGC Chandkheda. Contents. Objectives Concept of Inheritance – Need, Example Type of Class Type of Inheritance - forms Single level Inheritance Visibility Modes Multi level Inheritance. Objectives. - PowerPoint PPT Presentation
Popular Tags:
21
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1
Transcript
Page 1: Inheritance -  Multilevel

1

Inheritance - Multilevel

MEENA RAWATPGT (COMP)

KV ONGC Chandkheda

Page 2: Inheritance -  Multilevel

2

Contents

1. Objectives2. Concept of Inheritance – Need, Example3. Type of Class4. Type of Inheritance - forms5. Single level Inheritance6. Visibility Modes7. Multi level Inheritance

Page 3: Inheritance -  Multilevel

3

Objectives

• In this session, you will learn:– To be able to create new classes by inheriting from

existing classes.– To understand how inheritance promotes software

reusability.– To understand the notions of base classes and derived

classes. – Forms of Inheritance

Page 4: Inheritance -  Multilevel

4

Concept of Inheritance - Need

• The mechanism by which one class can inherit the properties of another class.

• It allows a hierarchy of classes to be built, moving from the most general to the most specific.

• Code reusability - one class can be used without redefining or rewrite. It makes faster development time, easier to maintenance, easy to extend.

• Capability to express the inheritance relationship• Transitive nature of nature

Page 5: Inheritance -  Multilevel

5

Real Life Examples

Page 6: Inheritance -  Multilevel

6

Vehicles

Automobiles

Cars Bike

Non Auto Mobile

Cycle

Page 7: Inheritance -  Multilevel

7

Type of Class

• Base Class / Super Class– Defines all qualities attributes common to any

derived classes.• Derived Class / Sub Class– Inherits those general properties and adds new

properties that are specific to that class.

Page 8: Inheritance -  Multilevel

8

Different forms of Inheritance

1. Single Inheritance – one base class, one derived class

2. Multiple Inheritance – multiple base class , One derived class

3. Hierarchical Inheritance - One base class , multiple derived class

4. Multilevel Inhetitance – derived from derived class

5. Hybrid Inheritance – mixture of above

Page 9: Inheritance -  Multilevel

9

Single Level Inheritance

One Base Class one derived classSyntax:Class derived_class_name : visibility_mode base_class_name{

class members};

base

derived

Page 10: Inheritance -  Multilevel

10

Example of Single Level Inheritanceclass base // base class{

int x;public:void setx(int n) { x = n; }void showx() { cout << x << ‘\n’ }

};// Inherit as publicclass derived : public base //derived class{

int y;public:void sety(int n) { y = n; }void showy() { cout << y << ‘\n’;}

};int main() {

derived ob;ob.setx(10);ob.sety(20);ob.showx();ob.showy();

}

Page 11: Inheritance -  Multilevel

11

Visibilit modes

1. Specified whether the features of the base class are privately or publically or protected derived.

2. Control the access-specifier to be for inheritable members of base class in the derived class.

Page 12: Inheritance -  Multilevel

12

Role of visibility modesVisibility Mode is Inheritable public

member becomes (in derived class)

Inheritable protected member becomes (in derived class)

Private member of base class are not directly accessible

to derived class

Public Public Protected

Protected Protected Protected

Private private private

Page 13: Inheritance -  Multilevel

13

Public Visibility ModeX check() private

Y display() public

Z getval() protected

A init() Private

Y display()B readit()

Public

Z getval()C writeval()

Protected

Class Super

Class Base

Page 14: Inheritance -  Multilevel

14

Private Visibility ModeX check() private

Y display() public

Z getval() protected

A init()Y display()Z getval()

Private

B readit() Public

C writeval() Protected

Class Super

Class Base

Page 15: Inheritance -  Multilevel

15

Protected Visibility ModeX check() private

Y display() public

Z getval() protected

A init() Private

B readit() Public

C writeval()Y display()Z getval()

Protected

Class Super

Class Base

Page 16: Inheritance -  Multilevel

16

Multilevel Inheritance(derived from a derived class)

Page 17: Inheritance -  Multilevel

17

Example• #include <iostream.h>• class mm• {• protected:• int rollno;• public:• void get_num(int a)• { rollno = a; }• void put_num()• { cout << "Roll Number Is:\n"<< rollno << "\n"; }• };• class marks : public mm• {• protected:• int sub1;• int sub2;• public:• void get_marks(int x,int y)• {• sub1 = x;• sub2 = y;• }• void put_marks(void)• {• cout << "Subject 1:" << sub1 << "\n";• cout << "Subject 2:" << sub2 << "\n";• }• };

Page 18: Inheritance -  Multilevel

18

• class res : public marks• {• protected: • float tot;• public:• void disp(void)• {• tot = sub1+sub2;• put_num();• put_marks();• cout << "Total:"<< tot;• }• };• int main()• {• res std1;• std1.get_num(5);• std1.get_marks(10,20);• std1.disp();• return 0;• }

Page 19: Inheritance -  Multilevel

19

Multiple Inheritance – many base class, one derived class

Syntax:Class derived_class_name : visibility_mode base_class_name , visibility_mode base_class_name{

class members};

Page 20: Inheritance -  Multilevel

20

Question asked in Board Exam• Answer the questions (i) to (iv) based on the following: 4• class PUBLISHER• {• char Pub[12];• double Turnover;• protected:• void Register();• public:• PUBLISHER();• void Enter();• void Display();• };• • class BRANCH• {• char CITY[20];• protected:• float Employees;• public:• BRANCH();• void Haveit();• void Giveit();• };• • class AUTHOR:private BRANCH,public PUBLISHER• {• int Acode;• char Aname[20];• float Amount;• public:• AUTHOR();• void Start();• void Show();• };• • Write the names of data members, which are accessible from objects belonging to class AUTHOR.• Write the names of all the member functions which are accessible from objects belonging to class BRANCH.• Write the names of all the members which are accessible from member functions of class AUTHOR.• How many bytes will be required by an object belonging to class AUTHOR?

Page 21: Inheritance -  Multilevel

21

Thank You.


Recommended