C++平面鲜花代码
时间: 2023-07-10 20:04:31 浏览: 111
以下是一个简单的C++平面鲜花代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
class Flower {
private:
string name;
string color;
int petals;
public:
Flower(string n, string c, int p) {
name = n;
color = c;
petals = p;
}
void printFlower() {
cout << "Name: " << name << endl;
cout << "Color: " << color << endl;
cout << "Number of petals: " << petals << endl;
}
};
int main() {
Flower rose("Rose", "Red", 32);
Flower lily("Lily", "White", 6);
cout << "First flower:" << endl;
rose.printFlower();
cout << "\nSecond flower:" << endl;
lily.printFlower();
return 0;
}
```
这个示例定义了一个名为`Flower`的类,该类有三个私有成员变量:`name`、`color`和`petals`。构造函数用于初始化这些变量。`printFlower`函数用于打印出鲜花的名称、颜色和花瓣数。在`main`函数中,我们创建了两个鲜花对象并打印它们的详细信息。
阅读全文