C++ C++;类继承,构造函数应该是什么样子?

C++ C++;类继承,构造函数应该是什么样子?,c++,class,inheritance,C++,Class,Inheritance,我想写一个代码来计算客户需要支付多少美元的电话费 我想要3种类型的合同: 基本:10美元/分钟,5美元/短信 短信god:10$/分钟,1$/短信和前150条免费短信 升级:5$/分钟,3$/短信和前25条免费短信 我想把它建成这样: 一个类,包含客户端必须支付的电话和短信费用: class BaseClass { private: int minutesPrice; int SMSPrice; public: setMinutesPrice(int x) { minu

我想写一个代码来计算客户需要支付多少美元的电话费

我想要3种类型的合同:

基本:10美元/分钟,5美元/短信

短信god:10$/分钟,1$/短信和前150条免费短信

升级:5$/分钟,3$/短信和前25条免费短信

我想把它建成这样:

一个类,包含客户端必须支付的电话和短信费用:

class BaseClass 
{
private: 
   int minutesPrice; 
   int SMSPrice;
public:
    setMinutesPrice(int x) { minutesPrice = x; }
    setSMSPrice(int x) { SMSPrice = x; }
};
和3子类您可以在上面看到的合同类型是什么:

class Basic : public BaseClass 
{
public:
    Basic()
    {
        setMinutesPrice(10);
        setSMSPrice(5);
    }
}

class SMSGod : public BaseClass
{
private: 
   int freeSMS;
public: 
   SMSGod() 
   {
       setMinutesPrice(10);
       setSMSPrice(1);
       freeSMS = 150;
   }
}

class Upgraded: public BaseClass
{
private: 
   int freeSMS;
public: 
   Upgraded() 
   {
       setMinutesPrice(5);
       setSMSPrice(3);
       freeSMS = 25;
   }
}
然后是一个客户端类:

class Client: public BaseClass
{
public:
    string name;
    string phoneNumber;
    string typeOfContract;
    int talkedMinutes;
    int sentSMS;
public:
Client(...){...}
};
这是我的问题。我应该如何制作Client()构造函数,以便能够先构建一个Client\u Client(…);对象我应该如何调用正确的构造函数:Basic、SMSGod或根据合同类型升级

这应该起作用:

Client Jack("Jack", "444-468-745", "Upgraded", 50, 25);
然后还有另一个函数,根据他说了多少分钟(50),发了多少短信(25),以及第三个参数(“升级”),我可以使用getter函数来了解获得“升级”合同的人的底价是多少,以及他们获得了多少免费短信

(这应在最后计算电话账单:

int havetopay(Client x)
{
   int freeSMS = x.getFreeSMS();
   int sentSMS = x.getSentSMS();
   int SMS;
   if (freeSMS > sentSMS) SMS = 0;
       else SMS = sentSMS - freeSMS;
   return (SMS * x.getSMSPrice() + x.getTalkedMinutes() * x.getMinutesPrice());
}
)


谢谢你的帮助

您的客户机类可以是模板类,根据传递的模板参数,将调用适当的构造函数:

template <class T>
class Client:public T
{
  private:
     T Obj;
  public:
     T getObject() const
     {
        return Obj;
     } 

};
Client<SMSGod> c;

template<class T>
int havetopay(Client<T> clientObj)
{
   auto x = clientObj.getObject();
   int freeSMS = x.getFreeSMS();
   int sentSMS = x.getSentSMS();
   int SMS;
   if (freeSMS > sentSMS) SMS = 0;
       else SMS = sentSMS - freeSMS;
   return (SMS * x.getSMSPrice() + x.getTalkedMinutes() * x.getMinutesPrice());
}
模板
类客户端:公共T
{
私人:
T-Obj;
公众:
T getObject()常量
{
返回Obj;
} 
};
客户c;
模板
int havetopay(客户客户端北京)
{
auto x=clientObj.getObject();
int freems=x.getfreems();
int sentSMS=x.getSentSMS();
int SMS;
如果(freeSMS>sentSMS)SMS=0;
else SMS=sentSMS-FreeMS;
返回(SMS*x.getSMSPrice()+x.getTalkedMinutes()*x.getMinutesPrice());
}

然后您也可以将havetopay更改为模板化函数

正如我在评论中所述,我认为客户机类不应该继承处理计划细节和计算的类,因为这会将它们错误地绑定在一起。希望动态更改计划也是很合理的,因此使客户机成为一个模板类,将计划作为其类型,也会不必要地将它们联系在一起(尽管这可能会节省一些代码)。我的建议大致如下:

class BasePlan 
{
private: 
   int minutesPrice; 
   int SMSPrice;
public:
    setMinutesPrice(int x) { minutesPrice = x; }
    setSMSPrice(int x) { SMSPrice = x; }
    virtual int calculateBill(int minutesTalked, int smsSent) = 0;
};

class Basic : public BasePlan 
{
public:
    Basic()
    {
        setMinutesPrice(10);
        setSMSPrice(5);
    }
    virtual int calculateBill(int minutesTalked, int smsSent) { return minutesTalked * minutesPrice + smsSent * SMSPrice;}
}

class SMSPlan : public BasePlan
{ 
private: 
   int freeSMS;
public: 
   SMSPlan(int minutesPrice, int smsPrice, int freeSMS) 
   {
       setMinutesPrice(minutesPrice);
       setSMSPrice(smsPrice);
       setFreeSMS(freeSMS);
   }

   public setFreeSMS(int free) { this.freeSMS = free; }

   virtual int calculateBill(int minutesTalked, int smsSent) { 
      int billedSMS = (freeSMS > sentSMS) ? 0 : sentSMS - freeSMS;
      return minutesTalked * minutesPrice + billedSMS * SMSPrice;}
}

class SMSGod : public SMSPlan 
{
public: 
   SMSGod() : SMSPlan(10, 1, 150)
   {
   }
}

class Upgraded: public SMSPlan 
{
public: 
   Upgraded() : SMSPlan(5, 3, 25) 
   {
   }
}

class Client
{
public:
    string name;
    string phoneNumber;
    BasePlan* currentPlan;
    int talkedMinutes;
    int sentSMS;
public:
Client(...){...}
void setPlan(BasePlan* plan) { this->currentPlan = setPlan(plan);}
int getBill() { return this->currentPlan->calculateBill(talkedMinutes, sentSMS); }
};
现在,您可以先创建一个客户机,然后随时将其计划设置为您喜欢的任何计划(您可能希望强制使用默认计划构建它,以确保它始终具有一个计划并避免空检查):


我也不认为在构造器中设置谈话时间并向客户发送短信有多大意义,但这取决于您。

我想说,这听起来像是战略模式的一个经典例子,您的客户与他们的计划是分开的。该计划由客户作为一个组成部分持有,并可在需要时互换。客户端对象应将所有与计划相关的计算委托给计划本身。是否可以使用模板<代码>客户机插孔(“插孔”,“444-468-745”,50,25)听起来对我很有好处。我可以将模板参数作为变量传递吗?像客户c?是的,您可以将模板类对象作为函数参数传递。我必须自己更正:我将传递一个字符串作为模板参数:string contract=“升级”;客户c;
Client Jack = new Client("Jack", "444-468-745", 50, 25);
Jack.setPlan(new Upgraded());
int JacksBill = Jack.getBill();