W08 Inheritance - Journal PDF

Title W08 Inheritance - Journal
Author Zohaib Malik
Course Computer Science
Institution Bahria University
Pages 18
File Size 902.6 KB
File Type PDF
Total Downloads 96
Total Views 136

Summary

Journal...


Description

Spring 2020

W08

Inheritance

Classification: Is A Relationship Base Classes and Derived Classes Protected Members Overriding Base-Class Members Types of Inheritance

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

1

Classification living creatures

mammal

whale

reptile

cat snake

W08-Inheritance 3

Inheritance: IS-A Relationship 

A cat is a mammal, a mammal is a living creature.



An apple is a fruit, fruit is a food.



A Toyota is a car, car is a vehicle.



A sphere is a 3D shape, a 3D shape is a shape.

W08-Inheritance 4

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

2

Inheritance Example

W08-Inheritance 5

Inheritance vs. Composition 

“Is A” Relationship 

Inheritance 



A class is derived from another class

“Has A” Relationship 

Composition 

A class contains other classes as members

W08-Inheritance 6

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

3

Inheritance Example

W08-Inheritance 7

Inheritance Derived class inherits data members and member functions from a previously defined base class.

W08-Inheritance 8

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

4

Multilevel Inheritance

Class Hierarchy

W08-Inheritance 9

Inheritance 

Single inheritance 



Class inherits from one base class

Multiple inheritance 

Class inherits from multiple base classes

W08-Inheritance 10

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

5

Single and Multiple Inheritance

W08-Inheritance 11

UML Representation of Inheritance

W08-Inheritance 12

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

6

protected Access 

Intermediate level of protection between public and private inheritance



Protected data members and functions are fully visible to derived classes, but are otherwise private.

W08-Inheritance 13

Accessing Class Members 



private:  only accessible within the class. protected: accessible within the base class and its derived classes. public:  Every object of the class outside the class definition has access to this member 



W08-Inheritance 14

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

7

Accessing Base Class Members Base class Derived class

private:

private: protected: protected:

public: public:

Derived object;

Base object;

W08-Inheritance 15

Declaring Inheritance class DerivedClass : access-level BaseClass

where 

access-level specifies the Type of Inheritance   

private by default, or public or protected (used very rarely)

W08-Inheritance

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

8

Declaring Inheritance class Programmer : public Employee { ... };   

Class Programmer inherits from class Employee friend functions not inherited private members of base class not accessible in the derived class

W08-Inheritance 17

Inheritance Concept Polygon

Rectangle

Triangle

class Rectangle{ private: int width, length; public: void set(int w, int l); int area(); };

class Polygon { private: int width, length; public: void set(int w, int l); };

class Triangle{ private: int width, length; public: void set(int w, int l); int area(); };

W08-Inheritance 18

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

9

Inheritance Concept class Polygon {

Polygon

protected: int width, length; public: void set(int w, int l);

Rectangle

Triangle

class Rectangle: public Polygon { public: int area(); };

}; class Rectangle{ protected: int width, length; public: void set(int w, int l); int area(); };

W08-Inheritance 19

Inheritance Concept class Polygon { protected:

Polygon

int width, length; public: Rectangle

void set(int w, int l); Triangle

class Triangle : public Polygon { public: int area(); }; W08-Inheritance

}; class Triangle{ protected: int width, length; public: void set(int w, int l); int area(); }; 20

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

10

Base and Derived Classes An object of a derived class is also an object of the base class. Base class

Derived classes

Student

GraduateStudent UndergraduateStudent

Shape

Circle Triangle Rectangle

Loan

CarLoan HomeImprovementLoan MortgageLoan

Employee

FacultyMember StaffMember

Account

CheckingAccount SavingsAccount

W08-Inheritance 21

Inheritance Concept x y

Point

Circle x y r

3D-Point x y z

class Circle : public Point { private: double r; };

class Point { protected: int x, y; public: void set(int a, int b); };

class 3D-Point: public Point { private: int z; };

W08-Inheritance 22

Object-Oriented Programming- Spring 2020 Instructor: Saima Jawad

11

Class Derivation Example parent

daughter

son

class daughter : public parent{ private: double a; public: void foo ( ); };

class parent{ protected: int x, y; public: void set(int a, int b); private: int z; };

void daughter :: foo ( ){ x = y = 20; set(5, 10); cout...


Similar Free PDFs