c++ lecture 7 thursday, 21 july 2005. chapter 9 inheritance l creating new classes from the existing...
Post on 31-Dec-2015
217 Views
Preview:
TRANSCRIPT
Chapter 9 Inheritance
Creating new classes from the existing classes
The notions of base classes and derived classes
Basic Idea of Inheritance
A base object A has some properties (data and functions).
An derived object B from A can have the same set of data or functions, plus some of its own.
Additionally, some of the data or functions may not be available for B, or some functions redefined.
Inheritance, example
A Point class has two coordinates (x,y), there are functions to get and set the x and y values.
A Circle class derived from Point is a Point plus the data radius, and additional functions of getting area, setting and getting radius.
Three Type of Members
public: can be accessed outside member function, including derived class
private: off limit except for member functions
protected: can not accessed outside member functions, except by derived class
Three Types of Class Members
public - accessible by member and non-member functions
private - accessible by class member functions and its friends
protected - accessible by member function, its friends, and derived class, but not non-member functions
Point Base Class
class Point { public: Point(int=0, int=0); void setPoint(int, int); int getX(); int getY(); protected: int x, y;};
Point Class
The Point class can be used asPoint A, B(1,0);A.setPoint(2,4);x = A.getX();
but NOTxvalue = A.x; // because x is protected
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;}
Circle Class
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)
Case Study, Point, Circle, Cylinder
A Point class contains x, y A Circle is a point with radius r A Cylinder is a Circle plus a height
C.f. Fig. 9.22-24
Member Type and Inheritance Type
class A : public (or protected or private) B {
public: - - - protected: - - - private:}
Inheritance Type
Base class member type
Public inheritance
Protected inheritance
Private inheritance
public No change Protected in derived class
Private in derived class
protected No change Protected in derived class
Private in derived class
private Hidden in derived class
Hidden in derived class
Hidden in derived class
See also Fig. 9.30
Basic Idea
Let the classes Square, Circle, etc be derived from the base class Shape.
We want to be able to draw all the shapes with
p->draw(); where p is a pointer of type Shape
class.
Draw a lot of Shapes
We might want to draw many different shapes by a loopfor(j = 0; j < jmax; ++j) {
p[j] -> draw();}
where p is array of pointers of base type Shape each pointing to a different class of concrete shape.
Virtual Functions
Declare a virtual function in the base classvirtual void draw() const;
Declare as “virtual” again in derived class if over-riding is needed.
Define the function draw() for each of the derived classes.
Dynamical binding
Pure Virtual Function
Declarevirtual function prototype = 0;
A pure virtual function is declared in the base class but not implemented. Implementations are given in specific derived class.
Class Inheritance and (Virtual) Functions
Shape
Point
Circle
Cylinder
(Pure) Virtual functions declared in Shape
Virtual functions maybe implemented in Point, Circle, or Cylinder
Base class
Derived classes
The Shape Example
shape.h (Fig. 10.13)
point.h, point.cpp (Fig.10.14-15)
circle.h, circle.cpp (Fig.10.16-17)
cylindr.h, cylindr.cpp (Fig.10.18-19)
Main program (Fig.10.20)
C.f. Fig. 10.13-20
top related