1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance and Composition (aka containment) Textbook Chapter –
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Another Way to Define A Class - Inheritance. 2 Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void.
1 Chapter 6 Object-Oriented Software Development.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Inheritence ( הורשה Inheritence ( הורשה ( השקפים מבוססים על שקפים של אליהו ברוטמן ומרינה.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
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.
Chapter 4 Inheritance.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CMSC 202 Inheritance.
1 Chapter 14 Object-Oriented Software Development.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.
1 Lecture OOP Course Inheritance Basics Shape.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Inheritance One of the most powerful features of C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
An Equal Opportunity University CS Fall 2014 Lecture 11 Class Review, Inheritance, Polymorphism Ismail Abumuhfouz Slide modified from Link 1 Link.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Abstract Classes and Interfaces 8. Building Applications Using C#/ Session 8 © Aptech Ltd. Objectives  Define and describe abstract classes  Explain.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 12: Inheritance and Composition.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
Inheritance and Access Control CS 162 (Summer 2009)
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
10/18/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #02:
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
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.
Classes, Interfaces and Packages
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
1 Chapter 4: Another way to define a class Inheritance..!!
1.  Inheritance Inheritance  Define a Class Hierarchy Define a Class Hierarchy  Visibility Mode Visibility Mode  Access Rights of Derived Classes.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real- world.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
Inheritance Saras M. Srivastava LEARN BY EXAMPLES PGT – Comp. Sc.
Inheritance CMSC 202, Version 4/02.
Polymorphism.
Inheritance and Run time Polymorphism
Review: Two Programming Paradigms
Polymorphism & Virtual Functions
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Programming Techniques Course
Characteristics of OOPL Templates Standard Template Library
COP 3330 Object-oriented Programming in C++
Chapter 14 Object-Oriented Software Development
Another way to define a class
Inheritance in C++ Inheritance Protected Section
Lecture 6: Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept Rectangle Triangle Polygon class Polygon{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); }; class Triangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); };

2 Rectangle Triangle Polygon class Polygon{ protected: int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); }; class Rectangle : public Polygon{ public: float area(); }; class Rectangle{ protected: int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept

3 Rectangle Triangle Polygon class Polygon{ protected: int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); }; class Triangle : public Polygon{ public: float area(); }; class Triangle{ protected: int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept

4 Point Circle3D-Point class Point{ protected: int x, y; public: void set (int a, int b); }; class Circle : public Point{ private: double r; }; class 3D-Point: public Point{ private: int z; }; xyxy xyrxyr xyzxyz

5 Augmenting the original class Specializing the original class Inheritance Concept RealNumber ComplexNumber ImaginaryNumber Rectangle Triangle Polygon Point Circle real imag real imag 3D-Point

6 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining new class types to be a –specialization –augmentation of existing types

7 Define a Class Hierarchy Syntax: class DerivedClassName : access-level BaseClassName where –access-level specifies the type of derivation private by default, or public Any class can serve as a base class –Thus a derived class can also be a base class

8 Class Derivation Point 3D-Point class Point{ protected: int x, y; public: void set (int a, int b); }; class 3D-Point : public Point{ private: double z; … }; class Sphere : public 3D-Point{ private: double r; … }; Sphere Point is the base class of 3D-Point, while 3D-Point is the base class of Sphere

9 What to inherit? In principle, every member of a base class is inherited by a derived class – just with different access permission

10 Access Control Over the Members Two levels of access control over class members –class definition –inheritance type class Point{ protected: int x, y; public: void set(int a, int b); }; class Circle : public Point{ … };

11 The type of inheritance defines the access level for the members of derived class that are inherited from the base class Access Rights of Derived Classes privateprotectedpublic private--- protectedprivateprotected publicprivateprotectedpublic Type of Inheritance Access Controlfor Members

12 class daughter : mother{ private: double dPriv; public: void mFoo ( ); }; Class Derivation class mother{ protected: int mProc; public: int mPubl; private: int mPriv; }; class daughter : mother{ private: double dPriv; public: void dFoo ( ); }; void daughter :: dFoo ( ){ mPriv = 10; //error mProc = 20; }; private/protected/public int main() { /*….*/ } class grandDaughter : public daughter { private: double gPriv; public: void gFoo ( ); };

13 What to inherit? In principle, every member of a base class is inherited by a derived class – just with different access permission However, there are exceptions for –constructor and destructor –operator=() member –friends Since all these functions are class-specific

14 Constructor Rules for Derived Classes The default constructor and the destructor of the base class are always called when a new object of a derived class is created or destroyed. class A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} }; class B : public A { public: B (int a) {cout<<“B”<<endl;} }; B test(1); A:default B output:

15 Constructor Rules for Derived Classes You can also specify an constructor of the base class other than the default constructor class A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} }; class C : public A { public: C (int a) : A(a) {cout<<“C”<<endl;} }; C test(1); A:parameter C output: DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args ) { DerivedClass constructor body }

16 Define its Own Members Point Circle class Point{ protected: int x, y; public: void set(int a, int b); }; class Circle : public Point{ private: double r; public: void set_r(double c); }; xyxy xyrxyr class Circle{ protected: int x, y; private: double r; public: void set(int a, int b); void set_r(double c); }; The derived class can also define its own members, in addition to the members inherited from the base class

17 Even more … A derived class can override methods defined in its parent class. With overriding, –the method in the subclass has the identical signature to the method in the base class. –a subclass implements its own version of a base class method. class A { protected: int x, y; public: void print () {cout<<“From A”<<endl;} }; class B : public A { public: void print () {cout<<“From B”<<endl;} };

18 class Point{ protected: int x, y; public: void set(int a, int b) {x=a; y=b;} void foo (); void print(); }; class Circle : public Point{ private: double r; public: void set (int a, int b, double c) { Point :: set(a, b); //same name function call r = c; } void print(); }; Access a Method Circle C; C.set(10,10,100); // from class Circle C.foo (); // from base class Point C.print(); // from class Circle Point A; A.set(30,50); // from base class Point A.print(); // from base class Point

19 Putting Them Together Time is the base class ExtTime is the derived class with public inheritance The derived class can –inherit all members from the base class, except the constructor –access all public and protected members of the base class –define its private data member –provide its own constructor –define its public member functions –override functions inherited from the base class ExtTimeTime

20 class Time Specification class Time{ public : void Set ( int h, int m, int s ) ; void Increment ( ) ; void Write ( ) const ; Time ( int initH, int initM, int initS ) ; // constructor Time ( ) ; // default constructor protected : int hrs ; int mins ; int secs ; } ; // SPECIFICATION FILE ( time.h)

21 Class Interface Diagram Protected data: hrs mins secs Set Increment Write Time Time class

22 Derived Class ExtTime // SPECIFICATION FILE ( exttime.h) #include “time.h” enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ; class ExtTime : public Time // Time is the base class and use public inheritance { public : void Set ( int h, int m, int s, ZoneType timeZone ) ; void Write ( ) const; //overridden ExtTime (int initH, int initM, int initS, ZoneType initZone ) ; ExtTime (); // default constructor private : ZoneType zone ; // added data member } ;

23 Class Interface Diagram Protected data: hrs mins secs ExtTime class Set Increment Write Time Set Increment Write ExtTime Private data: zone

24 Implementation of ExtTime Default Constructor ExtTime :: ExtTime ( ) { zone = EST ; } The default constructor of base class, Time(), is automatically called, when an ExtTime object is created. ExtTime et1; hrs = 0 mins = 0 secs = 0 zone = EST et1

25 Implementation of ExtTime Another Constructor ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType initZone) : Time (initH, initM, initS) // constructor initializer { zone = initZone ; } ExtTime *et2 = new ExtTime(8,30,0,EST); hrs = 8 mins = 30 secs = 0 zone = EST et ???

26 Implementation of ExtTime void ExtTime :: Set (int h, int m, int s, ZoneType timeZone) { Time :: Set (hours, minutes, seconds); // same name function call zone = timeZone ; } void ExtTime :: Write ( ) const // function overriding { string zoneString[8] = {“EST”, “CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”} ; Time :: Write ( ) ; cout <<‘ ‘<<zoneString[zone]<<endl; }

27 Working with ExtTime #include “exttime.h” … int main() { ExtTime thisTime ( 8, 35, 0, PST ) ; ExtTime thatTime ; // default constructor called thatTime.Write( ) ; // outputs 00:00:00 EST thatTime.Set (16, 49, 23, CDT) ; thatTime.Write( ) ; // outputs 16:49:23 CDT thisTime.Increment ( ) ; thisTime.Write ( ) ; // outputs 08:35:02 PST }

28 Take Home Message Inheritance is a mechanism for defining new class types to be a specialization or an augmentation of existing types. In principle, every member of a base class is inherited by a derived class with different access permissions, except for the constructors