Problem with Class Inheritance Assignment
Apr 12, 2010 at 6:42am UTC
Hi guys. There are two problems with my program. One is that my program exits automatically after the if statement is satisfied (e.g. it cannot work if my balance is 1500, as five.checkbalance(1000) would be true). So I can get the stuff in else to be outputted, but not in if. Another problem is that I always encounter an error whenever my program is complete. Please tell me what is wrong with my program!
Here is the main:
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
#include<iostream.h>
#include<stdlib.h>
#include"header.h"
int main ()
{
cout << "Fixed class testing:\n\n" ;
Fixed five;
five.s_balance(500);
five.s_interest(0.05);
if (five.checkbalance(1000)==true &&five.checkinterest(0.0325)==true )
{
cout << "Account can be started.\n" ;
five.display();
}
else
{
cout << "Insufficient balance or interest\n"
<< "Account cannot be started.\n" ;
}
cout << "Press return to continue. . ." ;
cin.get();
return 0;
}
And here are the methods:
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
Fixed::Fixed()
{
s_funds(0.0);
}
Fixed::Fixed(double f)
{
s_funds(f);
}
void Fixed::s_funds(double amt)
{
funds=amt;
}
double Fixed::g_funds()
{
return funds;
}
bool Fixed::checkinterest(double no)
{
if (g_interest()<no)
return false ;
else
return true ;
}
void Fixed::compound()
{
double interest;
interest=g_balance()*g_interest();
s_funds(interest);
}
double Fixed::available()
{
return g_balance()+g_funds();
}
void Fixed::display()
{
cout << "Account Particulars\n\n"
<< "\tname : " << g_name() << "\n"
<< "\tbalance : " << g_balance() << "\n"
<< "\tinterest : " << g_interest() << "\n"
<< "\tfunds : " << g_funds() << "\n"
<< "\tavailable : " << available() << "\n\n" ;
}
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
Savings::Savings(char *n, double r)
{
s_name(n);
s_interest(r);
}
Savings::~Savings()
{
delete name;
}
Savings & Savings::operator = (Savings& person)
{
this ->name=person.name;
this ->balance=person.balance;
this ->interestrate=person.interestrate;
return *this ;
}
double Savings::g_interest()
{
return interestrate;
}
void Savings::s_interest(double amt)
{
interestrate=amt;
}
double Savings::compound()
{
double original;
original=g_balance();
s_balance(original+original*interestrate);
return original*interestrate-original;
}
bool Savings::checkbalance(double no)
{
if (g_balance()<no)
return false ;
else
return true ;
}
void Savings::display()
{
cout << "Account Particulars\n\n"
<< "\tname : " << g_name() << "\n"
<< "\tbalance : " << g_balance() << "\n"
<< "\tinterest rate : " << interestrate*100 << "%\n\n" ;
}
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
Account::Account(char *n, double b, double ol)
{
s_name(n);
s_balance(b);
s_overDraft(ol);
}
Account::Account (Account & acc)
{
s_balance(acc.g_balance());
s_overDraft(acc.g_overDraft());
}
Account::~Account()
{
delete name;
}
void Account::s_name(char * n)
{
name = new char [strlen(n)+1];
if (!name)
{
cout << "Account:: s_name ( ) -Not enough memory! \n " ;
exit(10) ;
}
strcpy(name, n) ;
}
void Account::s_balance(double amt)
{
balance=amt;
}
void Account::s_overDraft(double amt)
{
if ( amt > 0.0 )
overDraft=amt;
else
overDraft=0.0;
}
char * Account::g_name()
{
if (name)
return name;
else
return "nil" ;
}
double Account::g_balance()
{
return balance;
}
double Account::g_overDraft()
{
return overDraft;
}
void Account::display()
{
cout << "Account Particulars\n\n"
<< "\tname : " << g_name() << "\n"
<< "\tbalance : " << g_balance() << "\n"
<< "\toverdraft : " << g_overDraft() << "\n\n" ;
}
void Account::credit (double amt)
{
s_balance(g_balance() + amt);
}
bool Account::debit (double amt)
{
if ( (g_balance()+g_overDraft() ) > amt)
{
s_balance(g_balance() -amt);
return true ;
}
else
return false ;
}
Account & Account::operator = (Account & acc)
{
s_name(acc.g_name());
s_balance(acc.g_balance());
s_overDraft(acc.g_overDraft()) ;
return *this ;
}
Account Account::operator + (double amt)
{
s_balance(g_balance() + amt);
return *this ;
}
Account operator + (double amt, Account &acc)
{
return acc + amt;
}
The header file:
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
#ifndef HEADER_H
#define HEADER_H
class Account
{
protected :
char *name;
double balance;
double overDraft;
public :
//Account();
Account(char *n, double b=0.0 , double ol=0.0);
Account(Account &);
~Account();
char *g_name(); void s_name(char *);
double g_balance(); void s_balance(double );
double g_overDraft(); void s_overDraft(double );
void display();
void credit(double );
bool debit(double );
Account & operator = (Account &);
Account operator + (double );
friend Account operator + (double , Account &);
};
class Savings:public Account
{
protected :
double interestrate;
public :
//Savings();
Savings(char *n, double );
~Savings();
Savings & operator = (Savings&);
double g_interest(); void s_interest(double );
double compound();
bool checkbalance(double );
void display();
};
class Fixed:public Savings
{
protected :
double funds;
public :
Fixed();
Fixed(double );
double g_funds(); void s_funds(double );
bool checkinterest(double );
void compound();
//void debit();
double available();
void display();
};
#endif
Topic archived. No new replies allowed.