如何定义一个Book类具有书名、出版社、单价、作者成员属性,并且对于每个属性提供相应方法?
2个回答
展开全部
在Book类中,通过构造方法的重载灵活实现成员变量的初始化;
无参的构造方法;
只有书名一个参数的构造方法;
有书名、出版社两个参数的构造方法;
有书名、出版社、单价三个参数的构造方法;
有书名、出版社、单价、作者四个参数的构造方法;
定义一个方法money,求得购买数量的总金额。要求:没有传入数量(即没有参数)时,直接返回一本书的价格;当传入数量(即有参数)时,金额为单价*数量,且数量只能是int型。通过方法重载来实现;
定义一个display方法,显示Book类的成员信息;
在测试类中,采用不同的构造方法,实例化5个不同的对象,并分别调用money方法,实现金额的计算。
展开全部
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Book
{
public:
Book():m_name("null"),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"无参构造方法..."<<endl;
}
Book(string name):m_name(name),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"具有书名一个参数的构造方法..."<<endl;
}
Book(string name,string author)
:m_name(name),m_author(author),
m_publisher("null"),m_price(0)
{
cout<<"具有书名、作者两个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(0)
{
cout<<"具有书名、作者、出版社三个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher,double price)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(price)
{
cout<<"具有书名、作者、出版社、单价四个参数的构造方法..."<<endl;
}
~Book(){}
void SetName(string name) {m_name=name;}
string getName() {return m_name;}
void SetPublisher(string publisher) {m_publisher=publisher;}
string getPublisher() {return m_publisher;}
void SetPrice(double price) {m_price=price;}
double getprice() {return m_price;}
void SetAuthor(string author) {m_author=author;}
string getAuthor() {return m_author;}
double money()
{
return m_price;
}
double money(int Count)
{
return m_price*Count;
}
void display()
{
cout<<"《"<<m_name<<"》\t"
<<m_author<<"\t"
<<m_publisher<<"\t"
<<setiosflags(ios::fixed)<<setprecision(1)<<m_price
<<endl<<endl;
}
private:
string m_name;
string m_author;
string m_publisher;
double m_price;
};
int main()
{
Book b1;
b1.display();
Book b2("高级Java网络编程");
b2.display();
Book b3("高级Java网络编程","王五");
b3.display();
Book b4("高级Java网络编程","王五","北京邮电社");
b4.display();
Book b5("高级Java网络编程","王五","北京邮电版社",54);
b5.display();
int n;
cout<<"请输入购买数量:";
cin>>n;
cout<<n<<"本书的金额:"<<b5.money(n);
cin.get();
cin.get();
return 0;
}
#include <iomanip>
#include <string>
using namespace std;
class Book
{
public:
Book():m_name("null"),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"无参构造方法..."<<endl;
}
Book(string name):m_name(name),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"具有书名一个参数的构造方法..."<<endl;
}
Book(string name,string author)
:m_name(name),m_author(author),
m_publisher("null"),m_price(0)
{
cout<<"具有书名、作者两个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(0)
{
cout<<"具有书名、作者、出版社三个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher,double price)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(price)
{
cout<<"具有书名、作者、出版社、单价四个参数的构造方法..."<<endl;
}
~Book(){}
void SetName(string name) {m_name=name;}
string getName() {return m_name;}
void SetPublisher(string publisher) {m_publisher=publisher;}
string getPublisher() {return m_publisher;}
void SetPrice(double price) {m_price=price;}
double getprice() {return m_price;}
void SetAuthor(string author) {m_author=author;}
string getAuthor() {return m_author;}
double money()
{
return m_price;
}
double money(int Count)
{
return m_price*Count;
}
void display()
{
cout<<"《"<<m_name<<"》\t"
<<m_author<<"\t"
<<m_publisher<<"\t"
<<setiosflags(ios::fixed)<<setprecision(1)<<m_price
<<endl<<endl;
}
private:
string m_name;
string m_author;
string m_publisher;
double m_price;
};
int main()
{
Book b1;
b1.display();
Book b2("高级Java网络编程");
b2.display();
Book b3("高级Java网络编程","王五");
b3.display();
Book b4("高级Java网络编程","王五","北京邮电社");
b4.display();
Book b5("高级Java网络编程","王五","北京邮电版社",54);
b5.display();
int n;
cout<<"请输入购买数量:";
cin>>n;
cout<<n<<"本书的金额:"<<b5.money(n);
cin.get();
cin.get();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询