在c ++中创建父类中的派生calss

时间:2016-05-25 09:03:27

标签: c++ inheritance

在我的程序中,我有一个父类和该类的2个派生类。 在一个函数中(虚函数,每个类/衍生函数不同)我赋予(使用new)一个其他类的var。结果,导致编译错误。 我该如何解决?

部分代码: 父类:

class client
{
protected:
    double surf_hours,disc_space,phone_time;
    string first_name,last_name;
public:
.
.
.
    virtual client* is_valid_plan()
    {
         client *B,*C;
         double client, business, company;
         client=compute_bill();
         B= new business(first_name,last_name,surf_hours,disc_space,phone_time);
         C= new company(first_name,last_name,surf_hours,disc_space,phone_time);
         .
         .
         .
};

派生类#1:

class business:public client
{
public:
    business(string name, string last, double surf, double disc, double phone):client(name,last,surf,disc,phone){}
    client* is_valid_plan()
    {
         client *B,*C;
         double client, business, company;
         business=compute_bill();
         B= new client(first_name,last_name,surf_hours,disc_space,phone_time);
         C= new company(first_name,last_name,surf_hours,disc_space,phone_time);
         .
         .
         .
};

派生类#2:

class company:public client
{
public:
    company(string name, string last, double surf, double disc, double phone):client(name,last,surf,disc,phone){}
    client* is_valid_plan()
    {
         client *B,*C;
         double client, business, company;
         company=compute_bill();
         B= new business(first_name,last_name,surf_hours,disc_space,phone_time);
         C= new client(first_name,last_name,surf_hours,disc_space,phone_time);
         .
         .
         .
};

1 个答案:

答案 0 :(得分:1)

您有变量/类名冲突:

   double client, business, company;
                  ^^^^^^^^
   client=compute_bill();
   B= new business(first_name,last_name,surf_hours,disc_space,phone_time);
          ^^^^^^^^

business变量选择更好的名称。同样的问题与其他变量名称有关:clientcompany