Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
C++ Inheritance Systems Programming.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Inheritance using Java
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
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.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter -6 Polymorphism
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
1 Chapter 4: Another way to define a class Inheritance..!!
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
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.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 9 – Object-Oriented Programming: Inheritance
Polymorphism & Virtual Functions
This technique is Called “Divide and Conquer”.
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming (OOP) Lecture No. 22
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Introduction to Classes and Objects
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

Inheritance Inheritance-2

Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<<"point = [" <<x<<","<<y<<"]"; }

Inheritance class Circle : public Point { double radius; public: Circle(int,int,double ); void display(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<<" and radius = "<<radius; } Base-class initializer syntax passes arguments to base class Point.

Inheritance void main(void) { Circle c(3,4,5); c.display(); }

Inheritance Three-Level Inheritance Hierarchy  Three level point/circle/cylinder hierarchy  Point x-y coordinate pair  Circle x-y coordinate pair Radius  Cylinder x-y coordinate pair Radius Height

Inheritance class Point { protected: int x,y; public: Point(int,int ); void display(void); }; Point::Point(int a,int b) { x=a; y=b; } void Point::display(void) { cout<<"point = [" <<x<<","<<y<<"]"; }

Inheritance class Circle : public Point { protected: double radius; public: Circle(int, int, double); void display(void); double GetArea(void); }; Circle::Circle(int a,int b,double c):Point(a,b) { radius = c; } void Circle::display(void) { Point::display(); cout<<" radius = "<<radius; } double Circle::GetArea(void) { return 3.14 * radius * radius; }

Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { height=h; } double Cylinder::GetVolume(void) { return GetArea() * height; } void Cylinder::display(void) { Circle::display(); cout<<" height = "<<height; }

Inheritance void main(void) { Cylinder c(3,4,2.5,3.7); c.display(); cout<<"\nVolume of cylinder is : << c.GetVolume(); } Output: point=[3,4] radius = 2.5 height = 3.7 Volume of cylinder is : 72.61

Inheritance Order of execution of Constructors/Destructors  Instantiating derived-class object  Chain of constructor calls Base of inheritance hierarchy –Last constructor called in chain –First constructor body to finish executing –Example: Point / Circle / Cylinder hierarchy »Point constructor called last »Point constructor body finishes execution first Initializing data members –Each base-class constructor initializes data members »Inherited by derived class

Inheritance Order of execution of Constructors/Destructors  Destroying derived-class object  Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next –Continue up hierarchy until final base reached »After final base-class destructor, object removed from memory  Base-class constructors, destructors, assignment operators  Not inherited by derived classes  Derived class constructors, assignment operators can call Constructors Assignment operators

Inheritance Example class Point { protected: int x,y; public: Point(int,int ); void display(void); ~Point() { cout<<"\nPoint Class Destructor\n"; } }; Point::Point(int a,int b) { cout<<"\nPoint Class Constructor\n"; x=a; y=b; } void Point::display(void) { cout<<"point = [" <<x<<","<<y<<"]"; }

Inheritance class Circle : public Point { protected: double radius; public: Circle(int,int,double); void display(void); ~Circle() { cout<<"\nCircle Class Destructor\n"; } }; Circle::Circle(int a,int b,double c):Point(a,b) { cout<<"\nCircle Class Constructor\n"; radius = c; } void Circle::display(void) { Point::display(); cout<<" radius = "<<radius; }

Inheritance class Cylinder:public Circle { double height; public: Cylinder(int,int,double,double); void display(void); double GetVolume(void); ~Cylinder() { cout<<"\nCylinder Class Destructor\n"; } }; Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) { cout<<"\nCylinder Class Constructor\n"; height=h; } double Cylinder::GetVolume(void) { return 3.14 * radius * radius * radius; } void Cylinder::display(void) { Circle::display(); cout<<" height = "<<height; }

Inheritance void main(void) { Cylinder c(3,4,2.5,3.7); } Output: Point Class Constructor Circle Class Constructor Cylinder Class Constructor Cylinder Class Destructor Circle Class Destructor Point Class Destructor

Inheritance Overriding  The function in the derived class with the same function (variable) name will override the functions (variables) in the base classes.  But you can still invoke or get the overrode functions(variables) with scope resolution operator ”::”.  We have used overriding in previous examples…  display function of Point, Circle and Cylinder

Inheritance Types of inheritance  public  private  protected

Inheritance Public Inheritance  With public inheritance, public and protected members of the base class become respectively public and protected members of the derived class.

Inheritance Protected Inheritance  Public and protected members of the base class become protected members of the derived class.

Inheritance Private Inheritance  With private inheritance, public and protected members of the base class become private members of the derived class.

Inheritance public, protected and private Inheritance