Top Banner
CS 2133: Data Structures OOP, Virtual Functions and Inheritance
25

CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Jan 03, 2016

Download

Documents

Julius Phelps
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

CS 2133: Data Structures

OOP, Virtual Functions and Inheritance

Page 2: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Inheritance

C++ Classes provide a way to enforce the walls of data abstraction by encapsulating the data and operations.

Inheritance and polymorphism allow you to derive new classes from existing classes. Software reusability is the main reason we do this. Life

Animals Plants

Snakes Dogs Rats Grass Orchid

Page 3: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Inheritance

Animal

Mammal

Horse

Vehicle Toy

SUV

Direct Inheritance Multiple Inheritance

Base

Represents derived classes

Represents base classes

Page 4: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Syntax of Inheritance

Class Vehicle{ // base class

public:

float mph;

float weight;

}

Class Car: public Vehicle { // derived class

public:

char brand_name[100];

}

Page 5: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Member Accessibility

A private member may be accessed only by methods within its class or by friends

A protected member may be accessed only by methods with its class hierarchy or friends

A public member is globally accessible

Page 6: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Public Inheritance

Each public member in the base class is public in the derived class

Each protected member in the base class is protected in the derived class

Each private member in the base class remains private in the base class and so is visible only in the base class

Page 7: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Private Inheritance

Each public member in the base class is private in the derived class

Each protected member in the base class is private in the derived class

Each private member in the base class remains private in the base class and so is visible in the base class

Page 8: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Protected Inheritance

Each public member in the base class is protected in the derived class

Each protected member in the base class is protected in the derived class

Each private member in the base class remains private in the base class and so is visible in the base class

Page 9: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Protected Example

class BC{// base classpublic: void set_x( int a ){x=a;}protected: int get_x() const{return x;}private: int x;};

class DC: public BC {public: void add(){ int c = get_x(); set_x( c+2); }};

int main(){

DC d;

d.set_x(3);

cout<<d.get_x<<‘\n’;

d.x =234;

d.add();

};

Page 10: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Example of Public Inheritance

Class B{// base classpublic: int x;protected:int w;private: int z;};class D: public B{public: int y; void set_w(int z) { w=a;} void base_w(B b) { b.w=0;}};

int main(){ D d; d.x = 33; d.y = 33; d.w = 77; d.z = 88;}

Page 11: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Further Discussion

Why is this bad? void base_w(B b) { b.w=0;} A derived class may access protected members that it

inherits from a base class. A derived class may access these protected members

precisely because, once inherited, they belong to the derived class

A derived class may not access protected members of a base class object, that is, an object that belongs to the base class but not to the derived class.

Page 12: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Notes about Inheritance

A private member is inherited but not visible in a derived class.

Except for a friend function, only methods of a class can access a private member of that class

A protected member is inherited and visible in a derived class.

In general protected data should be avoided. The data can be made private and made accessible by protected accessors.

For example, a class with private data members and protected accessors can be completely re-implemented without disturbing the interface.

Page 13: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Name Hiding

A member in a derived class of the same name as the base class hides it.

The scope resolution operator can be used if access to the base class member is required.

If a derived class adds a method with the same name in the base class, the added method hides the base class’s method

Page 14: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Name Hiding Example

class BC{//base class

public:

void h(float); //BC::h

};

class DC: public BC{ // derived class

public:

void h( char[ ]); // danger: hides BC::h

};

int main(){

DC d1;

d1.h(“Data Structures!”); // DC::h

d1.h(707.7); // ****ERROR: DC::h hides BC::h

d1.BC::h(707.7); // OK BC::h

}

Page 15: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Constructors under Inheritance

class Animal{protected:char species[MaxName+1];public: Animal() { strcpy(species, “ Animal”); } Animal(char* s) { strcpy(species, s);}};class Primate: public Animal{protected: heartcham;public: Primate(): Animal(“Primate”){} Primate(int n): Animal(”Primate”){heartcham=n;}}class Human: public Primate{public: Human(): Primate(){}; Human(int c):Primate(c){}};

executes firstAnimal::Animal()

Primate:Primate()

Human::Human()

Page 16: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Destructors under Inheritance

Constructors in an inheritance hierarchy fire in a a base class to derived class order

Destructors in an inheritance hierarchy fire in a derived class to a base class order

Constructors may not be virtual but destructors may be. Used when constructors for the base and derived class

dynamically allocated separate storage and the program dynamically allocates a class object

Page 17: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Constructors firing rulesassume D is derived from B

If D has constructors but B has no constructors, then the appropriate D constructor fires automatically

If D has no constructors but B has constructors, then B must have a default constructor so that B’s default constructor can fire whenever a D object is created.

If D has constructors and B has a default constructor, then B’s default constructor fires automatically unless another is explicitly invoked.

If D and B have constructors but B has no default constructor, then each D constructor must explicitly invoke, in its header, a B constructor.

Page 18: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

What is the output?

class B{

public:B( ) { cout << “B” << endl; }

};

class D1:public B{

public:D1( ):B( ) { cout << “D1” << endl; }

};

class D2: public D1( ){

public: D2( ): D1( ) {cout << “D2” << endl;

};

D2 d;Output:B D1 D2

Page 19: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Polymorphism & Virtual Methods

Polymorphism is the runtime binding of a pointer to a method

Non-virtual methods are bound at compile time

A method that needs to be bound at runtime is labeled Virtual.

Only methods may be virtual

Page 20: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Polymorphism

class Pet{

};

class Dog:public Pet{

public:virtual sound{cout<<“Bark”<<endl;}

};

class Cat:public Pet{

public:virtual sound{cout<<“Meow”<<endl;}

};

Pet

Dog Cat

Cat c; Dog d; Pet * ptr1, ptr2;

ptr1 = &c; ptr2=&d;

ptr1-> sound();

ptr2-> sound();

Page 21: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Inheritance and Virtual Methods

A pointer that points to a base class may also point to a subclass without casting.

The reverse direction is not allowed.

Once virtual always virtual.

Figure

Ellipse Rect

Circle

Figure * ptr

Page 22: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Pure Virtual Methods(Support for Programming Design)

An abstract class is a base class that is required to have a derived class. No object may be dreated for an abstract class

An abstract class is one that has a pure virtual method in its class declaration

Class Pet { //abstract class

public:

virtual void sound()=0;

}

Page 23: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Multiple Inheritance

class Win{ //base class...}

class ScrollWin: public Win{Widget horizontalSB;Widget verticalSB;...}

class PopupMenu{protected: int menuChoices; Win * menuSubWins;...}

class ScrollPopupMenu: public PopupMenu, public ScrollWin{...}

Win

ScrollWin PopupMenu

ScrollPopupMenu

Page 24: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Virtual Base Classes

class B{//base classprotected; int x;}

class D1: public virtual B{protected:...}

class D2: public virtual B{protected:...}

class Z: public D1,public D2{...};

Note that the variable x gets inherited from B along two different paths. The virtual base classes guarantees that only one copy of x arrives at Z

Page 25: CS 2133: Data Structures OOP, Virtual Functions and Inheritance.

Common Error(Slicing an Object)

In C++ it is legal to copy a derived-class object into a base class variable. For example, when a Manager object is assigned to a variable of type Employee, the result is only the employee portion of the manager data;

Manager m;

Employee e=m // holds onlyt the Employee data

The reverse assignment is not legal!