Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS-2135 Object Oriented Programming
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
OOP Languages: Java vs C++
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Chapter 8 More Object Concepts
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance Version 1.1. Topics Inheritance Constructors and Inheritance Function Over-riding (Redefinition) The Stringstream class Shadowing Variables.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Inheritance One of the most powerful features of C++
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object-Oriented Programming in C++ More examples of Association.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
1 CSC241: Object Oriented Programming Lecture No 17.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance and Run time Polymorphism
Chapter 5 Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Presentation transcript:

Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted of just the three orthogonal dimensions. We might describe a Carton class which has the same properties as a Box object plus the additional property of its composite material. We might then specialize even further, by using the Carton definition to describe a class called FoodCarton – this will be a special kind of Carton which is designed to hold food.

Inheritance in Classes tMyn2 length breadth height length breadth height material length breadth height material contents class FoodCarton class Box class Carton Inherited members Inherited members More General More Specialized Each class has the properties of the class it is derived from, plus additional properties that differentiate it.

Inheritance in Classes tMyn3 The Carton class is an extension of the Box class – you might say that the Carton class is derived from the specification of the Box class. In a similar way, the FoodCarton class has been derived from the Cartoon class. By following this process, we develop a hierarchy of interrelated classes. In the hierarchy, one class is derived from another by adding extra properties – in other words, by specializing.

Inheritance in Classes tMyn4 Given a class A, suppose that we create a new, specialized class B. So the class A is called the base class and the B is called the derived class. The derived class automatically contains all of the data members of the base class, and (with some restrictions which we will discuss) all the function members. The derived class is said to inherit the data members and function members of the base class.

Inheritance in Classes tMyn5 If class B is a derived class defined directly in terms of class A, then we say that class A is a direct base class of B. We also say that B is derived from A. In the example above, the class Carton is a direct base class of FoodCarton. Because Carton is itself defined in terms of the class Box, we say that the class Box is an indirect base class of the class FoodCarton. An object of the FoodCarton class will have inherited members from Carton – including the members that the Carton class inherited from the Box class.

Inheritance in Classes tMyn6 In the diagram above, each class has all the properties of the Box class (on which it is based), and this illustrates precisely the mechanism of class inheritance in C++. The derived class has a complete set of data members and member functions from the base class, plus its own data members and member functions. Thus, each derived class object contains a complete base class sub-object, plus other members. When does inheritance take place? There are a number of simple tests that you can employ.

Inheritance in Classes tMyn7 The first is the is a kind of test: any derived class object is a kind of base class object. In other words, a derived class should describe a subset of the objects represented by the base class. For example: a class Dog might be derived from a class Animal. This makes sense because a dog is a kind of animal. The is a kind of test is an excellent first check, but it’s not infallible. For example, suppose that we defined a class, Bird, that (among other things) reflected the fact that most birds can fly…

Inheritance in Classes tMyn8 …but now an ostrich is a kind of bird, but it’s nonsense to derive a class Ostrich from the Bird class, because ostriches can’t fly!! If your classes pass the is a kind of test, then you should double check by asking the following question: Is there anything I can say about (or demand of) the base class that’s inapplicable to the derived class? If there is, then the derivation probably isn’t safe.

Inheritance in Classes tMyn9 If your classes fail the is a kind of test, then you almost certainly shouldn’t use class derivation. In this case, you could instead implement the has a test. A class object passes the has a test, if it contains an instance of another class. You can implement this situation by including an object of the second class as a data member of the first. This type of dependence is called aggregation.

Inheritance in Classes tMyn10 For example, consider a class Automobile. This class is likely to contain major automobile components as its class members. An automobile has an engine, has a transmission, has a chassis and has suspension. So it makes sense for the Automobile class to contain objects of type Engine, Transmission, Chassis and Suspension. But it is not true to say that an engine is a kind of automobile! Next example tries to demonstrate the order of constructor and destructor calls:

Inheritance in Classes tMyn11 #include "stdafx.h" #include using namespace System; using namespace std; class Box { public: Box(); ~Box(); void showBoxMembers(); protected: double length; double breadth; double* height; };

Inheritance in Classes tMyn12 Box::Box() { cout<<"Base class constructor called."<<endl <<"Creating now object "<<this<<endl; height=new double; cout<<"Input length: "; cin>>length; cin.get(); cout<<"Input breadth: "; cin>>breadth; cin.get(); cout<<"Input height: "; cin>>*height; cin.get(); }

Inheritance in Classes tMyn13 Box::~Box() { cout<<"Base class destructor called."<<endl <<"Deallocating dynamically allocated memory from object " <<this<<endl; delete height; height=0; } void Box::showBoxMembers() { cout<<"The dimensions of the base object:"<<endl <<"length: "<<length<<endl <<"breadth: "<<breadth<<endl <<"height: "<<*height<<endl; }

Inheritance in Classes tMyn14 class Carton:public Box { public: Carton(); ~Carton(); void showCartonMembers(); private: double* weight; }; Carton::Carton() { cout<<"Derived class constructor called."<<endl <<"Creating now object "<<this<<endl; weight= new double; cout<<"Input weight: "; cin>>*weight; cin.get(); } Carton is derived directly from Box The keyword public is the base class access specifier, and it indicates how the members of Box are to be accessed within the Carton class.

Inheritance in Classes tMyn15 Carton::~Carton() { cout<<"Derived class destructor called."<<endl <<"Deallocating dynamically allocated memory from object " <<this<<endl; delete weight; weight=0; } void Carton::showCartonMembers() { cout<<"The dimensions of the derived object:"<<endl <<"length: "<<length<<endl <<"breadth: "<<breadth<<endl <<"height: "<<*height<<endl <<"weight: "<<*weight<<endl; }

Inheritance in Classes tMyn16 int main(array ^args) { Box first=Box(); first.showBoxMembers(); Carton second=Carton(); second.showCartonMembers(); return 0; } First type automatic objects…..

Inheritance in Classes tMyn17

Inheritance in Classes tMyn18 int main(array ^args) { Box* third=new Box(); third->showBoxMembers(); delete third; Carton* fourth=new Carton(); fourth->showCartonMembers(); delete fourth; return 0; } Next dynamic objects…..

Inheritance in Classes tMyn19

Inheritance in Classes tMyn20 So as a summary: Inheritance is a means of specifying hierarchical relationships between types. What happens when a derived class object is created and destroyed: 1.Space is allocated (on the stack or the free store) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself). 2.The base class’s constructor is called to initialize the data members inherited from the base class.

Inheritance in Classes tMyn21 3.The derived class’s constructor is then called to initialize the data members added in the derived class. 4.The derived class object is then usable. 5.When the object is destroyed (goes out of scope or is deleted) the derived class’s destructor is called on the object first. 6.Then the base class’s destructor is called on the object. 7.Finally the allocated space for the full object is reclaimed.

Inheritance in Classes tMyn22 If we had the statement… first.length=10.9; …the program will no longer compile. Reason: The length data member of the Box object first was declared as a protected data member, and so it’s not accessible to this statement.

Inheritance in Classes tMyn23 If we had the statement… second.length=10.8; …the program will again no longer compile. This is perhaps a little unexpected – after all, when we defined the Carton class, didn’t we inherit the Box class members as public? The reason for the error is that length is a protected data member in the base class. In the derived class Carton, the length member is a publicly inherited protected data member.

Inheritance in Classes tMyn24 The compiler interprets this by making length a protected member of Carton. Access to the inherited members of a derived class object is determined by both the access specifier of the data member in the base class and the access specifier of the base class in the derived class.

Inheritance in Classes tMyn25 Let’s add a few more lines to the main(): Basically it is OK to use a pointer to base class to store the address of a derived class object, but so far… int main(array ^args) { Box* fifth=new Carton(); fifth->showBoxMembers(); delete fifth; return 0; }

Inheritance in Classes tMyn26 …If we do not make any additional changes to our program, then the output would be: The derived class destructor will never be called!!

Inheritance in Classes tMyn27 The problem occurs because the binding to the destructor is being set at compile time, and since we’re applying the delete operator to an object pointed to by a pointer to Box, the Box class destructor is called. To fix this, we need dynamic binding for the destructors.