The following code has three classes. account
, savings
(derived), current
(derived). But three of the memberfunctions from the base class are not getting inherited. This is the error what I get. Is it possible doing without using virtual functions
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
When you do
void savingsAccount:: display(){/* rest here */}
you are telling the compiler to implement the definition of a member function called savingsAccount::display()
. However you don't specify that you have this member function in your derived class declarations. Since you overloaded it, you need to add its signature to the derived classes , like
class savingsAccount: public account
{
/* virtual */ void display() /* override */; // probably you want this virtual in the base class
// the rest
};
otherwise the compiler only knows that there exists a base version, ie account::display()
. So the compiler is indeed right telling you that there is no savingsAccount::display()
member function, as you only have account::display()
, ie the member function of the base class.
Your whole issue can be simplified to
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(){}
PS: You probably want to make those functions virtual
too (and also add const
to the ones that don't modify your member variables), so you have proper overriding, otherwise your derived member won't behave as you want when you have a class hierarchy controlled by a pointer-to-base.
PSS: put
#ifndef classes_h
#define classes_h
at the very beginning of your header, before any #include
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.