设计书类Book,成员函数包括作者(char author[40]),书名(char title[40]),价格(double price)。根据给定的main函数设计必要的成员函数。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> #include<cstring> #include<iomanip> using namespace std; //你提交的代码在这里 int main() { cout<<fixed<<setprecision(2); Book b1; cout<<"Show object b1:"; cout<<b1.GetTitle()<<","<<b1.GetAuthor()<<","<<b1.GetPrice()<<endl; char title[40],author[40]; double price; cin>>title>>author>>price; Book b2(title,author,price); cout<<"Show object b2:"; cout<<b2.GetTitle()<<","<<b2.GetAuthor()<<","<<b2.GetPrice()<<endl; cin>>title>>author>>price; cout<<"Reset and Show object b1:"; b1.Set(title,author,price); b1.Show(); cout<<"Reset and Show object b2:"; b2.Set(); b2.Show(); return 0; } 输入 2行,每行3个数据,分别代表一本书的书名、作者以及价格。 输出 按照样例输出格式输出。 输入样例 1 C++程序设计 郑莉 45.12 c++程序设计基础 王育坚 68.5 输出样例 1 Show object b1:NULL,NONE,0.00 Show object b2:C++程序设计,郑莉,45.12 Reset and Show object b1:c++程序设计基础,王育坚,68.50 Reset and Show object b2:高等数学(第七版)上册,同济大学数学系,37.70
时间: 2024-03-26 12:36:27 浏览: 144
```
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Book {
private:
char author[40];
char title[40];
double price;
public:
Book() {
strcpy(this->author, "NONE");
strcpy(this->title, "NULL");
this->price = 0.00;
}
Book(char title[], char author[], double price) {
strcpy(this->author, author);
strcpy(this->title, title);
this->price = price;
}
char* GetAuthor() { return this->author; }
char* GetTitle() { return this->title; }
double GetPrice() { return this->price; }
void Set(char title[], char author[], double price) {
strcpy(this->author, author);
strcpy(this->title, title);
this->price = price;
}
void Set() {
char title[40], author[40];
double price;
cin >> title >> author >> price;
Set(title, author, price);
}
void Show() {
cout << this->title << "," << this->author << "," << fixed << setprecision(2) << this->price << endl;
}
};
int main() {
cout << fixed << setprecision(2);
Book b1;
cout << "Show object b1:";
cout << b1.GetTitle() << "," << b1.GetAuthor() << "," << b1.GetPrice() << endl;
char title[40], author[40];
double price;
cin >> title >> author >> price;
Book b2(title, author, price);
cout << "Show object b2:";
cout << b2.GetTitle() << "," << b2.GetAuthor() << "," << b2.GetPrice() << endl;
cin >> title >> author >> price;
cout << "Reset and Show object b1:";
b1.Set(title, author, price);
b1.Show();
cout << "Reset and Show object b2:";
b2.Set();
b2.Show();
return 0;
}
```
阅读全文