...

View Full Version : Inheriting friend functions (need reason behind an error)



Reaga
04-01-2013, 01:41 AM
University work. In our previous chapter, we covered Inheritance, and the project for that chapter went off without a hitch. This project is supposed to demonstrate use of a template function. I have the template part down, but for some reason the subclasses are having a problem with the extraction operator.

Here's the code (note: any redundancies in the main are a result of my professor designing the skeleton for the project)


#include<iostream>
#include<string>
using namespace std;
#include <stdlib.h>
template <class T>
void produceReport(string title, T data[], int rows)
{
cout << '"' << title << '"' << endl;
for (int i=0; i<rows; i++)
{
cout << data[i] << endl;
for (int j=0; j<sizeof(data[i]), j++)
cout << "-";
cout << endl;
}
cout << "End of Report." << endl;
}


class BankAccount
{
friend ostream& operator<<(ostream&, BankAccount);
friend istream& operator>>(istream&, BankAccount&);
protected:
int acctNum;
double balance;
public:
BankAccount(int, double = 0.0);
BankAccount();
/*void enterAccountData();
void displayAccount();*/
BankAccount operator+=(double);
BankAccount operator-=(double);
BankAccount operator+(int);
bool operator<(BankAccount);
bool operator>(BankAccount);
bool operator==(BankAccount);
};
BankAccount::BankAccount(int acct, double bal)
{
acctNum=acct;
balance=bal;
}
BankAccount::BankAccount()
{
acctNum=1000;
balance=0.0;
}
/*void BankAccount::enterAccountData()
{
// write the code
}
void BankAccount::displayAccount()
{
// write the code
}*/
BankAccount BankAccount::operator+=(double deposit)
{
balance=balance+deposit;
}
BankAccount BankAccount::operator-=(double withdrawal)
{
balance=balance-withdrawal;
}
BankAccount BankAccount::operator+(int num)
{
acctNum=acctNum+num;
}
bool BankAccount::operator>(BankAccount account)
{
if (balance>account.balance)
return true;
else
return false;
}
bool BankAccount::operator<(BankAccount account)
{
if (balance<account.balance)
return true;
else
return false;
}
bool BankAccount::operator==(BankAccount account)
{
if (balance==account.balance)
return true;
else
return false;
}
ostream& operator<<(ostream& out, BankAccount account)
{
out << "Bank Account #" << account.acctNum << " has a balance of $" << account.balance << "." << endl;
return out;
}
istream& operator >> (istream& in, BankAccount& account)
{
cout << endl;
cout << "Enter account number." << endl;
in >> account.acctNum;
cout << "Enter the account's balance." << endl;
in >> account.balance;
return in;
}
class SavingsAccount : virtual public BankAccount
{
friend ostream& operator << (ostream&, SavingsAccount);
friend istream& operator>>(istream&, SavingsAccount&);
private:
double interestRate;
public:
SavingsAccount(int, double, double);
SavingsAccount(double);
SavingsAccount();
};
SavingsAccount::SavingsAccount(int acct, double bal, double rate) : BankAccount (acct, bal)
{
interestRate=rate;
}
SavingsAccount::SavingsAccount(double interest) : BankAccount (1000, 0.0)
{
interestRate=interest;
}
SavingsAccount::SavingsAccount():BankAccount(1000, 0.0)
{
interestRate=0.08;
}

ostream& operator<<(ostream& out, SavingsAccount acct)
{
out << "Bank Account #" << acct.acctNum << " has a balance of $" << acct.balance << "." << endl;
out << "Its interest rate is " << acct.interestRate*100 << "% per year." << endl;
return out;
}
istream& operator>>(istream& in, SavingsAccount acct)
{
cout << endl;
cout << "Enter the account number." << endl;
in >> acct.acctNum;
cout << "Enter the account balance." << endl;
in >> acct.balance;
cout << "Enter the interest rate. Example: 8% is entered as 0.08." << endl;
in >> acct.interestRate;
return in;
}
class CheckingAccount: virtual public BankAccount
{
friend ostream& operator<<(ostream&, CheckingAccount);
friend istream& operator>>(istream&, CheckingAccount&);
private:
double fee;
int numChecks;
public:
CheckingAccount(int, double, double, int);
CheckingAccount();
};
CheckingAccount::CheckingAccount(int acct, double bal, double cost, int checks):BankAccount(acct, bal)
{
fee=cost;
numChecks=checks;
}
CheckingAccount::CheckingAccount():BankAccount(1000, 0.0)
{
fee=3.00;
numChecks=150;
}
ostream& operator<<(ostream& out, CheckingAccount acct)
{
out << "Bank Account #" << acct.acctNum << " has a balance of $" << acct.balance << "." << endl;
out << "Its fee is $" << acct.fee << " and has " << acct.numChecks << " checks available." << endl;
return out;
}
istream& operator>>(istream& in, CheckingAccount acct)
{
cout << endl;
cout << "Enter the account number." << endl;
in >> acct.acctNum;
cout << "Enter the account balance." << endl;
in >> acct.balance;
cout << "Enter the available checks." << endl;
in >> acct.numChecks;
cout << "Enter account fee." << endl;
in >> acct.fee;
return in;
}
class CheckingWithInterest : public CheckingAccount, public SavingsAccount
{
friend ostream& operator<<(ostream&, CheckingWithInterest);
friend istream& operator>>(istream&, CheckingWithInterest&);
public:
CheckingWithInterest();
};
CheckingWithInterest::CheckingWithInterest() : SavingsAccount(0.02), CheckingAccount(1000, 0.0, 3.0, 150)
{
}
ostream& operator << (ostream& out, CheckingWithInterest cwi)
{
out << "Bank Account #" << cwi.acctNum << " has a balance of $" << cwi.balance << "." << endl;
out << "Its monthly fee is $" << cwi.fee << ", has " << cwi.numChecks << "checks available, and gains interest at a rate of " << cwi.interestRate*100 << "% per year." << endl;
return out;
}
istream& operator >> (istream& in, CheckingWithInterest& cwi)
{
cout << endl;
cout << "Enter the account number." << endl;
in >> cwi.acctNum;
cout << "Enter the account balance." << endl;
in >> cwi.balance;
cout << "Enter the available checks." << endl;
in >> cwi.numChecks;
cout << "Enter account fee." << endl;
in >> cwi.fee;
cout << "Enter the interest rate. Example: 8% is entered as 0.08." << endl;
in >> cwi.interestRate;
return in;
}

int main()
{
const int NUM = 5;
cout << "Checking with Interest" << endl;
CheckingWithInterest cwi[NUM];
for (int i=0; i<4; i++)
{
cin >> cwi[i];
}
cout << "Checking" << endl;
CheckingAccount checking[NUM];
for (int i=0; i<4; i++)
{
cin >> checking[i];
}
cout << "Savings" << endl;
SavingsAccount savings[NUM];
for (int i=0; i<4; i++)
{
cin >> savings[i];
}
int num[NUM] = {1, 2, 3, 4, 5};
string names[NUM] = {"Allison", "Bob", "Charlie", "Danielle", "Elmer"};
produceReport("Checking Acounts With Interest:", cwi, NUM);
produceReport("Checking Acounts Without Interest:", checking, NUM);
produceReport("Savings Accounts:", savings, NUM);
produceReport("Integers:", num, NUM);
produceReport("Names:", names, NUM);
system ("PAUSE");
return 0;
}


Error console gives me 2 issues repeated:

With the overloaded << operators (in base classes only) I get:
no accessible path to protected member declared in virtual base 'BankAccount'

In the main, when using the extraction operators for SavingsAccount or CheckingAccount (not w/ CheckingWithInterest, funnily enough):
operator >> is ambiguous.

Can anyone tell me what I've missed?

Edit: I found out I missed a & in the definition of the extraction operator for Checking and Savings accounts, that fixed the ambiguity, but not the lack of access to data.
Edit again: Simple mistake, I forgot to put protected instead of private. I think I have this, so an admin can just delete/lock this thread, apparently it just allowed me to reflect on my own work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum