Wednesday, August 10, 2011

Inheritance Example



Here is an example of inheritance. For this program we have a general account class that contains all the fields and methods that describe an account in the abstract. Then we have a Checking class that inherits from the Account class and adds to it the specific elements of a checking account. This is the usual patter of inheritance, General to specific.

Here is the code for the Account header and the class definition:


Account.h

#pragma once
#include <string>

class Account
{
/**********************
This is the parent class. We will never
actually implement this class only its
children. A child inherits all "public"
fields and methods
************************/
public:
Account(void);
~Account(void);
double GetBalance();
void SetBalance(double);
int GetAccountNumber();
void SetAccountNumber(int);
std::string GetCustomerName();
void SetCustomerName(std::string);
//virtual allows a child to change the method
virtual double CalculateBalance(double);

private:
double balance;
int accountNumber;
std::string customerName;
};


Account.cpp

#include "Account.h"


Account::Account(void)
{
}


Account::~Account(void)
{
}

//sets
void Account::SetAccountNumber(int accNumber)
{
accountNumber=accNumber;
}

void Account::SetBalance(double iniBalance)
{
balance=iniBalance;
}

void Account::SetCustomerName(std::string name)
{
customerName=name;
}

//gets
int Account::GetAccountNumber()
{
return accountNumber;
}

double Account::GetBalance()
{
return balance;
}

std::string Account::GetCustomerName()
{
return customerName;
}

//this method only returns a token. We want
//the children of the class to overwrite it
//to fit their needs
double Account::CalculateBalance(double transaction)
{
return 0;
}


Now here are the definitions of the child class. Notice that inheritance is signified by a single colon and then the accessor and name of the parent class You do not have to define any of the elements that are received through inheritance. The CalculateBalance() method is listed because it is overwritten in the child method. This is possible because it was declared "virtual" in the Parent class Account.


Checking.h

#pragma once
#include "account.h"
class Checking :
public Account
{
/************************
this class inherits from account
and so has access to all its public
fields and methods. Inheritance is
usually general->specific. Account
is general, checking is a specific kind
of account
**************************/
public:
Checking(void);
~Checking(void);
//only need to define new methods
void Withdrawal(double);
void Deposit(double);
//overwrite parent method of same name
double CalculateBalance(double);

};



Checking.cpp

#include "Checking.h"


Checking::Checking(void)
{
}


Checking::~Checking(void)
{
}

//give a body to the new methods
void Checking::Withdrawal(double w)
{
CalculateBalance(w * -1);
}

void Checking::Deposit(double d)
{
CalculateBalance(d);
}

//overwrite the parent method
double Checking::CalculateBalance(double transaction)
{
double newBalance= GetBalance() + transaction;

SetBalance(newBalance);


return GetBalance();
}



Now here is the Program class that tests the code. Note that only the Checking class is invoked.


Program.cpp
#include <iostream>
#include "Checking.h"
using namespace std;

int main()
{
//get the basic checking account info
Checking check;
cout << "Enter the beginning balance" << endl;
double bal;
cin >> bal;
check.SetBalance(bal);
cout << "Enter the Account number "<< endl;
int number;
cin >> number;
cin.ignore();
check.SetAccountNumber(number);
cout<<"Enter The customer name" <<endl;
string customer;
getline(cin, customer);
check.SetCustomerName(customer);
//get all deposits
cout << "How many deposits do you want to enter?" << endl;
int num;
cin >> num;
for (int i=1;i<=num;i++)
{
cout << "Enter Deposit :";
double dep=0;
cin>> dep;
check.Deposit(dep);
}

//get all Withdrawals
cout << "How many Withdrawals do you want to enter?" <<endl;
int num2;
cin >> num2;
for (int i=1;i<=num2;i++)
{
cout << "Enter Withdrawal :";
double wd=0;
cin >> wd;
check.Withdrawal(wd);
}
//get balance
cout<<"************************************"<<endl;
cout << "For checking Account " << check.GetAccountNumber ()
<<" belonging to " << check.GetCustomerName() << endl;
cout << "the current balance is " << check.GetBalance();

char c;
cin >> c;

}

No comments:

Post a Comment