4.设计一个产品类 Product,允许通过如下方式创建产品对象过指定产品名创建。
通过指定产品名和产品价格创建
通过指定产品名、产品价格、出厂日期(对象成员)创建。
Product 还应该包含如下属性:生产厂家、易碎标记、有效日期(使用对象成货)设计该类时,至少增加 3 个其他属性。成员函数包括访问和修改这些属性的操作
在 mainO中定义对象,并输出相关信息。
#include <string>
#include <iostream>
using namespace std;
class Product
{
private:
string name;
float price;
string date;
string factory;
int flag;
string fordate;
public:
Product() {}
Product(string n,float p) {name = n;price = p;}
Product(string n,float p,string d) {name = n;price = p;date =d;}
void print()
{
cout<<"名称:"<<name<<endl;
cout<<"价格:"<<price<<endl;
cout<<"出厂日期:"<<date<<endl;
cout<<"生产厂家:"<<factory<<endl;
cout<<"易碎标志:"<<flag<<endl;
cout<<"有效日期:"<<fordate<<endl;
}
void setName(string n) {name = n;}
string getName() {return name;}
};
int main()
{
Product p1("名称1",10.2);
Product p2("名称2",91.8,"2021-10-20");
p1.print();
p2.print();
return 0;
}