Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Need a bit of guidance on initializing class objects.
New Posts  All Forums:Forum Nav:

Need a bit of guidance on initializing class objects.

post #1 of 2
Thread Starter 
Excuse the messy code (in some areas, it is from planning)...
Okay... I have this: Main.cpp (Click to show)
#include
#include "BankAccounts.h"
using namespace std;

int main()
{
int IDnum, keyedID = 0, userinterface = 0;
double transfer = 0;
char firstname;
double cb, sb;


/*
while (keyedID = 0)
{cout << "Please enter your account number: ";
cin >> keyedID;
if (keyedID = 12)
(userinterface = 1);}

if (userinterface = 1)
();
*/

return 0;

}

This: BankAccounts.h (Click to show)
#ifndef BankAccounts_H
#define BankAccounts_H
#include
using namespace std;

class CheckingAccount
{
private:
double CheckingBalance;
double MonthlyFee;

public:
double getBalance() const;
double getMonthlyFee() const;
void addMoney();
void subtractMoney();

CheckingAccount(double cb, double mf)
{ CheckingBalance = cb;
MonthlyFee = 5;}
};
class SavingAccount
{
private:
double savingbalance;
double interestrate;
double minimumreq;

public:
double getbalance() const;
double getinterestrate() const;
double getminimumreq() const;
void addmoney();
void subtractmoney();
SavingAccount(double sb, double ir, double mr)
{ savingbalance = sb;
interestrate= .05;
minimumreq = 200;}
};

class UserAccount: public CheckingAccount, public SavingAccount
{
private:
int ID;
char Name;
public:
char getName() const;
int getID() const;
void MoneyTransfer();
UserAccount(int ID, char Name)
{ Name = firstname;
ID = idnum;}
};



#endif
And this: BankAccounts.cpp (Click to show)
#include
#include "BankAccounts.h"

//UserAccount definition section.
char UserAccount::getName()
{return Name;}
int UserAccount::getID()
{return ID;}

//CheckingAccount definition section.
double CheckingAccount::getBalance()
{return CheckingBalance;}
double CheckingAccount::getMonthlyFee()
{return MonthlyFee;}
void CheckingAccount::addMoney()
{CheckingBalance += transfer;}
void CheckingAccount::subtractMoney()
{CheckingBalance += transfer;}
CheckingAccount::CheckingAccount

//SavingAccount definition section.
double SavingAccount::getbalance()
{return savingbalance;}
double SavingAccount::getinterestrate()
{return interestrate;}
double SavingAccount::getminimumreq()
{return minimumreq;}
void addmoney();
void subtractmoney();

I am having trouble with the initilization of my classes. I need to initilize the CheckingAccount's monthly fee and checking balance. i will need to do similiar iwth SavingAccount and UserAccount, but on those, I need to be able to initialize several different accounts. Because we aren't required to have the user set up an account, i need to have it in the code to make a few accounts to start off with.

Any advice, or explanation of the initializaiton would be appreciated.

Edit: I just realized my messy code made a smiley face. Just ignore that.[/SPOILER]
Edited by ./Cy4n1d3\. - 5/6/12 at 11:07pm
post #2 of 2
I know this post is a week old, but I'm trollin' for brain exercises to get ready for summer semester. I won't write a whole lot in case this issue has been abandoned.

Should you have default constructors for this situation? You know, constructors for CheckingAccount and SavingAccount that don't take any parameters. In it, you'd set the class variables to default values; maybe 0 for your case (as most of this banking stuff starts at 0 when the account is created).

I'm pretty sure the class that inherits those 2 main classes will call their default constructors by uh... default. It needs to be there iirc (haven't messed with inherited classes for a few months now)

I don't see in your main that you ever create class objects. If you set your default constructors and then declare class objects within main, it should initialize.

EDIT: also, consider destructors. When main terminates one of the class objects that had inheritance, I'm not sure it calls the destructors for the original class(es) without there being a destructor ( ~className( ) ) set up by the programmer as part of the class. Thats not an issue with non-inherited classes though.
Edited by Dactyl - 5/19/12 at 7:31am
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Need a bit of guidance on initializing class objects.