是否可以为派生类创建构造函数?

时间:2019-11-12 23:54:05

标签: c++ class derived-class

我试图调用派生类中的所有虚函数,然后在外部将它们定义为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;
}


1 个答案:

答案 0 :(得分:3)

如果您上过课:

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
}

在构建accountInfo()时,它将调用saving()的构造函数。