-
November 25th, 2012, 09:19 PM
#1
Struggling with 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:
Code:
#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:
Code:
#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
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
-
November 26th, 2012, 04:18 AM
#2
Re: Struggling with Friend classes in seperate header files
Yes, and?
Now, I see no reason whatsoever, the Person should access privates of the account. Can you explain why you want that?
-
November 26th, 2012, 12:33 PM
#3
Re: Struggling with Friend classes in seperate header files
Avoid 'friend' at all costs (a.k.a. never use it). Usually the 'friend'-option means your design is bad.
-
November 27th, 2012, 04:03 AM
#4
Re: Struggling with Friend classes in seperate header files
Originally Posted by Skizmo
Avoid 'friend' at all costs (a.k.a. never use it). Usually the 'friend'-option means your design is bad.
That's a pretty bad blanket statement there.
friend classes exist for a reason, and sometimes, the clean and good design does use friends where a forced approach avoiding friends at all costs will be awkward or make documentation awkward.
One of the good reasons to use friend is if you are making a library of classes to be used by others.
And some of the classes need to "talk to eachother", but this "talk" isn't actually part of the public interface for the classes.
friends solve this. the classes can access private data/members of eachother.
The alternative without friends is that you make all those special purpose members and data public. Which means you need documentation saying "don't ever use this function" or you don't document at all which will cause other issues. And it causes problems because sooner or later one of the consumers of your classes will have found "this neat trick" with calling/using one of those functions and now you're sort of forced into continuously supporting it.
friend classes mean stuff that's supposed to be private stays private for everyone but the friends. that's a good thing. But yes, as with many things, you can abuse it in wrong ways.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|