【问题标题】:Is it possible to create a constructor for a derived class?是否可以为派生类创建构造函数?
【发布时间】:2020-03-08 17:41:13
【问题描述】:

我试图调用派生类中的所有虚函数,然后在外部将它们定义为saving 类,但是我看起来我的构造函数类没有正确定义,我似乎无法修复它。

我正在使用来自class accountInfo 的构造函数来存储虚函数和受保护的成员,我希望balance 使用saving(current_balance,current_interest); 进行更新。但是,我确信我在某个地方有一个逻辑错误,但我找不到它。我知道我不使用构造函数accountInfo(double addBalance, double intRate);,因为我想使用派生类而不是父类来更新 balance 的值


#include <iostream>
#include <math.h>
#include <string>
using namespace std;


class accountInfo{

public:
  accountInfo();
  accountInfo(double addBalance, double intRate);
  virtual void deposit(double amount);
  virtual void withdraw(double amount);
  virtual double calcInt();
  virtual double monthlyProc();
  void virtual print();
  ~accountInfo();
protected:
  double balance = 0;
  int numDeposits = 0;
  int numWithdraws = 0;
  double annual_Interest = 0;
  double monthlyServiceFee = 10;
  bool status = false;
};

class saving: public accountInfo{

public:

  saving(double addBalance, double intRate);
  void print();
  void withdraw(double amount);
  void deposit(double amount);
  double calcInt();
  double monthlyProc();

};

void saving::deposit(double amount){   //how to call back the base function
  if(balance >=25){
      status = true;
      balance +=amount;
      numDeposits++;
  }
  else
      status = false;
      cout << "Balance is below 25" << endl;
}



accountInfo::accountInfo(){}

accountInfo::accountInfo(double addBalance, double intRate){

  balance = addBalance;
  annual_Interest = intRate;
}

saving::saving(double addBalance, double intRate){

  balance = addBalance;
  annual_Interest = intRate;
}


void saving::withdraw(double amount){

  balance -= amount;
  numWithdraws++;

}

double saving::calcInt(){

  double monthlyRate = annual_Interest/12;
  double monthlyInterest = balance * monthlyRate;
  return balance + monthlyInterest;

}

double saving::monthlyProc(){   //how to call calcInt to delete the following

  balance -= monthlyServiceFee;
  numWithdraws = 0;
  numDeposits = 0;
  monthlyServiceFee = 0;

  return balance;
}

void saving::print(){

  cout <<"current balance " <<balance << endl;
  cout << "Interest Rate "<<annual_Interest << endl;
  cout << "number of deposits " << numDeposits << endl;
  cout << "number of withdraws " <<  numWithdraws << endl;
  cout << "monthlyServiceFee "  << monthlyServiceFee << endl;
}


accountInfo::~accountInfo(){}


int main(){
  double current_balance = 0;
  double current_interest = 0;
  double money, money0;
//accountInfo obj0;
accountInfo obj = accountInfo(100,100);

char option;

while(option != 'Q'){
cout << "Select action" << endl;
cin >> option;
option = toupper(option);
saving obj1(current_balance,current_interest); 
switch(option){

  case('A'):
  {
  cout << "Please input your balance and interest rate: " << endl;
  cin >> current_balance;
  cin >> current_interest;
  obj1 = saving(current_balance,current_interest);
  }
  break;

  case('D'):
  cout << "Money to deposit" << endl;
  cin >> money;
  obj1.deposit(money);
  break;

  case('W'):
  cout << "Money to withdraw" << endl;
  cin >> money0;
  obj1.withdraw(money0);
  break;

  case('P'):
  obj1.print();
  break;

  case ('Q'):
  option = 'Q';
  break;

}
}
  // cout << "the balance after interest is " <<obj.calcInt() << endl;
  // cout << "money after the monthly services " << obj.monthlyProc() << endl;
  return 0;
}


【问题讨论】:

  • 提示:使用 accountInfo::accountInfo(double addBalance, double intRate) : balance(addBalance), annual_Interest(intRate) { ... } 之类的构造函数。您的子类可能还应该调用父类构造函数:saving::saving(double addBalance, double intRate) : accountInfo(addBalance, intRate) { },从而避免重新实现它。
  • 您可能想了解一些关于initialization liststhe override keyword 的用法
  • 您可能还想了解virtual destructors。您缺少 accountInfo 函数的定义,所以也许您真的想要 pure virtual 函数?
  • 我认为这对code review 来说可能是一个很好的问题(当然,在你修复了链接器错误之后)。

标签: c++ class derived-class


【解决方案1】:

如果你曾经有过课程:

class Base {
    public:
        Base();
};

和其他一些派生类:

class Derived : public Base{
    public:
        Derived();
};

调用父类的构造函数的正确方法是这样的:

Derived::Derived() : Base() {
    // Some implementation
}

所以你在: 之后调用构造函数。所以在你的情况下,它可能看起来像这样:

saving::saving(double addBalance, double intRate) : accountInfo(addBalance, intRate) {
    // Some implementation
}

这将在saving() 被构造时调用accountInfo() 的构造函数。

【讨论】:

  • 基类构造函数需要是公共的、受保护的或者派生类是友元才能访问基类的构造函数。
  • 好收获。谢谢。固定。
猜你喜欢
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
相关资源
最近更新 更多