answersLogoWhite

0

// ASSIGNMENT 3.cpp : Defines the entry point for the console application.

//

#include

"stdafx.h"

#include

"conio.h"

#include

<iostream>

using

std::fixed;

using

namespace std;

class

Account

{

public: Account( double ); // constructor initializes balance

void

credit( double ); // add an amount to the account balance bool debit( double ); // subtract an amount from the account balance

void setBalance( double ); // sets the account balance

double getBalance(); // return the account balance

private: double balance;

// data member that stores the balance

// Account constructor initializes data member balance

Account::Account(

double initialBalance )

{

// if initialBalance is greater than or equal to 0.0, set this value

// as the balance of the Account

if ( initialBalance >= 0.0 )

balance = initialBalance;

else // otherwise, output message and set balance to 0.0

{

cout <<

"Error: Initial balance cannot be negative." << endl;

balance = 0.0;

}

// end if...else

}

// end Account constructor

// credit (add) an amount to the account balance

void Account::credit( double amount )

{

balance = balance + amount;

// add amount to balance

}

// end function credit

// debit (subtract) an amount from the account balance

// return bool indicating whether money was debited

bool Account::debit( double amount )

{

if ( amount > balance ) // debit amount exceeds balance

{

cout <<

"Debit amount exceeded account balance." << endl;

return

false;

}

// end if

else // debit amount does not exceed balance

{

balance = balance - amount;

return true;

}

// end else

}

// end function debit

// set the account balance

void Account::setBalance( double newBalance )

{ balance = newBalance;

}

// end function setBalance

// return the account balance

double Account::getBalance()

{

return balance;

}

// end function getBalance

};

// end class Account

class

SavingsAccount : public Account

{

public:

// constructor initializes balance and interest rate

SavingsAccount(

double, double );

double calculateInterest(); // determine interest owed

private:

double interestRate;

SavingsAccount::SavingsAccount(

double initialBalance, double rate ) : Account( initialBalance ) // initialize base class

{

interestRate = ( rate < 0.0 ) ? 0.0 : rate;

// set interestRate

}

// end SavingsAccount constructor

// return the amount of interest earned

double SavingsAccount::calculateInterest()

{

return getBalance() * interestRate;

}

// end function calculateInterest // interest rate (percentage) earned by account

};

// end class SavingsAccount

class

CheckingAccount : public Account

{

public:

// constructor initializes balance and transaction fee

CheckingAccount(

double, double );

void

credit( double ); // redefined credit function

bool debit( double ); // redefined debit function

private:

double transactionFee; // fee charged per transaction

// utility function to charge fee

void

chargeFee();

CheckingAccount::CheckingAccount(

double initialBalance, double fee )

: Account( initialBalance )

// initialize base class

{

transactionFee = ( fee < 0.0 ) ? 0.0 : fee;

// set transaction fee

}

// end CheckingAccount constructor

// credit (add) an amount to the account balance and charge fee

void CheckingAccount::credit( double amount )

{

Account::credit( amount );

// always succeeds

chargeFee();

}

// end function credit

// debit (subtract) an amount from the account balance and charge fee

bool CheckingAccount::debit( double amount )

{

bool success = Account::debit( amount ); // attempt to debit

if

( success ) // if money was debited, charge fee and return true

{

chargeFee();

return

true;

}

// end if

else // otherwise, do not charge fee and return false

return false;

}

// end function debit

// subtract transaction fee

void CheckingAccount::chargeFee()

{

Account::setBalance( getBalance() - transactionFee );

cout <<

"$" << transactionFee << " transaction fee charged." << endl;

}

// end function chargeFee

};

// end class CheckingAccount

int

_tmain(int argc, _TCHAR* argv[])

{

Account account1( 50.0 );

// create Account object

SavingsAccount account2( 25.0, .03 );

// create SavingsAccount object

CheckingAccount account3( 80.0, 1.0 );

// create CheckingAccount object

cout << fixed << setprecision ( 2 );

// display initial balance of each object

cout <<

"account1 balance: $" << account1.getBalance() << endl;

cout <<

"account2 balance: $" << account2.getBalance() << endl;

cout <<

"account3 balance: $" << account3.getBalance() << endl;

cout <<

"\nAttempting to debit $25.00 from account1." << endl;

account1.debit( 25.0 );

// try to debit $25.00 from account1

cout <<

"\nAttempting to debit $30.00 from account2." << endl;

account2.debit( 30.0 );

// try to debit $30.00 from account2

cout <<

"\nAttempting to debit $40.00 from account3." << endl;

account3.debit( 40.0 );

// try to debit $40.00 from account3

// display balances

cout <<

"\naccount1 balance: $" << account1.getBalance() << endl;

cout <<

"account2 balance: $" << account2.getBalance() << endl;

cout <<

"account3 balance: $" << account3.getBalance() << endl;

cout <<

"\nCrediting $40.00 to account1." << endl;

account1.credit( 40.0 );

// credit $40.00 to account1

cout <<

"\nCrediting $65.00 to account2." << endl;

account2.credit( 65.0 );

// credit $65.00 to account2

cout <<

"\nCrediting $20.00 to account3." << endl;

account3.credit( 20.0 );

// credit $20.00 to account3

// display balances

cout <<

"\naccount1 balance: $" << account1.getBalance() << endl;

cout <<

"account2 balance: $" << account2.getBalance() << endl;

cout <<

"account3 balance: $" << account3.getBalance() << endl;

// add interest to SavingsAccount object account2

double interestEarned = account2.calculateInterest();

cout <<

"\nAdding $" << interestEarned << " interest to account2."<<endl;

account2.credit( interestEarned );

cout <<

"\nNew account2 balance: $" << account2.getBalance() << endl;

return 0;

}

// end main

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

Class hierarchy is a fundamental concept of object oriented programs explain what this term means with reference to inheritance abstraction and instantiation?

Class hierarchy is a term used in Java. It is used for identifying the inheritance hierarchy or the parent class relationships Ex: Public class B extends C { } Public class A extends B { } Here if we take the class hierarchy for class 'A' it would be A


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


Why does Java support multilevel inheritance?

Because it is one of the most important and widely used inheritance concepts in Java. In multi level inheritance a class directly inherits features from one class and indirectly inherits features form other classes that are in the inheritance hierarchy.


Why super constructor should be first statement in subclass constructor in java?

Because, the parent class also needs to be initialized when you create an object in the inheritance hierarchy.


Inheritence and its types in object oriented programmings?

There are only two types of inheritance in object oriented programming: Single inheritance: where a class inherits directly from just one base class. Multiple inheritance: where a class inherits directly from two or more base classes. Multi-level inheritance is often thought of as being a separate type of inheritance, however inheritance relates to a derived class and those that it directly inherits from. If a base class is itself a derived class (an intermediate class), then its base class or classes are simply indirect base classes of the derivative. But in isolation, the intermediate class either uses single or multiple inheritance, even if its base class or classes are also intermediates. Virtual inheritance is also thought of as being a separate type, however virtual inheritance doesn't change the relationship between classes within the hierarchy. the only difference virtual inheritance makes is that the virtual base class or classes are constructed by the most-derived class within the current hierarchy, rather than by their most direct descendants. In this way, only one instance of each virtual base exists in the hierarchy, rather than multiple instance as would normally exist. The actual inheritance is still single or multiple, however.

Related Questions

Class hierarchy is a fundamental concept of object oriented programs explain what this term means with reference to inheritance abstraction and instantiation?

Class hierarchy is a term used in Java. It is used for identifying the inheritance hierarchy or the parent class relationships Ex: Public class B extends C { } Public class A extends B { } Here if we take the class hierarchy for class 'A' it would be A


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


Why does Java support multilevel inheritance?

Because it is one of the most important and widely used inheritance concepts in Java. In multi level inheritance a class directly inherits features from one class and indirectly inherits features form other classes that are in the inheritance hierarchy.


What is the difference between class inheritance and interface inheritance?

interface inheritance is a misleading term. Interface inheritance would be equivalent to the union of the method signatures of interfaces ( no typo here, an interface may implment multiple other interfaces) Class inheritance - single hierarchy (in C#), and not only the methods are inherited, but also the data members. (interface in C# cannot define data members)


Why super constructor should be first statement in subclass constructor in java?

Because, the parent class also needs to be initialized when you create an object in the inheritance hierarchy.


What is the uses of constructor in java?

The constructor will invoke all constructors in the inheritance hierarchy to ensure that all the parent classes of the current classes get initialized when the current class is instantiated.


Is it possible to implement overriding in every inheritance?

Yes. It is possible to implement overriding in every inheritance level. If there are methods of the same name in multiple classes in the hierarchy the one that is closest to the current object gets invoked. The other methods can be specifically invoked using the super keyword.


Inheritence and its types in object oriented programmings?

There are only two types of inheritance in object oriented programming: Single inheritance: where a class inherits directly from just one base class. Multiple inheritance: where a class inherits directly from two or more base classes. Multi-level inheritance is often thought of as being a separate type of inheritance, however inheritance relates to a derived class and those that it directly inherits from. If a base class is itself a derived class (an intermediate class), then its base class or classes are simply indirect base classes of the derivative. But in isolation, the intermediate class either uses single or multiple inheritance, even if its base class or classes are also intermediates. Virtual inheritance is also thought of as being a separate type, however virtual inheritance doesn't change the relationship between classes within the hierarchy. the only difference virtual inheritance makes is that the virtual base class or classes are constructed by the most-derived class within the current hierarchy, rather than by their most direct descendants. In this way, only one instance of each virtual base exists in the hierarchy, rather than multiple instance as would normally exist. The actual inheritance is still single or multiple, however.


What is Buddhism's hierarchy?

There is no hierarchy.


Do the Amish have a Hierarchy?

hierarchy


What is the plural of hierarchy?

The plural form of hierarchy is hierarchies.


How do youy spell hierarchy?

Hierarchy