Friend classes in seperate header files

I am struggling to enable friendship between two classes in separate header files for a banking program.
I am attempting to get the Person class to use variables from the Account class. Heres what I have so far.

ACCOUNT.h:
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
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <string>
#include <math.h>
#include <windows.h>
#include <vector>
#include "Person.h"

using namespace std;

class person;

class account

{
int deposit;
friend class person;

public:
void dep(int);	//function to accept amount and add to balance amount
void draw(int);	//function to accept amount and subtract from balance amount
int retdeposit() const;	//function to return balance amount
friend class person;


};


PERSON.h:
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
#ifndef PERSON_H
#define PERSON_H


#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <string>
#include <math.h>
#include <windows.h>





using namespace std;



class person 
{
	int acno;
	char name[50];
	char add[50];
	char dob[10];
	char type[30];
	char tele[12];
	char mort;
	char mortgage;
	double Principle;
	double initialPrinciple;
	double yearlyInterest;
	double initialYearlyInterest;
	double monthlyInterest;
	double initialMonthlyInterest;
	double monthlyPayment;
	double initialMonthlyPayment;
	int lengthInYears;
	float lengthInYears2;
	double initialLengthinYears;
	int lengthInMonths;
	int initialLengthInMonths;
	
	

	
	
public:
	void create_account();	//function to get data from user
	void show_account() const;	//function to show data on screen
	void modify();	//function to add new data
	void report() const;	//function to show data in tabular format
	int retacno() const;	//function to return account number
	char rettype() const;	//function to return type of account
	
	
	#endif	// PERSON_H
	
};      //class ends here
The #endif should be placed at the end of the file. Not inside the class definition. You don't need two identical lines friend class person;.
Shouldn't the financial stuff (Principle etc) be part of the account class?

Another thing to do, is have a CBank class, which manages accounts & people, rather than have this in the person class.

IMO, the CPerson & CAccount classes, just store info related to these things respectively - like a record in a database. Then provide a minimal amount of functions to interact with these, but most of the work is done by the CBank class. That is create new accounts & people, find accounts, delete accounts etc.

You might also need a transaction class.

What I am saying is think about how things happen in the real world, to help design your classes. Class design can be quite tricky sometimes.

Here is some code I did to help someone else.

http://www.cplusplus.com/forum/beginner/76482/4/#msg412946


It is not complete & there is a red herring (forcing some thought on your part).

You will have to extend it a bit to have different types of accounts & transactions.

Hopefully this will give you a better idea of what goes in each class.
Last edited on
1
2
3
4
5
CAccount* CAccount::getDetails() {
	return this;	//returns a pointer to this CAccount object
					//you can then use the pointer to access the member
					//variables
}
¿what's the point with that?


There may be a relationship between an Account and a Person.
Like `a person has many accounts'


void person::create_account(); //function to get data from user ¿how is that supposed to work?
char person::rettype() const; //function to return type of account ¿which account?
Good work ne555 - you found the red herring!!!
Topic archived. No new replies allowed.