C++ 中的组合语法
Composition syntax in C++
我正在尝试使用 C++ 中的组合和 类。我遇到的代码片段以下列方式实现组合:
#include<iostream>
using namespace std;
class Engine {
public:
int power;
};
class Car {
public:
Engine e;
string company;
string color;
void show_details() {
cout << "Compnay is: " << company << endl;
cout << "Color is: " << color << endl;
cout << "Engine horse power is: " << e.power << endl;
}
};
int main() {
Car c;
c.e.power = 500;
c.company = "hyundai";
c.color = "black";
c.show_details();
return 0;
}
这可以正常编译并运行。我不喜欢此实现的一件事是函数 "void show_details" 的实际 位置 ,我更愿意将其放在外面。
但是,如果我天真地尝试执行以下操作:
#include<iostream>
using namespace std;
class Engine {
public:
int power;
};
class Car {
public:
//Engine e;
string company;
string color;
void show_details();
//void show_details() {
// cout << "Compnay is: " << company << endl;
// cout << "Color is: " << color << endl;
// cout << "Engine horse power is: " << e.power << endl;
//}
};
void Car::show_details(){
cout << "Compnay is: " << company << endl;
cout << "Color is: " << color << endl;
cout << "Engine horse power is: " << e.power << endl;
}
int main() {
Car c;
c.e.power = 500;
c.company = "hyundai";
c.color = "black";
c.show_details();
return 0;
}
g++ returns 出现以下错误:
comp3.cpp: In member function ‘void Car::show_details()’:
comp3.cpp:22:44: error: ‘e’ was not declared in this scope
cout << "Engine horse power is: " << e.power << endl;
^
comp3.cpp: In function ‘int main()’:
comp3.cpp:26:9: error: ‘class Car’ has no member named ‘e’
c.e.power = 500;
我显然对范围界定问题感到困惑,但我不确定缺失的部分是什么。
感谢所有帮助!!!
在第二个例子中你注释掉了e
。
//Engine e;
我正在尝试使用 C++ 中的组合和 类。我遇到的代码片段以下列方式实现组合:
#include<iostream>
using namespace std;
class Engine {
public:
int power;
};
class Car {
public:
Engine e;
string company;
string color;
void show_details() {
cout << "Compnay is: " << company << endl;
cout << "Color is: " << color << endl;
cout << "Engine horse power is: " << e.power << endl;
}
};
int main() {
Car c;
c.e.power = 500;
c.company = "hyundai";
c.color = "black";
c.show_details();
return 0;
}
这可以正常编译并运行。我不喜欢此实现的一件事是函数 "void show_details" 的实际 位置 ,我更愿意将其放在外面。
但是,如果我天真地尝试执行以下操作:
#include<iostream>
using namespace std;
class Engine {
public:
int power;
};
class Car {
public:
//Engine e;
string company;
string color;
void show_details();
//void show_details() {
// cout << "Compnay is: " << company << endl;
// cout << "Color is: " << color << endl;
// cout << "Engine horse power is: " << e.power << endl;
//}
};
void Car::show_details(){
cout << "Compnay is: " << company << endl;
cout << "Color is: " << color << endl;
cout << "Engine horse power is: " << e.power << endl;
}
int main() {
Car c;
c.e.power = 500;
c.company = "hyundai";
c.color = "black";
c.show_details();
return 0;
}
g++ returns 出现以下错误:
comp3.cpp: In member function ‘void Car::show_details()’:
comp3.cpp:22:44: error: ‘e’ was not declared in this scope
cout << "Engine horse power is: " << e.power << endl;
^
comp3.cpp: In function ‘int main()’:
comp3.cpp:26:9: error: ‘class Car’ has no member named ‘e’
c.e.power = 500;
我显然对范围界定问题感到困惑,但我不确定缺失的部分是什么。
感谢所有帮助!!!
在第二个例子中你注释掉了e
。
//Engine e;