Single Inheritence
Single Inheritence
Single Inheritence
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
public:
Account(double);
void credit(double);
float debit(double);
void setbalance(double);
double getbalance();
private:
double balance;
};
#endif
#include"Account.h"
#include<iostream>
Account::Account(double initialbalance)
if(initialbalance>=0)
balance=initialbalance;
}
else
balance=balance+amount;
if(amount>balance)
return false;
else
balance=balance-amount;
return true;
balance=newbalance;
double Account::getbalance()
{
return balance;
#ifndef SAVINGS_H
#define SAVINGS_H
#include"Account.h"
public:
SavingsAccount(double,double);
double calculatefloaterest();
private:
double floaterestrate;
};
#endif
#include"SavingsAccount.h"
#include<iostream>
if(rate>=0)
floaterestrate=rate;
}
else
double SavingsAccount::calculatefloaterest()
return (getbalance()*floaterestrate);
#ifndef CHECKING_H
#define CHECKING_H
#include"Account.h"
#include"SavingsAccount.h"
public:
CheckingAccount(double,double);
void credit(double);
float debit(double);
private:
double transactionfee;
void chargefee();
};
#endif
#include"CheckingAccount.h"
#include<iostream>
if(fee>=0)
transactionfee=fee;
else
Account::credit(amount);
chargefee();
float success=Account::debit(amount);
if(success)
chargefee();
return true;
}
else
return false;
void CheckingAccount::chargefee()
Account::setbalance(getbalance()- transactionfee);
#include"Account.h"
#include"SavingsAccount.h"
#include"CheckingAccount.h"
#include<iostream>
#include<iomanip>
int main()
Account account1(50.0);
SavingsAccount account2(25.0,0.03);
CheckingAccount account3(80.0,1.0);
cout<<fixed<<setprecision(2);
account1.debit(25.0);
account2.debit(30.0);
account3.debit(40.0);
account1.credit(40.0);
account2.credit(65.0);
account3.credit(20.0);
cout<<"\nAccount1 balance=$"<<account1.getbalance()<<endl;
cout<<"\nAccount2 balance=$"<<account2.getbalance()<<endl;
cout<<"\nAccount3 balance=$"<<account3.getbalance()<<endl;
double floaterestearned=account2.calculatefloaterest();
#define PACKAGE_H
#include<string>
class Package
public:
Package( const string &, const string &, const string &,const string &, int, const string &, const string &,
const string &,const string &, int, double, double );
private:
string senderName;
string senderAddress;
string senderCity;
string senderState;
int senderZIP;
string recipientName;
string recipientAddress;
string recipientCity;
string recipientState;
int recipientZIP;
double weight;
double costPerOunce;
};
#endif
#include "Package.h"
#include<iostream>
#include<string>
Package::Package( const string &sName, const string &sAddress,const string &sCity, const string
&sState, int sZIP,const string &rName, const string &rAddress, const string &rCity,const string &rState,
int rZIP, double w, double cost ):senderName( sName ), senderAddress( sAddress ),
senderCity( sCity ),senderState( sState ), senderZIP( sZIP ),
recipientName( rName ),recipientAddress( rAddress ), recipientCity( rCity ),recipientState( rState ),
recipientZIP( rZIP )
setWeight(w);
setCostPerOunce(cost);
senderName=name;
return senderName;
senderAddress = address;
return senderAddress;
senderCity = city;
return senderCity;
senderState = state;
return senderState;
{
senderZIP = zip;
return senderZIP;
recipientName = name;
return recipientName;
recipientAddress = address;
return recipientAddress;
recipientCity = city;
}
return recipientCity;
recipientState = state;
return recipientState;
recipientZIP = zip;
return recipientZIP;
}
double Package::getWeight() const
return weight;
cout<<costPerOunce;
return costPerOunce;
#ifndef TWODAY_H
#define TWODAY_H
#include "Package.h"
public:
TwoDayPackage( const string &, const string &, const string &,const string &, int, const string &, const
string &, const string &,const string &, int, double, double, double );
private:
double flatFee;
};
#endif
#include"TwoDayPackage.h"
#include<iostream>
setFlatFee( fee );
return flatFee;
#ifndef OVERNIGHT_H
#define OVERNIGHT_H
#include "Package.h"
#include"TwoDayPackage.h"
public:
OvernightPackage( const string &, const string &, const string &,const string &, int, const string &, const
string &, const string &,const string &, int, double, double, double );
private:
double overnightFeePerOunce;
};
#endif
#include "OvernightPackage.h"
#include<iostream>
setOvernightFeePerOunce( fee );
overnightFeePerOunce =overnightFee;
double OvernightPackage::getOvernightFeePerOunce()const
return overnightFeePerOunce;
double OvernightPackage::calculateCost()const
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h"
#include<iostream>
#include<string>
#include<iomanip>
int main()
Package package1( "Ahmad Khan", "1 Main St", "Boston", "MA", 11111,"Zahid", "7 Elm St", "New York",
"NY", 22222, 8.5, .5 );
TwoDayPackage package2( "Ayesha", "5 Broadway", "Somerville", "MA",33333, "George", "21 Pine Rd",
"Cambridge", "MA", 44444,10.5, .65, 2.0 );
OvernightPackage package3( "Seokjin", "2 Oak St", "Boston", "MA",55555,"Helina", "9 Main St",
"Denver", "CO", 66666,12.25, .7, .25 );
cout<<"Package 1:\n\nSender:\n"<<package1.getSenderName()<<'\
n'<<package1.getSenderAddress()<<'\
n'<<package1.getSenderCity()<<","<<package1.getSenderState()<<' '<< package1.getSenderZIP();
cout<<"\n\nRecipient:\n"<<package1.getRecipientName()<<'\n'<<package1.getRecipientAddress()<<'\
n'<<package1.getRecipientCity()<<","<<package1.getRecipientState()<<' '<< package1.getRecipientZIP();
cout<<"\nPackage 2:\n\nSender:\n"<<package2.getSenderName()<<'\
n'<<package2.getSenderAddress()<<'\n'<<package2.getSenderCity()<<",
"<<package2.getSenderState()<< ' ' << package2.getSenderZIP();
return 0;
Student at a university
Freshman Sophomore
Employee Inheritance Hierarchy
Academic Administrative
Vice Principal
Lecturer
Employee at a university
Shape