Inheritance + Polymorphism

To start off, I thank everyone in advance who tries to help me. I have not been understanding the whole concept of polymorphism and inheritance well. Or rather I have, but all of the examples I have been able to find have shown a single file. I need to write a program with 9 files: 4 header files with 4 implementation files and one Driver. There will be one base class (Account) and 2 subclasses (Checking,Saving). The last of the four classes is Customer; Account has a Customer data member and uses it in some of its functions. The process of what is supposed to happen is self-explanatory based on the header file.

Here are the files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Account.h
#pragma once
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "customer.h"
class Account
{
public:
	Account();
	Account(float, Customer);
	Customer cust;
	float balance;
	virtual void makeDeposit(float);
	bool makeWithdrawal(float);
	float getBalance();
	virtual void view();
	virtual void adjustBalance();
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Account.cpp
#include "account.h"
#include "customer.h"
#include <iostream>
using namespace std;

Account::Account()
{
	balance=0;
	Customer::Customer();
}
Account::Account(float theBalance, Customer aCustomer)
{
	balance=theBalance;
}
void Account::view()
{
	Customer theCustomer;
	theCustomer.view();
	cout<<balance<<endl;
}
float Account::getBalance()
{
	return balance;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//savings.h
#pragma once
#ifndef SAVINGS_H
#define SAVINGS_H
#include "customer.h"
#include "account.h"
class Savings : public Account
{
	float interestRate;
	Savings();
	Savings(Customer,double, double);
	void makeDeposit(float);
	void adjustBalance();
	void view();
};
#endif 


//Savings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "savings.h"
#include "customer.h"

Savings::Savings()
{
	
	interestRate=0;
}
Savings::Savings (Customer d,double d2, double d3):Account(d2,d)
{
	interestRate=d3;
}
void Savings::makeDeposit(float a)
{
	Account:makeDeposit(a);
}
void Savings::adjustBalance()
{

}
void Savings::view()
{

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Checking.h
#pragma once
#ifndef CHECKING_H
#define CHECKING_H
class Checking : public Account
{
public:
	bool overdraftProtection;
	Checking();
	Checking(Customer,double,bool);
	void makeDeposit(float);
	void adjustBalance();
	void view();
}
#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Checking.cpp
#include "account.h"
#include "checking.h"
#include "customer.h"
#include "savings.h"
Checking::Checking()
{

}
void Checking::makeDeposit(float a)
{
	Account:makeDeposit(a);
}
void Checking::adjustBalance()
{

}
void Checking::view()
{
	
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Customer.h
#pragma once
#ifndef CUSTOMER_H
#define CUSTOMER_H

class Customer
{
public:
	char accountID[5];
	char name[20];
	Customer();
	Customer(char[],char[]);
	void view();
	Customer& operator =(const Customer&);

};
#endif 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//customer.cpp
#include"customer.h"
#include <cstring>
#include <iostream>
using namespace std;
Customer::Customer()
{
	for(int x=0;x<5;x++)
	{
		accountID[x]=NULL;
	}
	for(int x=0;x<20;x++)
	{
		name[x]=NULL;
	}
}
Customer::Customer(char ID[5],char alias[20])
{
	for(int x=0;x<5;x++)
	{
		accountID[x]=ID[x];
	}
	for(int x=0;x<5;x++)
	{
		name[x]=alias[x];
	}
}
void Customer::view()
{
	for(int x=0;name[x]!=NULL;x++)
	{
		cout<<name[x];
	}
	cout<<endl;
	for(int x=0;accountID[x]!=NULL;x++)
	{
		cout<<accountID[x];
	}
}
Customer& Customer::operator =(const Customer& obj)
{
  
 strcpy(this->accountID, obj.accountID); //OR: strcpy(accountID, obj.accountID);
 strcpy(this->name, obj.name); //OR: strcpy(custName, obj.custName);


 return(*this);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//test.cpp

#include <iostream>
using namespace std;
#include "account.h"
#include "checking.h"
#include "savings.h"
const int MAX = 4;
void doTransactions (Account*);
const int NAME_SIZE=20;
const int ID_SIZE=20;
int main()
{          
            Account* acctsPtr [MAX];
            char acctType;
            bool validType = true;
            char custName[NAME_SIZE];
            char acctID [ID_SIZE];
            double startBal;
 
            int aNum;
            for (aNum = 0; aNum < MAX && validType; aNum++)
            {
                        cout << "Enter c for checking; s for savings; any other character to quit: ";
                        cin >> acctType;
                        acctType = tolower (acctType);
 
                        if (acctType == 'c' || acctType == 's')
                        {
                                                cout << "Enter customer name: ";
                                                cin >> custName;
                                                cout << "Enter account number: ";
                                                cin >> acctID;
                                                cout << "Enter account beginning balance: ";
                                                cin >> startBal;
 
                        }
 
                        switch (acctType)
                        {
                                    case 'c':
                                    {          
                                                char response;
                                                bool overdraftOk;
                                                cout << "Does this account have overdraft protection? ";
                                                cin >> response;
                                                overdraftOk = (tolower(response) == 'y')? true : false;
                                                Customer c (custName, acctID);
                                                acctsPtr[aNum] = new Checking (c, startBal, overdraftOk);
                                                doTransactions (acctsPtr[aNum]);
                                                acctsPtr[aNum]->adjustBalance();
                                                cout << "Balance after service charge (if any): " <<
                                                              acctsPtr[aNum]->getBalance() << endl;
                                                break;
                                    }
                                   
                                    case 's':
                                    {
                                                double intRate;
                                                cout << "Enter current monthly interest rate: ";
                                                cin >> intRate;
                                                Customer c (custName, acctID);
                                                acctsPtr[aNum] = new Savings (c, startBal, intRate);
                                                doTransactions (acctsPtr[aNum]);
                                                acctsPtr[aNum]->adjustBalance();
                                                cout << "Balance after interest: " <<
                                                              acctsPtr[aNum]->getBalance() << endl;
                                                break;
                                    }
 
                                    default:
                                                validType = false;
                                                aNum--;
                                                break;
                        }
            }
 
            int totalAccts = aNum;
            cout << "\nAccounts:\n";
            for (int i = 0; i < totalAccts; i++)
            {
                        acctsPtr[i]->view();
                        cout << "\n";
            }
 
            for (int i = 0; i < totalAccts; i++)
                        delete acctsPtr[i];
}
 
void doTransactions (Account * aPtr)
{
            double depAmt, withdAmt;
 
            cout << "Enter total deposits: ";
            cin >> depAmt;                    
            aPtr->makeDeposit (depAmt);
            cout << "Balance after deposits: "
                         << aPtr->getBalance() << endl;   
           
            cout << "Enter total withdrawals: ";
            cin >> withdAmt;
            if (aPtr->makeWithdrawal (withdAmt))
                        cout << "Balance after withdrawals: "
                                    << aPtr->getBalance() << endl;
            else
                        cout << "Withdrawal not made -- balance too low\n"
                                     << " and no overdraft protection\n";
}
Hi,
What is your question ?
Multiple files are just like single file with the exception that it is a bit tougher for someone online to just copy, paste and compile your code.

Here are a couple of rules:
1. Declarations and template/inline functions go in header files
2. Other class member functions go in source files
3. Include base headers at the top of child headers.

I'm not sure what your problem is, but try including Account.h at the top of customer.h.

Why? Because for the customer class to work, it NEEDS to know about the account class.
What wait...

The you have customer derived from account and you have a customer member inside of account. That's sort of a loop which isn't going to work out easily.
Customer isn't derived from account. However, a customer holding an account and an account holding a customer (which IS what he's doing) is only possible if you make that a pointer type (this shouldn't compile at all, I believe C++ compilers are single pass).
Topic archived. No new replies allowed.