Virtual method problems

Hello,

I have a program that makes different types of accounts (One Account class, and other class inherit from it) , and then my main class goes through a vector of each account and calls its "printInfo" method.

My problem, is that it's only ever calling the virtual method in the class Account, and not in any of its sub-classes that inherit from it.

Here is my code (Shortend so you can see the relative parts;


Account.h class
1
2
Account(double amount, string accType, int accNo, Customer *acOwner, double overdraft, double intrate);
virtual void printInfo();


Account.cpp class

1
2
3
4
5
6
7
8
9
10
11
  void Account::printInfo() 
  {
	cout << "----------------------------------------------------\n";
	cout << "Account Number: " << accountNumber << "\n";
	cout << "Account Type: " << accountType << "\n";
	cout << "Account Balance: " <<  balance << "\n";
	cout << "Account Owned By: " <<  getOwner() << "\n";
	cout << "Account Interest Rate: " <<  getInterest() << "\n";
	cout << "Account Overdraft Limit: " <<  getOverdraft() << "\n";
	cout << "----------------------------------------------------\n";
  }


CorporateSavings.h class

class CorporateSavings: public Account

CorporateSavings(double amount, string accType, int accNo, Customer *acOwner, string businessOwner, double overdraft, double intrate);

void printInfo();


CorporateSavings.cpp class

1
2
3
4
5
6
7
8
9
10
  void CorporateSavings::printInfo() {
	cout << "----------------------------------------------------\n";
	cout << "Account Number: " << accountNumber << "\n";
	cout << "Account Type: " << accountType << "\n" ;
	cout << "Account Balance: " <<  balance << "\n" ;
	cout << "Account Owned By: " <<  getOwner() << "\n" ;
	cout << "Account Interest Rate: " <<  getInterest() << "\n";
	cout << "Account Overdraft Limit: " <<  getOverdraft() << "\n";
	cout << "----------------------------------------------------\n";
  }


Main class

vector<Account> account;

1
2
CorporateSavings stdAccount(balance, accountType, accountNumber, owner, business, overdraftAmount, intrate);
								account.push_back(stdAccount);




1
2
3
4
5
if (accountNumber == account[i].getAccountNumber())
{
account[i].printInfo();
found = true;
}



The output I get is always the same, because it's always Account's printInfo method, when it should be CorporateSavings printInfo method.

Can anyone see why the virtual method is not working?

Thanks in advance

Last edited on
In the vector you store Account objects so they can't be any other type. You can store pointers in the vector instead and it should work beter vector<Account*> account;
Thanks for the help!

Could you suggest a way for me to store pointers to each account? I understand the basics of pointers, but I'm sure of the best way to go about doing this.

Maybe I should have a vector for <Account*> and one for <Account>? Would I need to do that?

Could I just creata a pointer to an Account, create the account, then make that pointer point to it, then store it in the vector? Would I need to give each account I create a different name as they would over write themselves otherwise?

Cheers
Topic archived. No new replies allowed.