|
father
class People {
public:
People (char * str); // Constructor
~ People (); // Destructor
protected:
char * name;
};
#endif
Parent class implementation:
{
People :: People (char * str) {
name = new char [strlen (str) +1];
strcpy (name, str);
cout << "People construct:" << name << endl;
}
// The implementation of the destructor
People :: ~ People () {
cout << "People destroy:" << name << endl;
delete [] name;
} People :: People (char * str) {
name = new char [strlen (str) +1];
strcpy (name, str);
cout << "People construct:" << name << endl;
}
// The implementation of the destructor
People :: ~ People () {
cout << "People destroy:" << name << endl;
delete [] name;
}
Derived class:
class Teacher: public People {
public:
Teacher (char * str, char * sch); // Constructor
~ Teacher (); // Destructor
protected:
char * school;
};
Derived implementation
Teacher :: Teacher (char * str, char * sch)
: People (str) // Call the constructor of the base class
{school = new char [strlen (sch) +1];
strcpy (school, sch);
cout << "Teacher construct:" << name << "in" ------ 1
<< school << endl;
}
Teacher :: ~ Teacher () {
cout << "Teacher destroy:" << name << "in" ----------- 2
<< school << endl;
delete [] school;}
The name of the parent class member must be a protected type in order to use name in the derived and destructor functions 1, 2 and 1. Change to a private type and it will not be accessible in subclasses. However, the subclass inherits its own name attribute when inherited. Why can't it be called? Is the name of the parent class or a child class called at 1, 2? |
|