类设计::
#include<iostream>
#include<cstring> //
#include<iomanip>
using namespace std;
class Book
{
private:
char author[40],title[40];
double price;
public:
Book(const char *a="NULL",const char *t="NONE",double p=0)
{
strcpy(author,a);
strcpy(title,t);
price=p;
}
~Book(){}
char *GetTitle();
char *GetAuthor();
double GetPrice();
void Set(const char *a,const char *t,double p);
};
char *Book::GetAuthor()
{
return author;
}
char *Book::GetTitle()
{
return title;
}
double Book::GetPrice()
{
return price;
}
void Book::Set(const char *a,const char *t,double p)
{
strcpy(author,a);
strcpy(title,t);
price=p;
}
int main()
{
cout<<fixed<<setprecision(2); // 需要增加#include<iomanip>
Book b1;
cout<<"Book1:"<<b1.GetTitle()<<"----"<<b1.GetAuthor()<<"----"<<b1.GetPrice()<<endl;
Book b2("郑莉","C++程序设计",45.12);
cout<<"Book2:"<<b2.GetTitle()<<"----"<<b2.GetAuthor()<<"----"<<b2.GetPrice()<<endl;
b1.Set("王育坚","Visual c++程序设计基础",68.5);
cout<<"Book3:"<<b1.GetTitle()<<"----"<<b1.GetAuthor()<<"----"<<b1.GetPrice()<<endl;
return 0;
}