#include<iostream.h>
#include<string.h>
class Merchandise{
public:
Merchandise(char *,int ,double );
Merchandise(){};
~Merchandise(){};
friend istream &operator>>(istream& in,Merchandise& s);
friend ostream &operator<<(ostream& out,Merchandise& s);
int getNo(){
return this->no;
}
char *getName(){
return this->name;
}
double getPrice(){
return this->price;
}
private:
int no;
char name[20];
double price;
};
Merchandise::Merchandise(char *name,int no,double price){
strcpy(this->name,name);
this->no=no;
this->price=price;
}
istream& operator>>(istream &in,Merchandise& s){
cout<<"* * * * * * * * * * * * 输 入 商 品 名称 编号 价格 * * * * * * * * * * * *";
in>>s.name;
in>>s.no;
in>>s.price;
return in;
}
ostream& operator<<(ostream &out,Merchandise& s){
cout<<"* * * * * * * * * * * * * * * *商 品 信 息 * * * * * * * * * * * * * * * *";
out<<s.name<<"\t";
out<<s.no<<"\t";
out<<s.price<<endl;
return out;
}
int main(){
Merchandise mer;
cin>>mer;
cout<<mer;
return 0;
}
重载运算符“<<”和“>>”,使其能够输入一件商品的信息和输出这件商品的信息。商品的信息由编号、商品名和价格。
最新推荐文章于 2021-04-09 15:20:11 发布