Download Inheritance and Constructors in C++: Point, Circle, and Cylinder and more Slides Computer Science in PDF only on Docsity!
Chapter 19 – Inheritance Part 2
Outline
19.8 Direct Base Classes and Indirect Base Classes
19.9 Using Constructors and Destructors in Derived Classes
19.10 Implicit Derived-Class Object to Base-Class Object
Conversion
19.11 Software Engineering with Inheritance
19.12 Composition vs. Inheritance
19.13 “Uses A” and “Knows A” Relationships
19.14 Case Study: Point, Circle, Cylinder
Docsity.com
19.8 Direct and Indirect Base Classes
- Direct base class
- Explicitly listed derived class’ header with the colon (:) notation when that derived class is declared.
- class HourlyWorker : public Employee
- Employee is a direct base class of HourlyWorker
- Indirect base class
- Inherited from two or more levels up the class hierarchy
- class MinuteWorker : public HourlyWorker
- Employee is an indirect base class of MinuteWorker
Docsity.com
19.9 Using Constructors and Destructors in Derived Classes (II)
- Derived-class constructor
- Calls the constructor for its base class first to initialize its base- class members
- If the derived-class constructor is omitted, its default constructor calls the base-class’ default constructor
- Destructors are called in the reverse order of constructor calls. - Derived-class destructor is called before its base-class destructor
Docsity.com
**1. Point definition
- Load header 1.1 Function definitions** 1 // Fig. 19.7: point2.h 2 // Definition of class Point 3 #ifndef POINT2_H 4 #define POINT2_H 5 6 class Point { 7 public: 8 Point( int = 0, int = 0 ); // default constructor 9 ~Point(); // destructor 10 protected: // accessible by derived classes 11 int x, y; // x and y coordinates of Point 12 }; 13 14 #endif 15 // Fig. 19.7: point2.cpp 16 // Member function definitions for class Point 17 #include 18 19 using std::cout; 20 using std::endl; 21 22 #include "point2.h" 23 24 // Constructor for class Point 25 Point::Point( int a, int b ) 26 { 27 x = a; 28 y = b; 29 30 cout << "Point constructor: " 31 << '[' << x << ", " << y << ']' << endl; 32 }
Docsity.com
1. Load header 1.1 Function Definitions 58 // Fig. 19.7: circle2.cpp 59 // Member function definitions for class Circle 60 #include 61 62 using std::cout; 63 using std::endl; 64 65 #include "circle2.h" 66 67 // Constructor for Circle calls constructor for Point 68 Circle::Circle( double r, int a, int b ) 69 : Point( a, b ) // call base-class constructor 70 { 71 radius = r; // should validate 72 cout << "Circle constructor: radius is " 73 << radius << " [" << x << ", " << y << ']' << endl; 74 } 75 76 // Destructor for class Circle 77 Circle::~Circle() 78 { 79 cout << "Circle destructor: radius is " 80 << radius << " [" << x << ", " << y << ']' << endl;
81 } Docsity.com
**1. Load headers 1.1 Initialize objects
- Objects enter and leave scope** 82 // Fig. 19.7: fig19_07.cpp 83 // Demonstrate when base-class and derived-class 84 // constructors and destructors are called. 85 #include 86 87 using std::cout; 88 using std::endl; 89 90 #include "point2.h" 91 #include "circle2.h" 92 93 int main() 94 { 95 // Show constructor and destructor calls for Point 96 { 97 Point p( 11, 22 ); 98 } 99 100 cout << endl; 101 Circle circle1( 4.5, 72, 29 ); 102 cout << endl; 103 Circle circle2( 10, 5, 5 ); 104 cout << endl; 105 return 0;
106 } Docsity.com
19.10 Implicit Derived-Class Object to Base-Class Object Conversion
- baseClassObject = derivedClassObject;
- This will work
- Remember, the derived class object has more members than the base class object
- Extra data is not given to the base class derivedClassObject = baseClassObject;
- May not work properly
- Unless an assignment operator is overloaded in the derived class, data members exclusive to the derived class will be unassigned
- Base class has less data members than the derived class
- Some data members missing in the derived class object
Docsity.com
19.10 Implicit Derived-Class Object to Base-Class Object Conversion (II)
- Four ways to mix base and derived class pointers and objects - Referring to a base-class object with a base-class pointer - Allowed - Referring to a derived-class object with a derived-class pointer - Allowed - Referring to a derived-class object with a base-class pointer. - Code can only refer to base-class members, or syntax error - Referring to a base-class object with a derived-class pointer - Syntax error - The derived-class pointer must first be cast to a base-class pointer
Docsity.com
9.12 Composition vs. Inheritance
- "is a" relationship
- "has a" relationship
- Composition - class has an object from another class as a data member Employee “is a” BirthDate; //Wrong! Employee “has a” Birthdate;//Composition
Docsity.com
9.13 “Uses A” And “Knows A” Relationships
- “uses a” relationship
- One object issues a function call to a member function of another object
- “ knows a ” relationship
- One object is aware of another
- Contains a pointer or handle to another object
- Also called an association
Docsity.com
1. Point definition 1.1 Function definitions 1 // Fig. 19.8: point2.h 2 // Definition of class Point 3 #ifndef POINT2_H 4 #define POINT2_H 5 6 #include 7 8 using std::ostream; 9 10 class Point { 11 friend ostream &operator<<( ostream &, const Point & ); 12 public: 13 Point( int = 0, int = 0 ); // default constructor 14 void setPoint( int, int ); // set coordinates 15 int getX() const { return x; } // get x coordinate 16 int getY() const { return y; } // get y coordinate 17 protected: // accessible to derived classes 18 int x, y; // coordinates of the point 19 }; 20 21 #endif 22 // Fig. 19.8: point2.cpp 23 // Member functions for class Point 24 #include "point2.h" 25 26 // Constructor for class Point 27 Point::Point( int a, int b ) { setPoint( a, b ); } 28 29 // Set the x and y coordinates 30 void Point::setPoint( int a, int b ) 31 { 32 x = a;
Docsity.com
1.1 Function definitions 33 y = b; 34 } 35 36 // Output the Point 37 ostream &operator<<( ostream &output, const Point &p ) 38 { 39 output << '[' << p.x << ", " << p.y << ']'; 40 41 return output; // enables cascading 42 }
Docsity.com
1.1 Function definitions
35 // Constructor for Circle calls constructor for Point 36 // with a member initializer and initializes radius 37 Circle::Circle( double r, int a, int b ) 38 : Point( a, b ) // call base-class constructor 39 { setRadius( r ); } 40 41 // Set radius 42 void Circle::setRadius( double r ) 43 { radius = ( r >= 0? r : 0 ); } 44 45 // Get radius 46 double Circle::getRadius() const { return radius; } 47 48 // Calculate area of Circle 49 double Circle::area() const 50 { return 3.14159 * radius * radius; } 51 52 // Output a circle in the form: 53 // Center = [x, y]; Radius = #.## 54 ostream &operator<<( ostream &output, const Circle &c ) 55 { 56 output << "Center = " << static_cast< Point > ( c ) 57 << "; Radius = " 58 << setiosflags( ios::fixed | ios::showpoint ) 59 << setprecision( 2 ) << c.radius; 60 61 return output; // enables cascaded calls 62 }
Docsity.com
1. Cylinder definition 1 // Fig. 19.10: cylindr2.h 2 // Definition of class Cylinder 3 #ifndef CYLINDR2_H 4 #define CYLINDR2_H 5 6 #include 7 8 using std::ostream; 9 10 #include "circle2.h" 11 12 class Cylinder : public Circle { 13 friend ostream &operator<<( ostream &, const Cylinder & ); 14 15 public: 16 // default constructor 17 Cylinder( double h = 0.0, double r = 0.0, 18 int x = 0, int y = 0 ); 19 20 void setHeight( double ); // set height 21 double getHeight() const; // return height 22 double area() const; // calculate and return area 23 double volume() const; // calculate and return volume 24 25 protected: 26 double height; // height of the Cylinder 27 }; 28 29 #endif
Docsity.com