













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to inheritance in c++, focusing on the use of member functions in derived classes. Topics covered include public, protected, and private inheritance, base and derived classes, and the implementation of protected members. The document also discusses the concept of encapsulation and its implications for derived classes. Several code examples are provided to illustrate the concepts.
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!
19.1 Introduction
19.2 Base and Derived Classes
19.2 Base and Derived Classes (II)
19.4 Casting Base Class Pointers to Derived Class Pointers
19.4 Casting Base-Class Pointers to Derived-Class Pointers (II)
1.1 Function definitions ---------------------
1. Circle class definition 34 y = b; 35 } 36 37 // Output Point (with overloaded stream insertion operator) 38 ostream &operator<<( ostream &output, const Point &p ) 39 { 40 output << '[' << p.x << ", " << p.y << ']'; 41 42 return output; // enables cascaded calls 43 } 44 // Fig. 19.4: circle.h 45 // Definition of class Circle 46 #ifndef CIRCLE_H 47 #define CIRCLE_H 48 49 #include
1.1 Initialize objects 1.2 Assign objects
2. Function calls 134 cout << "\nCircle c (via *circlePtr):\n" << *circlePtr 135 << "\nArea of c (via circlePtr): " 136 << circlePtr->area() << '\n'; 137 138 // DANGEROUS: Treat a Point as a Circle 139 pointPtr = &p; // assign address of Point to pointPtr 140 141 // cast base-class pointer to derived-class pointer 142 circlePtr = static_cast< Circle * >( pointPtr ); 143 cout << "\nPoint p (via *circlePtr):\n" << *circlePtr 144 << "\nArea of object circlePtr points to: " 145 << circlePtr->area() << endl; 146 return 0; 147 } 122 Circle *circlePtr = 0, c( 2.7, 120, 89 ); 123 124 cout << "Point p: " << p << "\nCircle c: " << c << '\n'; 125 126 // Treat a Circle as a Point (see only the base class part) 127 pointPtr = &c; // assign address of Circle to pointPtr 128 cout << "\nCircle c (via *pointPtr): " 129 << *pointPtr << '\n'; 130 131 // Treat a Circle as a Circle (with some casting) 132 // cast base-class pointer to derived-class pointer 133 circlePtr = static_cast< Circle * >( pointPtr );
Program Output Point p: [30, 50] Circle c: Center = [120, 89]; Radius = 2. Circle c (via *pointPtr): [120, 89] Circle c (via *circlePtr): Center = [120, 89]; Radius = 2. Area of c (via circlePtr): 22. Point p (via *circlePtr): Center = [30, 50]; Radius = 0. Area of object circlePtr points to: 0.
19.6 Overriding Base-Class Members in a Derived Class
1. Employee **class definition
1. Load header 1.1 Function definitions 64 double wage; // wage per hour 65 double hours; // hours worked for week 66 }; 67 68 #endif 69 // Fig. 19.5: hourly.cpp 70 // Member function definitions for class HourlyWorker 71 #include
1.1 Function Definitions --------------------
**1. Load header 1.1 Initialize object
97 // Print the HourlyWorker's name and pay 98 void HourlyWorker::print() const 99 { 100 cout << "HourlyWorker::print() is executing\n\n"; 101 Employee::print(); // call base-class print function 102 103 cout << " is an hourly worker with pay of $" 104 << setiosflags( ios::fixed | ios::showpoint ) 105 << setprecision( 2 ) << getPay() << endl; 106 } 107 // Fig. 19.5: fig19_05.cpp 108 // Overriding a base-class member function in a 109 // derived class. 110 #include "hourly.h" 111 112 int main() 113 { 114 HourlyWorker h( "Bob", "Smith", 40.0, 10.00 ); 115 h.print(); 116 return 0; 117 } HourlyWorker::print() is executing