类与对象之小task


task

​ 设计一个产品类Product,允许通过如下方式创建产品对象。

  1. 通过指定产品名创建。
  2. 通过指定产品名和产品价格创建。
  3. 通过指定产品名,产品价格,出厂日期(对象成员)创建。
  4. Product还应该包含如下属性:生产厂家,易碎标记,有效日期(使用对象成员)。设计该类时,至少增加3个其他属性。成员函数包括访问和修改这些属性的操作。
  5. 在main()中定义对象,并输出相关信息。
#include<iostream>
#include<string>
using namespace std;
class Product
{
public:
Product();
Product(string na);
Product(string na, double pr);
void SetProduct(string na, double pr, string fa, bool ea, string co, double h);
void output();
~Product();

private:
string name;
double price;
string factory;
bool easy_break;
string color;
double high;
};

Product::Product()
{
}

Product::Product(string na)
{
na = name;
}

Product::Product(string na, double pr)
{
na = name;
pr = price;
}

void Product::SetProduct(string na, double pr, string fa, bool ea, string co, double h)
{
name = na;
price = pr;
factory = fa;
easy_break = ea;
color = co;
high = h;
}

void Product::output()
{
cout << name << " " << price << " " << endl;
cout << factory<< " " << easy_break << " " << endl;
cout << color << " " << high << " " << endl;
}

Product::~Product()
{
}
int main()
{
Product p1("car");
Product p2("glass", 3.00);
p1.SetProduct("car", 100000.0, "nj", 0, "red", 1.5);
p1.output();
return 0;
}
car 100000
nj 0
red 1.5

Author: T1g3r
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source T1g3r !
评论
  TOC