优化#include<iostream>using namespace std;class Teacher{protected: string name; int age; char gender; string address; string phone; string title;public: void display(){ cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender: "<<gender<<endl; cout<<"Address: "<<address<<endl; cout<<"Phone: "<<phone<<endl; cout<<"Title: "<<title<<endl; }};class Cadre{protected: string name; int age; char gender; string address; string phone; string post;public: void display(){ cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender: "<<gender<<endl; cout<<"Address: "<<address<<endl; cout<<"Phone: "<<phone<<endl; cout<<"Post: "<<post<<endl; }};class Teacher_Cadre:public Teacher, public Cadre{private: double wages;public: void show(){ Teacher::display(); cout<<"Post: "<<Cadre::post<<endl; cout<<"Wages: "<<wages<<endl; }};int main(){ Teacher_Cadre tc; tc.name="Tom"; tc.age=30; tc.gender='M'; tc.address="Beijing"; tc.phone="123456789"; tc.title="Professor"; tc.post="Manager"; tc.wages=10000; tc.show(); return 0;}
时间: 2023-08-20 13:09:38 浏览: 91
使用include <iostream>时一定要加using namespace std
代码本身没有什么问题,但是可以优化一下:
1. 在 Teacher 和 Cadre 类中,有很多重复的属性和方法,可以抽象出一个基类来,减少冗余代码。
2. 在 Teacher 和 Cadre 类中,属性和方法都是 protected 的,可以将它们改为 private ,并添加相应的访问函数(如 getName()、getAge() 等)。
3. 可以使用构造函数来对对象进行初始化,避免在 main 函数中逐个赋值。
优化后的代码如下:
```
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int age;
char gender;
string address;
string phone;
public:
Person(string n, int a, char g, string addr, string ph) : name(n), age(a), gender(g), address(addr), phone(ph) {}
string getName() { return name; }
int getAge() { return age; }
char getGender() { return gender; }
string getAddress() { return address; }
string getPhone() { return phone; }
};
class Teacher : public Person {
private:
string title;
public:
Teacher(string n, int a, char g, string addr, string ph, string t) : Person(n, a, g, addr, ph), title(t) {}
string getTitle() { return title; }
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << "Title: " << title << endl;
}
};
class Cadre : public Person {
private:
string post;
public:
Cadre(string n, int a, char g, string addr, string ph, string p) : Person(n, a, g, addr, ph), post(p) {}
string getPost() { return post; }
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << "Post: " << post << endl;
}
};
class Teacher_Cadre : public Teacher, public Cadre {
private:
double wages;
public:
Teacher_Cadre(string n, int a, char g, string addr, string ph, string t, string p, double w) : Teacher(n, a, g, addr, ph, t), Cadre(n, a, g, addr, ph, p), wages(w) {}
void show() {
Teacher::display();
cout << "Post: " << Cadre::post << endl;
cout << "Wages: " << wages << endl;
}
};
int main() {
Teacher_Cadre tc("Tom", 30, 'M', "Beijing", "123456789", "Professor", "Manager", 10000);
tc.show();
return 0;
}
```
阅读全文