C++ Inheritance
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
Syntax for calling a different constructor of a base class
All members that were public in Mother would become protected in Daughter. This would not restrict Daughter from declaring its own public members.
What does inheritance look like in the case of: class Daughter: protected Mother;
its constructors and its destructor its assignment operator members (operator=) its friends its private members
What is inherited from a base class?
In daughter case, nothing specified: call default constructor In son case, constructor specified: call this specific constructor
What is the difference between Daughter (int a) and Son (int a) : Mother (a)
Mother: no parameters Daughter: int parameter Mother: int parameter Son: int parameter
What is the output of the following: // constructors and derived classes #include <iostream> using namespace std; class Mother { public: Mother () { cout << "Mother: no parameters\n"; } Mother (int a) { cout << "Mother: int parameter\n"; } }; class Daughter : public Mother { public: Daughter (int a) { cout << "Daughter: int parameter\n\n"; } }; class Son : public Mother { public: Son (int a) : Mother (a) { cout << "Son: int parameter\n\n"; } }; int main () { Daughter kelly(0); Son bud(0); return 0; }
20 10
What is the output of the following: // multiple inheritance #include <iostream> using namespace std; class Polygon { protected: int width, height; public: Polygon (int a, int b) : width(a), height(b) {} }; class Output { public: static void print (int i); }; void Output::print (int i) { cout << i << '\n'; } class Rectangle: public Polygon, public Output { public: Rectangle (int a, int b) : Polygon(a,b) {} int area () { return width*height; } }; class Triangle: public Polygon, public Output { public: Triangle (int a, int b) : Polygon(a,b) {} int area () { return width*height/2; } }; int main () { Rectangle rect (4,5); Triangle trgl (4,5); rect.print (rect.area()); Triangle::print (trgl.area()); return 0; }