Download presentation
Presentation is loading. Please wait.
Published byLionel Paul Modified over 9 years ago
1
C++ Lecture 7 Thursday, 21 July 2005
2
Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived classes
3
Basic Idea of Inheritance l A base object A has some properties (data and functions). l An derived object B from A can have the same set of data or functions, plus some of its own. l Additionally, some of the data or functions may not be available for B, or some functions redefined.
4
Class Hierarchy Shape 2D-shape3D-shape Circle Quadrilateral Triangle Sphere Cube Square
5
Inheritance, example l A Point class has two coordinates (x,y), there are functions to get and set the x and y values. l A Circle class derived from Point is a Point plus the data radius, and additional functions of getting area, setting and getting radius.
6
Three Type of Members l public: can be accessed outside member function, including derived class l private: off limit except for member functions l protected: can not accessed outside member functions, except by derived class
7
Point/Circle Example class Point { … protected: int x, y; }; class Circle : public Point { … }
8
Class that Contains other Classes class Employee { public: … private: Date birthday; Date hireday; }
9
Classes that is Derived from Existing Classes class Point { …. } class Circle : public Point { … }
10
Three Types of Class Members l public - accessible by member and non-member functions l private - accessible by class member functions and its friends l protected - accessible by member function, its friends, and derived class, but not non- member functions
11
Point Base Class class Point { public: Point(int=0, int=0); void setPoint(int, int); int getX(); int getY(); protected: int x, y; };
12
Point Class l The Point class can be used as Point A, B(1,0); A.setPoint(2,4); x = A.getX(); l but NOT xvalue = A.x; // because x is protected
13
Circle Class Derived from Point Class class Circle : public Point { friend ostream &operator<< … public: Circle(double r=0.0, int x=0, int y=0); void setRadius(double); double getRadius() const; double area() const; protected: double radius; }
14
Circle Class l We can use the circle class in the following ways Circle c; c.setPoint(1,2); x = c.getX(); y = c.getY(); c.getRadius(1.2); a = c.area(); c.f. Fig. 9.4-6 (points); 9.7-9.9 (circle); 9.12-9.16 (point2,circle3)
15
Case Study, Point, Circle, Cylinder l A Point class contains x, y l A Circle is a point with radius r l A Cylinder is a Circle plus a height C.f. Fig. 9.22-24
16
Member Type and Inheritance Type class A : public (or protected or private) B { public: - - - protected: - - - private: }
17
Inheritance Type Base class member type Public inheritance Protected inheritance Private inheritance publicNo changeProtected in derived class Private in derived class protectedNo changeProtected in derived class Private in derived class privateHidden in derived class See also Fig. 9.30
18
Multiple Inheritance base1base2 Derived-Class
19
Chapter 10 Virtual Functions and Polymorphism
20
Basic Idea l Let the classes Square, Circle, etc be derived from the base class Shape. l We want to be able to draw all the shapes with p->draw(); l where p is a pointer of type Shape class.
21
Draw a lot of Shapes l We might want to draw many different shapes by a loop for(j = 0; j < jmax; ++j) { p[j] -> draw(); } l where p is array of pointers of base type Shape each pointing to a different class of concrete shape.
22
Virtual Functions l Declare a virtual function in the base class virtual void draw() const; l Declare as “virtual” again in derived class if over-riding is needed. l Define the function draw() for each of the derived classes. l Dynamical binding
23
Pure Virtual Function l Declare virtual function prototype = 0; A pure virtual function is declared in the base class but not implemented. Implementations are given in specific derived class.
24
Class Inheritance and (Virtual) Functions Shap e Point Circle Cylinder (Pure) Virtual functions declared in Shape Virtual functions maybe implemented in Point, Circle, or Cylinder Base class Derived classes
25
The Shape Example l shape.h (Fig. 10.13) l point.h, point.cpp (Fig.10.14-15) l circle.h, circle.cpp (Fig.10.16-17) l cylindr.h, cylindr.cpp (Fig.10.18-19) l Main program (Fig.10.20) C.f. Fig. 10.13-20
26
Questions l What are virtual functions? l Distinguish between static and dynamic binding. l Distinguish between virtual functions and pure virtual functions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.