Inheritance: hiding methods

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Object-Oriented Programming in C++ Lecture 7 Polymorphism.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Overview of C++ Polymorphism
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
CISC181 Introduction to Computer Science Dr
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism Lec
CS1201: Programming Language 2
Virtual Functions Department of CSE, BUET Chapter 10.
Object Oriented Programming (OOP) Lecture No. 9
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Constructors and Destructors
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Object-Oriented Programming (OOP) Lecture No. 25
Jeff West - Quiz Section 12
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Inheritance: hiding methods A derived class may use the base class’s member functions. A derived class may also redefine the base class’s member functions. In that case, the derived class’s definition hides the one in the base class.

Inheritance : hiding methods Example 1: class Base { public: Base() { } void print() {cout << "Base::print()\n";} }; class Derived : public Base { Derived() { } int main () { Derived obj; obj.print(); return 0; } Output: Base::print()

Inheritance : hiding methods Example 2: class Base { public: Base() { } void print() {cout << "Base::print()\n";} }; class Derived : public Base { Derived() { } void print() {cout << “Derived::print()\n";} int main () { Derived obj; obj.print(); return 0; } Output: Derived::print()

Inheritance : hiding methods Example 2: class Base { public: Base() { } void print() {cout << "Base::print()\n";} void print(int x) {cout << "Base::print(x)\n";} }; class Derived : public Base { Derived() { } void print() {cout << “Derived::print()\n";} int main () { Derived obj; obj.print(10); return 0; } print() is overloaded This definition hides BOTH Base::print methods. COMPILER ERROR! Base::print(x) is hidden by Derived::print()

Inheritance : hiding methods Example 2: class Base { public: Base() { } void print() {cout << "Base::print()\n";} void print(int x) {cout << "Base::print(x)\n";} }; class Derived : public Base { Derived() { } void print() {cout << “Derived::print()\n";} void print(int x) {cout << "Derived::print(x)\n";} int main () { Derived obj; obj.print(10); return 0; } Output: Derived::print(x)

Virtual functions Recall the class hierarchy: We wish to create an array of pointers to various subtypes of Vehicles. In addition, each member of the array should behave according to each subclass. This can be achieved if every Vehicle member function that is overriden in the subclasses is declared as virtual. Vehicle Truck Train Bicycle

Virtual functions Output: Example 1 fleet[0] is of type Vehicle! class Vehicle { public: Vehicle() { } void print() { cout << “Vehicle\n”;} }; class Train : public Vehicle { Train() { } void print() { cout << “Train\n”;} } class Truck : public Vehicle { Truck() { } void print() { cout << “Truck\n”;} int main () { Vehicle *fleet[2]; fleet[0] = new Train; fleet[0]->print(); fleet[1] = new Truck; fleet[1]->print(); delete fleet[0]; delete fleet[1]; return 0; } fleet[0] is of type Vehicle! Output: Vehicle

Virtual functions Output: Example 2 Now, the compiler will check class Vehicle { public: Vehicle() { } virtual void print() { cout << “Vehicle\n”;} }; class Train : public Vehicle { Train() { } virtual void print() { cout << “Train\n”;} } class Truck : public Vehicle { Truck() { } virtual void print() { cout << “Truck\n”;} int main () { Vehicle *fleet[2]; fleet[0] = new Train; fleet[0]->print(); fleet[1] = new Truck; fleet[1]->print(); delete fleet[0]; delete fleet[1]; return 0; } Now, the compiler will check the dynamic type of fleet[i] and call the appropriate method. Output: Train Truck

Virtual functions If a class has virtual methods, then the destructor must also be virtual! class Vehicle { public: Vehicle() { } ~Vehicle { } virtual void print() {}; }; class Train : public Vehicle { Train() { } ~Train { } } int main () { Vehicle *t = new Train; delete t; return 0; The sequence of constructor/destructor calls in this program is: Vehicle() Train() ~Vehicle() The Train destructor was never called, because it's not virtual, so the compiler called only the destructor for Vehicle, since this is the type of *t.

Virtual functions If a class has virtual methods, then the destructor must also be virtual! class Vehicle { public: Vehicle() { } virtual ~Vehicle { } virtual void print() {}; }; class Train : public Vehicle { Train() { } virtual ~Train { } } int main () { Vehicle *t = new Train; delete t; return 0; Now, the sequence of constructor/destructor calls is correct: Vehicle() Train() ~Train() ~Vehicle()

Multiple inheritance A class may inherit from more than one base class. JetCar inherits the members of Car and those of Jet. JetCar's constructor calls the constructors of Car and Jetin the order specified in JetCar's definition (i.e. as they appear after the colon) class Car { ... }; class Jet { ... }; class JetCar: public Car, public Jet{ ... };

Multiple inheritance What if we have: class Vehicle { ... }; JetCar's constructor calls the constructor of Car which calls the constructor of Vehicle and then calls the constructor of Jet which calls the constructor of Vehicle The Vehicle constructor was called twice! class Vehicle { ... }; class Car : public Vehicle { ... }; class Jet : public Vehicle { ... }; class JetCar: public Car, public Jet { ... };

Multiple inheritance Use virtual inheritance to tell the compiler that the base class constructor should be called only once: JetCar's constructor calls the constructor of Car which calls the constructor of Vehicle and then calls the constructor of Jet class Vehicle { ... }; class Car : virtual public Vehicle { ... }; class Jet : virtual public Vehicle { ... }; class JetCar: public Car, public Jet { ... };

Examples Most of the examples we did in class can be found in ~b11/labs/inheritance