C++l设计一个图书类,包含图书编号,名称,价格,出版社等属性设计一个函数显示图书的全部信息
时间: 2024-02-27 20:51:43 浏览: 24
好的,这里给出一个简单的图书类的实现,同时包含一个显示全部信息的函数:
```c++
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string id; // 图书编号
string name; // 图书名称
double price; // 图书价格
string publisher; // 出版社
public:
Book(string _id, string _name, double _price, string _publisher):
id(_id), name(_name), price(_price), publisher(_publisher) {} // 构造函数
void display() { // 显示全部信息
cout << "编号:" << id << endl;
cout << "名称:" << name << endl;
cout << "价格:" << price << endl;
cout << "出版社:" << publisher << endl;
}
};
int main() {
Book book("001", "C++ Primer", 68.0, "电子工业出版社");
book.display();
return 0;
}
```
在这个示例中,我们定义了一个 `Book` 类,包含了四个私有属性:图书编号、图书名称、图书价格和出版社。构造函数用于初始化这些属性,同时我们还定义了一个 `display` 函数来显示全部信息。在 `main` 函数中,我们创建了一个 `Book` 对象,并调用了 `display` 函数来显示其全部信息。