oop inheritance

26
Left Blank! 12/06/2022 Zubair Farooq (MCS) 1

Upload: zubair-ch

Post on 14-Apr-2017

311 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Oop inheritance

02/05/2023Zubair Farooq (MCS) 1

Left Blank!

Page 2: Oop inheritance

02/05/2023Hadziq Fabroyir - Informatics ITS 2

Page 3: Oop inheritance

02/05/2023Hadziq Fabroyir - Informatics ITS 3

IntroductionTitle: Inheritance

Class: Master Computer Science (MCS)

Subject: OOP using C++

Present by Zubair Farooq

Date 20 August, 2015

Page 4: Oop inheritance

02/05/2023Hadziq Fabroyir - Informatics ITS 4

Objectives• Inheritance• Advantages of Inheritance• Why we use Inheritance?• FORMS OF INHERITANCE• Defining a Base Class• Defining a Derived Class• Visibility Modes• Inheritance Program example

Page 5: Oop inheritance

02/05/2023Zubair Farooq (MCS) 5

InheritanceInheritance allows a software developer

to derive a new class from an existing one.

Page 6: Oop inheritance

02/05/2023Zubair Farooq (MCS) 6

Inheritance

The existing class is called the parent, super, or base class.

The derived class is called a child or subclass.

Page 7: Oop inheritance

02/05/2023Zubair Farooq (MCS) 7

Inheritance

• The child inherits characteristics of the parent.• The child has special rights to the parents methods and data.•The child has its own unique behaviors and data.

Page 8: Oop inheritance

02/05/2023Zubair Farooq (MCS) 8

Inheritance

Inheritance relationships are often shown graphically in a class diagram with the arrow pointing to the

parent class.

Inheritance should create an is-a

relationship, meaning the child is a more

specific version of the parent.

Animal

Bird

Page 9: Oop inheritance

02/05/2023Zubair Farooq (MCS) 9

Advantages of Inheritance?

Sometimes we need to repeat the code or we need repeat the whole class properties. So It helps in various ways.

It saves memory space.

It saves time.

It increases reliability of the code.

It saves the developing and testing efforts.

Page 10: Oop inheritance

02/05/2023Zubair Farooq (MCS) 10

Why we use Inheritance?

To increase the reusability of the code and to make further usable for another classes. We use the concept of inheritance.

Page 11: Oop inheritance

02/05/2023Zubair Farooq (MCS) 11

FORMS OF INHERITANCE

Single Inheritance:It is the inheritance hierarchy wherein one derived class inherits from one base class.

Multiple Inheritance:It is the inheritance hierarchy wherein one derived class inherits from multiple base classes.

Hierarchical Inheritance:It is the inheritance hierarchy wherein multiple subclasses inherits from one base class.

Multilevel Inheritance:It is the inheritance hierarchy wherein subclass acts as a base class for other classes.

Hybrid Inheritance:The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

Page 12: Oop inheritance

02/05/2023Zubair Farooq (MCS) 12

FORMS OF INHERITANCE

C# and Java support only Single inheritance, meaning that a derived class can have only one parent class.

Page 13: Oop inheritance

02/05/2023Zubair Farooq (MCS) 13

Defining a Base ClassBase class has general features common to all the derived classes and derived class (apart from having all the features of the base class) adds specific features.

class Base-class

{

... ... ...

…….//Members of base class

};

Page 14: Oop inheritance

02/05/2023Zubair Farooq (MCS) 14

Defining a Derived Class

The general form of deriving a subclass from a base class is as follows

Class derived-class-name : visibility-mode base-class-name

{

…… //

…….// members of the derived class

};

The visibility-mode is optional.

This visibility mode specifies how the features of base class are visible to the derived class.

Page 15: Oop inheritance

02/05/2023Zubair Farooq (MCS) 15

Visibility ModePublic Inheritance

Protected Inheritance

Private Inheritance

Page 16: Oop inheritance

02/05/2023Zubair Farooq (MCS) 16

Public Inheritanceclass A : public B{ // Class A now inherits the members of Class B

// with no change in the “access specifier” for

} // the inherited members

Page 17: Oop inheritance

02/05/2023Zubair Farooq (MCS) 17

Protected Inheritance

class A : protected B{ // Class A now inherits the members of Class B

// with public members “promoted” to protected

} // but no other changes to the inherited members

Page 18: Oop inheritance

02/05/2023Zubair Farooq (MCS) 18

Private Inheritance

class A : private B{ // Class A now inherits the members of Class B

// with public and protected members

} // “promoted” to private

Page 19: Oop inheritance

02/05/2023Zubair Farooq (MCS) 19

Visibility Modes

Page 20: Oop inheritance

02/05/2023Zubair Farooq (MCS) 20

Execution of base class constructor

When both derived and base class contains constructors, the base constructor is executed first and then the constructor in the derived class is executed.

Page 21: Oop inheritance

02/05/2023Zubair Farooq (MCS) 21

Program Statement

Imagine a publishing company that markets both book and audiocassette versions of its works.

Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display its data. Write a main() program to test the book and tape classes by creating instances of them, asking the user to fill in data with getdata(), and then displaying the data with putdata().

Reference: Object-Oriented Programming in C++ (4th Edition) by Robert Lafore(CH-9 Ex Question #1.)

Page 22: Oop inheritance

02/05/2023Zubair Farooq (MCS) 22

Base Classclass publication{ char title[20]; float price; public: void getdata( ){ cout<<"Please enter the title and price of Book : "; cin>>title>>price; } void putdata( ){ cout<<"Book Title : "<<title<<endl; cout<<"Price : "<<price<<endl; } };

Page 23: Oop inheritance

02/05/2023Zubair Farooq (MCS) 23

SubClassclass book : publication{ int pages; public: void getdata(){ publication::getdata(); cout<<"ENter the number of pages : "; cin>>pages; } Void putdata () { publication::putdata(); cout<<"Pages : "<<pages<<endl; } };

Page 24: Oop inheritance

02/05/2023Zubair Farooq (MCS) 24

SubClassclass tape : publication{ float time; public: void getdata(){ publication::getdata(); cout<<"Enter time play in minute : "; cin>>time; } void putdata(){ publication::putdata(); cout<<"Playing time : "<<time; } };

Page 25: Oop inheritance

02/05/2023Zubair Farooq (MCS) 25

Mian() Functionint main(void) { clrscr(); book b; tape t; b.getdata(); t.getdata(); b.putdata(); t.putdata(); getch(); return 0; }

Page 26: Oop inheritance

02/05/2023Zubair Farooq (MCS) 26

Feel Free to ask any Questions.