1 / 26

Inheritance in C++: Creating New Classes, Base Classes, Derived Classes

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.

johnbrehm
Download Presentation

Inheritance in C++: Creating New Classes, Base Classes, Derived Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Lecture 7 Thursday, 21 July 2005

  2. Chapter 9 Inheritance • Creating new classes from the existing classes • The notions of base classes and derived classes

  3. 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.

  4. Class Hierarchy Shape 2D-shape 3D-shape Cube Circle Triangle Sphere Quadrilateral Square

  5. 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.

  6. 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

  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 • 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

  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 • 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

  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 • 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 • 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

  16. Member Type and Inheritance Type class A : public (or protected or private) B { public: - - - protected: - - - private: }

  17. Inheritance Type See also Fig. 9.30

  18. Multiple Inheritance base1 base2 Derived-Class

  19. Chapter 10 Virtual Functions and Polymorphism

  20. 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.

  21. 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.

  22. 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

  23. 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.

  24. 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

  25. 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

  26. Questions • What are virtual functions? • Distinguish between static and dynamic binding. • Distinguish between virtual functions and pure virtual functions.

More Related