在testBook.cpp中,创建一个mian函数,在mian函数中创建两个Book类型的对象,并对各对象初始化,调用showBook()成员函数输出各对象的信息,通过相应的set函数,修改各本书的名称和价格,再输出书的相关信息。。
// Book.h代码:
#ifndef BOOK_H
#define BOOK_H
#include<iostream>
#include<string>
class Book
{
public:
explicit Book(std::string name, int price, int number, std::string publishing);
void setBook(std::string name, int price);
int getBook();
void ShowBook();
private:
std::string BookName;
double BookPrice;
int BookNumber;
std::string PubliShing;
};
#endif
//Book.cpp代码:
#include "Book.h"
#include<iostream>
#include<string>
using namespace std;
Book::Book(string name, int price, int number, string publishing)
:BookName(name), BookPrice(price), BookNumber(number), PubliShing(publishing)
{
//BookName=name;
}
void Book::setBook(string name, int price)
{
BookName=name;
BookPrice=price;
}
int Book::getBook()
{
return BookNumber;
}
void Book::ShowBook()
{
cout << "hello,welcome to Book!\n" << BookName <<" " << BookPrice << " " << BookNumber << " "<< PubliShing << endl;
}
//testBook.cpp代码:
#include <iostream>
#include"Book.h"
#include<string>
using namespace std;
int main()
{
string name;
int price;
Book Book1("三国演义",25.2,0001,"中华出版社");
Book Book2("红楼梦",30.4,0002,"中华出版社");
Book1.ShowBook();
cout << "请输入你所要修改的书的名字以及价格\n";
getline(cin,name);
cin >> price;
Book1.setBook(name, price);
cout << "get the name and price of the book" << Book1.getBook() << Book2.getBook() << endl;
cout << "intformation about the book\n";
Book1.ShowBook();
Book2.ShowBook();
}