用这个book类简单总结了之前关于友元,内敛,复制构造函数<iomanip>
注释写了注意事项
#include<iostream>
#include<iomanip>using namespace std;
class book{
private:
double price;
int date;
public:
book():price(0),date(0){};
book(double p,int d){price = p;date=d;};
book(book& b){price = b.price;date = b.date;}
~book(){};//如果要手动写析构函数 一定要写完函数体
void setdate(int d);
void setprice(double p);
int getdate();
double getprice();
friend void getanotherdate(book& b);//friend关键字写在返回值之前
double getantherprice();
};//不要漏了分号 养成好习惯
void book::setdate(int d){//不要漏了作用域
date = d;
}
void book::setprice(double p){
price = p;
}
int book::getdate(){
return date;
}
double book::getprice(){
return price;
}
void getanotherdate(book& b){
setfill(' ');
cout<<setw(5)<<b.date<<endl;
}
inline double book::getantherprice()
{
return price;
}
int main()
{
setfill(' ');
book b1;
cout<<"date:"<<b1.getdate()<<" date:"<<setw(3)<<b1.getprice()<<endl;
book b2(10,20);
cout<<"date:"<<b2.getdate()<<" date:"<<setw(3)<<b2.getprice()<<endl;
book b3;
b3.setdate(20);
b3.setprice(60.0);
cout<<"date:"<<b3.getdate()<<" date:"<<setw(3)<<b3.getprice()<<endl;
book b4(b3);
cout<<"date:"<<b4.getdate()<<" date:"<<setw(3)<<b4.getprice()<<endl;
cout<<"date:";
getanotherdate(b4);
cout<<"price:"<<b4.getantherprice()<<endl;
return 0;
}