Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Inheritance, Base, Class, Derived, Derived, Classes, Reusability, Program, Reliability, 3d, Game, Engine

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 8

Toggle sidebar

Related documents


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 - Advantages

„ Saves time and money ‰ SS pecialization is only required in the derivedi li ti i l i d i th d i d class coding ‰ Where as base classes have been already standardized with much efforts „ Helps better structuring your program

Usman Younis

„ Increased reliability

Inheritance (contd..)

„ Example: „ In a 3D game engine, the code for ah d f character is written once with all its attributes and behavior ‰ Character’s shape ‰ Its walk

Usman Younis

‰ Jump ‰ Crawling ‰ Etc..

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 };

Syntax (contd..) „ Colon in the declaration identifies that the class is inherited „ Colon is followed by a keyword (public in previous case) ‰ This identifies the access of the base class members using derived class’s objectderived class’s object ‰ E.g., void main() { derivedClass dObject; dObject.private_data; //Error dObject.public_data; //Okay d b bl d f b k

Usman Younis

dObject.public_data_of_base; //Okay

baseClass bObject; bObject.private_data; //Error bObject.public_data_of_base; //Okay }

Protected Access

„ So far, we have seen two access modes in C++C++ ‰ Public: „ Directly accessible by the users of a class ‰ Private: „ Not directly accessible by the users of a class, not even by the derived classes

Usman Younis

by the derived classes „ A third access mode ‰ Protected „ Directly accessible by the derived classes, but not by the users (objects)

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 { };