Docsity

Inheritance-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Inheritance-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 8

Inheritance

Inheritance

Function A

Base Class Local Data

Function B

Local Data

Function A

Derived Class (^) Derived Class Local Data

Usman Younis

Function A Function B Function C, D

Function A

Local Data

Function B Function E

Function B Function F

Derived Class

Inheritance

„ A tool for reusability, such that new classes ,called derived classes, are created fromcalled derived classes are created from the existing ones, base classes „ Derived classes inherit the capabilities of their base classes, and can add their own „ Terminology

Usman Younis

‰ Base class (super-class) ‰ Derived class (sub-class)

Inheritance - Advantages

„ Reusability: ‰ H lHelps the code to be reused th d t b d ‰ Base classes are written and tested once, sort of standardized ‰ Derived classes inherit the attributes and behavior of the base class, and add their own specialties

Usman Younis

specialties ‰ No limit on the number of derived classes

Inheritance (contd..)

„ All these properties can then be inherited by a specific character „ With required tweaks, it could be formed into a specialized character „ Finally, you can have as many objects of this character in your game, as you want

Usman Younis

Inheritance (contd..)

Usman Younis

Soldiers in CoD

Zombies in PoP

Inheritance

Specialization Vs. Generalization

CCar

Performance

Luxury Sports

ralization

Specializati

Usman Younis

Performance

Toyota Honda

Phantom Porche

Gener

on

Syntax

Base class

Derived class class derivedClass: public baseClass class baseClass { private: //private data of the base protected: //protected data of the base

{ //Derived class inherits all the //data of base class (except //private) private: //private data of the derived protected:

Usman Younis

public: //pubic data of the base };

protected: //protected data of the derived public: //pubic data of the derived };

Public and Private Derivation

private

When you don’t class A supply an access specifier, private is protected public

private private

class B : public A class c : private A

assumed

Usman Younis

protected public

protected public Object B (^) Object C

Object A

PUBLIC INHERITANCE

„ Public inheritance means that: ‰ public members of the base class become publicbli b f th b l b bli members of the derived class; ‰ protected members of the base class become protected members of the derived class; and ‰ private members of the base class are inaccessible within the derived class

Usman Younis

inaccessible within the derived class.

class Base { }; class Derived : public Base { };

PROTECTED INHERITANCE

„ Protected inheritance means that: ‰ public members of the base class becomebli b f th b l b protected members of the derived class; ‰ protected members of the base class become protected members of the derived class; and ‰ private members of the base class are inaccessible within the derived class

Usman Younis

inaccessible within the derived class.

class Base { }; class Derived : protected Base { };

PRIVATE INHERITANCE

„ Private inheritance means that: ‰ public members of the base class become privatebli b f th b l b i t members of the derived class; ‰ protected members of the base class become private members of the derived class; and ‰ private members of the base class are inaccessible within the derived class

Usman Younis

inaccessible within the derived class.

class Base { }; class Derived : private Base { };