成员函数没有在c ++

时间:2016-09-18 00:06:21

标签: c++ inheritance

以下代码有三个类。 accountsavings(派生),current(派生)。但是基类中的三个成员函数没有被继承。这是我得到的错误。是否可以不使用虚函数

In file included from main.cpp:1:0:
classes.hpp:96:30: error: no ‘void savingsAccount::deposit()’ member function declared in class ‘savingsAccount’
 void savingsAccount::deposit()
                              ^
classes.hpp:105:31: error: no ‘void savingsAccount::withdraw()’ member function declared in class ‘savingsAccount’
 void savingsAccount::withdraw()
                               ^
classes.hpp:130:31: error: no ‘void savingsAccount::display()’ member function declared in class ‘savingsAccount’
 void savingsAccount:: display()
                               ^
classes.hpp:181:30: error: no ‘void currentAccount::deposit()’ member function declared in class ‘currentAccount’
 void currentAccount::deposit()
                              ^
classes.hpp:190:31: error: no ‘void currentAccount::withdraw()’ member function declared in class ‘currentAccount’
 void currentAccount::withdraw()
                               ^
classes.hpp:220:31: error: no ‘void currentAccount::display()’ member function declared in class ‘currentAccount’
 void currentAccount:: display()
#include<iostream>
#include<cstring>
#define MAX 50

#ifndef classes_h
#define classes_h

using namespace std;
class account
{
    protected:
        char name[MAX];
        int accountNumber;
        char type[MAX];
        float balance;
        float minBalance;
        static int Customers;   
    public:
        account();  
        account(char*,int);

        void deposit();     
         void withdraw();   
         void display();
         int getAccountNumber();
        static void numberOfCustomers();

};

account::account(char* name, int accountNumber) 
{
    strcpy(this->name,name);
    this->accountNumber=accountNumber;
    //strcpy(this->type,type);
    this->balance=0;
    Customers++;
}

int account::Customers=0;
void account ::numberOfCustomers()
{
    cout<<"Total number of customer-------->"<<Customers;
}

void account::display()
{

}

/********************************
//Savings Account class
********************************/

class savingsAccount: public account
{
    protected:
        float minBalance; 
    //  float rate;
    public:
        savingsAccount();
        savingsAccount(char*,int);
        savingsAccount(account&);   //Copy Constructor
/*      void deposit();     //user
        void withdraw();    //user
        void display();     //user
*/      int getAccountNumber();
};



savingsAccount::savingsAccount(char* name, int accountNumber):account(name,accountNumber)
{
    minBalance=0;
    strcpy(type,"savings");
}


savingsAccount::savingsAccount(account& tp):account(tp)
{
    minBalance=0;   
    strcpy(type,"savings");
}



int savingsAccount::getAccountNumber()
{
    return accountNumber;
}       




void savingsAccount::deposit()
{
    float amount;
    cout<<"Enter the amount needs to be deposited"<<endl;
    cin >> amount;
    balance=balance+amount;
}


void savingsAccount::withdraw()
{
    float amount ; 
    if(balance ==0)
    {
        cout<<"Account balance is Nil"<<endl;
        return;
    }
    cout<<"Enter the amount yout would like to withdraw"<<endl;
    while(1)
    {
        cin>>amount;
        if(balance-amount<0)
        {
            cout<<"insufficient funds, try some less amount\n";

        }
        else
        {
            balance=balance-amount;
            return ;
        }
    }
}

void savingsAccount:: display()
{
    cout<<"Account Number"<<accountNumber<<endl;
    cout<<"Name-->"<<name<<endl;
    cout<<"Accounttype-->Savings"<<endl;
    cout<<"Balance-->"<<balance<<endl;
    cout<<"Minimum Balance -->"<<minBalance;

}

/***********************************
//Current Account class
************************************/
class currentAccount: public account
{
    protected:
        float minBalance;
    public:
        currentAccount();
        currentAccount(char*,int);
        currentAccount(account&);
/*      void deposit();
        void withdraw();
        void display();
*/      int getAccountNumber();

};

/*
currentAccount::currentAccount(char* name, int accountNumber):account((char*)name,accountNumber,"current account")
{
     minBalance=1000;
     balance=1000;
}
*/
currentAccount::currentAccount(char* name, int accountNumber):account(name,accountNumber)
{
    minBalance=0;
    strcpy(type,"Current");
}


currentAccount::currentAccount(account& tp):account(tp)
{
    minBalance=0;   
    strcpy(type,"Current");
}




void currentAccount::deposit()
{
    float amount;
    cout<<"Enter the amount needs to be deposited"<<endl;
    cin >> amount;
    balance=balance+amount;
}


void currentAccount::withdraw()
{
    float amount ; 
    if(balance ==0)
    {
        cout<<"Account balance is Nil"<<endl;
        return;
    }
    cout<<"Enter the amount yout would like to withdraw"<<endl;
    while(1)
    {
        cin>>amount;
        if(balance-amount<0)
        {
            cout<<"insufficient funds, try some less amount\n";

        }
        else
        {
            balance=balance-amount;
            return ;
        }
    }
    if(balance-amount<1000)
    {
        cout<<"Please keep the balance above minimum balance ";
    }

}

void currentAccount:: display()
{
    cout<<"Account Number"<<accountNumber<<endl;
    cout<<"Name-->"<<name<<endl;
    cout<<"Accounttype-->Current Account"<<endl;
    cout<<"Balance-->"<<balance<<endl;
    cout<<"Minimum Balance -->"<<minBalance;

}

int currentAccount::getAccountNumber()
{
    return accountNumber;
}       

#endif

1 个答案:

答案 0 :(得分:1)

当你这样做时

void savingsAccount:: display(){/* rest here */}

您告诉编译器实现名为savingsAccount::display()的成员函数的定义。但是,您没有在派生类声明中指定您具有此成员函数。由于您重载了它,需要将其签名添加到派生类,例如

class savingsAccount: public account
{
    /* virtual */ void display() /* override */; // probably you want this virtual in the base class
    // the rest
};

否则编译器只知道存在基本版本,即account::display()。所以编译器确实告诉你没有savingsAccount::display()成员函数,因为你只有account::display(),即基类的成员函数。

您的整个问题可以简化为

struct X
{
    void f();
};

struct Y: X
{
    // void f(); 
};

// error if you don't add the signature in Y,
// there is no Y::f(), only the X::f() inherited part
void Y::f(){} 

int main(){}

Live on Coliru

PS:您可能也希望将这些函数virtual(并且还将const添加到不修改您的成员变量的函数),因此您有适当的覆盖,否则您的派生成员当你有一个由指针到基类控制的类层次结构时,它将不会按你的需要运行。

PSS:放

#ifndef classes_h
#define classes_h

在标题的最开头,在任何#include之前。