Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果B类和C类都是从C+中的A类派生而来,如何将B类的对象转换为C类+;?_C++_Class_Inheritance_Type Conversion - Fatal编程技术网

C++ 如果B类和C类都是从C+中的A类派生而来,如何将B类的对象转换为C类+;?

C++ 如果B类和C类都是从C+中的A类派生而来,如何将B类的对象转换为C类+;?,c++,class,inheritance,type-conversion,C++,Class,Inheritance,Type Conversion,我有一个名为“Customer”的基类,以及两个派生类“RegularAccount”和“VipAccount” 现在,我想实现一个“promote”成员方法,如果RegularAccount满足某些条件,它允许将RegularAccount升级为VipAccount 如何在C++中实现这一点?我试图在RegularAccount的“promote”方法中调用构造函数,但未能实现。在RegularAccount的成员函数中,是否有方法将所有数据从RegularAccount的对象复制到VipAc

我有一个名为“Customer”的基类,以及两个派生类“RegularAccount”和“VipAccount”

现在,我想实现一个“promote”成员方法,如果RegularAccount满足某些条件,它允许将RegularAccount升级为VipAccount

如何在C++中实现这一点?我试图在RegularAccount的“promote”方法中调用构造函数,但未能实现。在RegularAccount的成员函数中,是否有方法将所有数据从RegularAccount的对象复制到VipAccount的对象

以下是我的RegularAccount类的结构:

class RegularAccount : public Customer {
private:

public:
  // Constructors && Destructors
  RegularAccount();
  RegularAccount(string id, string name, string address, string phoneNumber, int numberOfRental);
  ~RegularAccount();

  // Member functions
  void rentItem(const string itemName);

  void returnItem(const string itemName);

  void details();

  void showRentalList();

  void promote();

};
class VipAccount : public Customer {
private:
  int rewardPoints;
  int freeRentItemAwarded;


public:
  // Constructors
  VipAccount();

  VipAccount(string id, string name, string address, string phoneNumber, int numberOfRental);

  // Member functions
  void rentItem(const string itemName);

  void returnItem(const string itemName);

  void checkRewardPoints();

  void details();

  void showRentalList();

  void promote();

};
VipAccount类别:

class RegularAccount : public Customer {
private:

public:
  // Constructors && Destructors
  RegularAccount();
  RegularAccount(string id, string name, string address, string phoneNumber, int numberOfRental);
  ~RegularAccount();

  // Member functions
  void rentItem(const string itemName);

  void returnItem(const string itemName);

  void details();

  void showRentalList();

  void promote();

};
class VipAccount : public Customer {
private:
  int rewardPoints;
  int freeRentItemAwarded;


public:
  // Constructors
  VipAccount();

  VipAccount(string id, string name, string address, string phoneNumber, int numberOfRental);

  // Member functions
  void rentItem(const string itemName);

  void returnItem(const string itemName);

  void checkRewardPoints();

  void details();

  void showRentalList();

  void promote();

};

从RegularAccount获得VipAccount,这是有道理的,因为Vip应该拥有常规帐户的一切。为VipAccount编写一个构造函数,它接受RegularAccount实例作为参数。在构造函数中,将参数传递给基类RegularAccount。要升级,请创建一个以RegularAccount为参数的VipAccount实例,然后删除RegularAccount实例

请放入最小可复制示例。什么是VIPActIn的定义?我是一个同时学习C和C++的学生。我对指针和继承概念很陌生。@斯皮耶尔抱歉,我只是把标题放在VIPActType类中。这里的问题是,如果你问10个C++开发者“什么是最好的方法”,你会得到11个不同的答案。不幸的是,基于意见的问题与stackoverflow.com无关。我实现这一点的方法是在类中有一个状态标志,
enum class Status{regular,vip}和一个
状态\u状态成员变量。或者将另一个状态接口成员变量用于具体的状态实现,该状态实现可以根据需要进行设置或重置。