c++声明一个图书类,包含图书编号、名称、价格‘出版社等属性,设计一个函数显示图书的全部信息,通过main函数调用显示结果
时间: 2024-02-27 20:53:36 浏览: 20
好的,以下是一个 C++ 图书类的声明,包含图书编号、名称、价格、出版社等属性和显示全部信息的函数:
```cpp
#include <iostream>
#include <string>
class Book {
private:
std::string id;
std::string name;
double price;
std::string publisher;
public:
Book(std::string id, std::string name, double price, std::string publisher);
void display();
};
Book::Book(std::string id, std::string name, double price, std::string publisher) {
this->id = id;
this->name = name;
this->price = price;
this->publisher = publisher;
}
void Book::display() {
std::cout << "ID: " << id << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Price: " << price << std::endl;
std::cout << "Publisher: " << publisher << std::endl;
}
int main() {
Book book("B001", "C++ Primer", 88.8, "China Machine Press");
book.display();
return 0;
}
```
这个类有四个私有成员变量,即图书编号、名称、价格和出版社,以及一个公有的构造函数和一个公有的显示图书信息的成员函数 `display()`。在 `main()` 函数中创建了一个 `Book` 类的对象 `book`,并调用了其 `display()` 函数来显示图书的全部信息。