应该如何修改这段代码#include<iostream> #include<string> using namespace std; class Creature { public: Creature(const int& hands, const int& legs):_hands(hands),_legs(legs) {cout << "A Creature has been created!" << endl; cout << "It has " << hands << " hand(s)!" << endl; cout << "It has " << legs << " leg(s)!" << endl;} ~Creature() {cout << "Creature object exiled!" << endl;} int GetHands() const {return _hands;} int GetLegs() const {return _legs;} private: int _hands; int _legs; }; class Beast:virtual public Creature { public: Beast(const int& hands, const int& legs, const string& name) :Creature(hands,legs),_name(name){cout << "Its beast name is " << _name << endl;} ~Beast() {cout << "Beast object exiled!" << endl;} string GetName() const {return _name;} private: string _name; }; class Human:virtual public Creature { public: Human(const int& hands, const int& legs, const string& name):Creature(hands,legs),_name(name) {cout << "Its human name is " << _name << endl;} ~Human() {cout << "Human object exiled!" << endl;} string GetName() const {return _name;} private: string _name; }; class Orc:public Human,public Beast { public: Orc(const int& hands, const int& legs, const string& beast_name, const string& human_name):Creature(hands, legs),Beast(hands,legs,beast_name),Human(hands,legs,human_name){} ~Orc() {cout << "Orc object exiled!" << endl;} string GetBeastName() const {return Beast::GetName();} string GetHumanName() const {return Human::GetName();} };
时间: 2024-02-01 10:16:03 浏览: 64
#include <iostream> #include<iomanip> using namespace std;九九乘法表
这段代码本身是没有问题的,但是为了使代码更加易读易懂,可以进行一些调整和优化:
1. 将构造函数中的参数改为 const int& 改为 const int,因为 const int& 对于这个例子来说并没有太大的必要。
2. 将 cout 语句提取为成员函数,这样可以提高代码的复用性和可维护性。
3. 将 GetHands 和 GetLegs 函数改为 inline 函数,可以提高代码的执行效率。
4. 将虚继承改为非虚继承,因为在这个例子中并没有体现出虚继承的优势。
修改后的代码如下:
```
#include<iostream>
#include<string>
using namespace std;
class Creature {
public:
Creature(int hands, int legs): _hands(hands), _legs(legs) {}
int GetHands() const {
return _hands;
}
int GetLegs() const {
return _legs;
}
void Print() const {
cout << "A Creature has been created!" << endl;
cout << "It has " << GetHands() << " hand(s)!" << endl;
cout << "It has " << GetLegs() << " leg(s)!" << endl;
}
private:
int _hands;
int _legs;
};
class Beast: public Creature {
public:
Beast(int hands, int legs, const string& name) : Creature(hands, legs), _name(name) {}
string GetName() const {
return _name;
}
void Print() const {
Creature::Print();
cout << "Its beast name is " << GetName() << endl;
}
private:
string _name;
};
class Human: public Creature {
public:
Human(int hands, int legs, const string& name): Creature(hands, legs), _name(name) {}
string GetName() const {
return _name;
}
void Print() const {
Creature::Print();
cout << "Its human name is " << GetName() << endl;
}
private:
string _name;
};
class Orc: public Human, public Beast {
public:
Orc(int hands, int legs, const string& beast_name, const string& human_name): Creature(hands, legs), Human(hands, legs, human_name), Beast(hands, legs, beast_name) {}
string GetBeastName() const {
return Beast::GetName();
}
string GetHumanName() const {
return Human::GetName();
}
void Print() const {
Creature::Print();
cout << "Its human name is " << GetHumanName() << endl;
cout << "Its beast name is " << GetBeastName() << endl;
}
};
int main() {
Orc orc(2, 2, "beast", "human");
orc.Print();
return 0;
}
```
这样的代码更加易读易懂,也更加符合现代 C++ 的编程规范和风格。
阅读全文