260 likes | 272 Views
Learn about inheritance in C++ and how to create new classes by deriving from existing classes. Understand the concepts of base classes and derived classes, and how to access and modify their data and functions. Explore examples of inheritance, such as the Shape class hierarchy and the Point/Circle classes. Gain knowledge of virtual functions and polymorphism, and how they enable dynamic binding.
E N D
C++ Lecture 7 Thursday, 21 July 2005
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.
Class Hierarchy Shape 2D-shape 3D-shape Cube Circle Triangle Sphere Quadrilateral Square
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
Point/Circle Example class Point { … protected: int x, y; }; class Circle : public Point { … }
Class that Contains other Classes class Employee { public: … private: Date birthday; Date hireday; }
Classes that is Derived from Existing Classes class Point { …. } class Circle : public Point { … }
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 as Point A, B(1,0); A.setPoint(2,4); x = A.getX(); • but NOT xvalue = 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 See also Fig. 9.30
Multiple Inheritance base1 base2 Derived-Class
Chapter 10 Virtual Functions and Polymorphism
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 loop for(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 class virtual 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 • 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.
Class Inheritance and (Virtual) Functions Base class (Pure) Virtual functions declared in Shape Shape Point Derived classes Virtual functions maybe implemented in Point, Circle, or Cylinder Circle Cylinder
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
Questions • What are virtual functions? • Distinguish between static and dynamic binding. • Distinguish between virtual functions and pure virtual functions.