I. questions and answers
1. If a function with the same name is defined in both the base class and the derived class, when the derived class object calls the function, the
Which function will be used?
Answer: the function defined in the derived class of the call.
2. There are the following definitions:
class MyBASE {
public:
void set(int n) { k = n; }
int get() const { return k; }
public:
int k;
};
class MyDERIVED :protected MyBASE {
};
Analysis of the above procedures shows that there are several protected members in the MyDERIVED class.
Answer: there are three ways to protect inheritance when inheriting the base class (using protected inheritance). This leads to that the public members inherited from the base class also become protected members, including inherited property k, set method and get regular member method.
3. In my own words, what does the derived class inherit from the base class and what can't it inherit?
Except for constructor, destructor, friend function, operator overloaded function, other members (public member, protected member, private member) can inherit.
4. If the derived class does not add any data members, does it need a constructor?
Yes, each class needs its own constructor. Even if no new members are added to the derived class, the derived class will first call the constructor of the base class when creating the object, and call its own constructor even if the constructor is empty.
5. Suppose Corporation is the base class and PublicCorporation is the derived class. Assuming that both classes
The head() function is defined, ph is a pointer to the type Corporation, and is assigned to a
Address of the PublicCorporation object. If the base class defines head() as:
a. conventional non virtual methods;
b. virtual method;
How will pH - > head () explain this?
Answer: (a) if head() is a non virtual function, then pH - > head() will call corporation::head(), but the value of the member variable uses the initial value given by the derived class object, which is given by the base class object. In other words, pH - > head() uses the member variable initialized by the derived class object.
(b) if it is a virtual function, pH - > head() calls publicCorporation::head().
II. Programming questions
1. Design a Building class Building, from which the teaching Building class teach Building and dormitory Building are derived
The former includes basic information such as teaching building number, number of floors, number of classrooms, total area, etc.
The latter includes basic information such as dormitory building number, number of floors, number of dormitories, total area and total number of students.
#include <iostream> #include <iomanip> using namespace std; class Building{ public: Building(int ID,int floors,int area):ID(ID),floors(floors),area(area){}; int getID(); int getFloors(); int getArea(); private: int ID; int floors; int area; }; int Building::getID() { return ID; } int Building::getFloors() { return floors; } int Building::getArea() { return area; } class Teach_Building:public Building{ public: Teach_Building(int ID,int floors,int area,string buildName,int classRoom, int studentNum):Building(ID,floors,area),buildName(buildName), classRooms(classRooms),studentNum(studentNum){}; void display(); private: string buildName; int classRooms; int studentNum; }; void Teach_Building::display() { cout << buildName<<"base information as follows: "<<endl; cout<<"NO: "<<this->getID()<<endl; cout<<"level num: "<<this->getFloors()<<endl; cout<<"total areas: "<<this->getArea()<<endl; cout<< "nums of classroom: "<<classRooms<<endl; cout<<"capacity of students: "<<studentNum<<endl; } class Dorm_Building:public Building{ public: Dorm_Building(int ID, int floors, int area, string buildingName, int dormRooms, int studentNum):Building(ID,floors,area), buildingName(buildingName),dromRooms(dormRooms), studentNum(studentNum){}; void display(); private: string buildingName; int dromRooms; int studentNum; }; void Dorm_Building::display(){ cout<<buildingName<<"dormbuilding information as follows: "<<endl; cout<<"ID: "<<this->getID()<<endl; cout<<"level num: "<<this->getFloors()<<endl; cout<<"total area: "<<this->getArea()<<endl; cout<<"dorm num: "<<this->dromRooms<<endl; cout<<"capacity of students: "<<studentNum<<endl; } int main() { Teach_Building *p = new Teach_Building(1,2,3,"2",2,4); p->display(); return 0; }