Summary: In the previous chapter, the declaration, initialization, and display of objects were presented. In this chapter, we continue our construction of classes by discussing how to write advanced constructors and destructors. Besides, this chapter also discusses one of C++ most powerful features, inheritance. Inheritance permits the reuse and extension of existing code in a way that ensures the new code does not adversely affect what has already been written.
Payroll::Payroll(double dFed, double dState){
dFedTax = dFed;
dStateTax = dState;
};
class Payroll{
public:
Payroll(double, double);
private:
double dFedTax;
double dStateTax;
}
#include “Payroll.h
#include <iostream.h>
Payroll::Payroll(double dFred, double dState){
dFedTax = dFed;
dStateTax = dState;
};
int main( ){
Payroll employee; //illegal
……
return 0;
}
//points
#include <iostream.h>
#include<math.h>
class point {
private:
int x,y;
public:
point( int xnew, int ynew);
inline int getx(){
return(x);
}
inline int gety(){
return(y);
}
double finddist(point a, point b);
};
point::point(int xnew, ynew) //parameterized constructor
{
x = xnew;
y = ynew;
}
double point::finddist(point a, point b)
{
double temp;
temp = ((b.y – a.y)*(b.y – a.y) + (b.x – a.x)*(b.x – a.x));
return(sqrt(temp)):
}
int main()
{
double value;
point aobj(4,3), bobj(0, -1);
value = aobj.finddist(aobj, bobj);
cout << “Distance between two points = “<< value << endl;
return 0;
}
class Payroll{
public:
Payroll();
Payroll(double dFed);
Payroll(double dFed, double dState);
private:
double dFedTax;
double dStateTax;
}
#include “Payroll.h
#include <iostream.h>
Payroll::Payroll(){
dFedTax = 0.28;
dStateTax = 0.05;
};
Payroll::Payroll(double dFed){
dFedTax = dFed;
};
Payroll::Payroll(double dFred, double dState){
dFedTax = dFed;
dStateTax = dState;
};
int main( ){
Payroll employeeFL(0.28);
Payroll employeeMA(0.28, 0.0595);
return 0;
}
Payroll::Payroll(double dFed, double dState){
dFedTax = dFed;
dStateTax = dState;
};
Payroll::Payroll(double dFed, double dState)
:dFedTax(dFed), dStateTax(dState){
};
Employee(const int id = 999, const double hourly = 5.65);
#include<iostream.h>
class Employee{
private:
int idNum;
double hourlyRate;
public:
Employee(const int id = 9999, const double hourly = 5.65);
void setValues(const int id, const double hourly);
void displayValues();
};
Employee::Employee(const int id, const double hourly)
{
idNum = id;
hourlyRate = hourly;
}
void Employee::displayValues()
{
cout<<”Employee #<< idNum<<” rate $”<<
hourlyRate<< “ per hour “<<endl;
}
void Employee::setValues(const int id, const double hourly)
{
idNum = id;
hourlyRate = hourly;
}
int main(){
Employee assistant;
cout<< “Before setting values with setValues()”<< endl;
assistant.displayValues();
assistant.setValues(4321, 12.75);
cout<< “After setting values with setValues()”<< endl;
assistant.displayValues();
return 0;
}
class Stocks {
public:
Stocks(char* szName);
~Stocks(); //destructor
void setStockName(char* szName);
char* getStockName();
void setNumShares(int);
int getNumShares(int);
void setPricePerShare(double);
double getPricePerShar();
double calcTotalValue();
private:
char* szStockName;
int iNumShares;
double dCurrentValue;
double dPricePerShare;
};
#include “stocks_02.h”
#include <string.h>
#include <iostream.h>
Stocks::Stocks(char* szName){
szStockName = new char[25];
strcpy(szStockName, szName);
};
Stocks::~Stocks(){
delete[] szStockName;
cout <<”Destructor called” << endl;
}
void Stocks::setNumShares(int iShares){
iNumShares = iShares;
}
int Stocks::getNumShares()const{
return iNumShares;
}
void Stocks::setPricePerShare(double dPrice){
dPricePerShare = dPrice;
}
int Stocks::getPricePerShare() const{
return dPricePerShare;
}
void Stocks::setStockName(char* szName){
strcpy(szStockName, szName);
}
char* Stock::getStockName() const{
return szStockName;
}
double Stocks::calcTotalValue(){
dCurrentValue = iNumShares*dPricePerShare;
return dCurrentValue;
}
int main(){
Stocks stockPick1(“Cisco”);
stockPick1.setNumShares(100);
stockPick1.setPricePerShare(68.875);
Stocks* stockPick2 = new Stocks(“Lucent”); //heap object
stockPick2->setNumShares(200);
stockPick2->setPricePerShare(59.5);
cout << “The current value of your stock in “
<< stockPick1.getStockName() << “ is $”
<< stockPick1.calcTotalValue()
<< “.” << endl;
cout << “The current value of your stock in “
<< stockPick2->getStockName() << “ is $”
<< stockPick2->calcTotalValue()
<< “.” << endl;
return 0;
}
int main(){
Stocks stockPick1(“Cisco”);
stockPick1.setNumShares(100);
stockPick1.setPricePerShare(68.875);
Stocks* stockPick2 = new Stocks(“Lucent”); //heap object
stockPick2->setNumShares(200);
stockPick2->setPricePerShare(59.5);
cout << “The current value of your stock in “
<< stockPick1.getStockName() << “ is $”
<< stockPick1.calcTotalValue()
<< “.” << endl;
cout << “The current value of your stock in “
<< stockPick2->getStockName() << “ is $”
<< stockPick2->calcTotalValue()
<< “.” << endl;
delete stockPick2;
return 0;
}
const Date currentDate;
class Payroll{
public:
Payroll();
private:
const double dFedTax;
const double dStateTax;
};
#include “Payroll.h
#include <iostream.h>
Payroll::Payroll()
:dFedTax(0.28), dStateTax(0.05){
};
class Payroll{
public:
Payroll();
private:
const double dFedTax;
const double dStateTax;
};
#include “Payroll.h”
#include <iostream.h>
Payroll::Payroll( ){
dFedTax = 0.28; //illegal
dStateTax = 0.05; //illegal
};
double getStateTax(Payroll* pStateTax) const;
double Payroll::getStateTax(Payroll* pStateTax) const {
return pStateTax->dStateTax;
};
class Person
{
private:
int idnum;
char lastName[20];
char firstName[15];
public:
void setFields(int, char[], char[]);
void outputData( );
};
void Person::setFields(int num, char last[], char first[])
{
idnum = num;
strcpy(lastName, last);
strcpy(firstName, first);
}
void Person::outputData( )
{
cout<< “ID#”<< idnum << “ Name: “<< firstName << “ “<< lastName << endl;
}
class Customer: public Person
{
……// other statements go here
}
![]() Figure 1: Base class and derived class |
int main(){
customer cust;
cust.setField(123, “Richard”, “Leakey”);
cust.outputData( );
return 0;
}
#include<iostream.h>
#include<string.h>
class Person
{
private:
int idnum;
char lastName[20];
char firstName[15];
public:
void setFields(int, char[], char[]);
void outputData( );
};
void Person::setFields(int num, char last[], char first[])
{
idnum = num;
strcpy(lastName, last);
strcpy(firstName, first);
}
void Person::outputData( )
{
cout<< “ID#”<< idnum << “ Name: “<< firstName << “ “<< lastName << endl;
}
class Customer:public Person
{
private:
double balanceDue;
public:
void setBalDue;
void outputBalDue( );
};
void Customer::setBalDue(double bal)
{
balanceDue = bal;
}
void Customer::outputBalDue()
{
cout<< “Balance due $” << balanceDue<< endl;
}
int main()
{
Customer cust;
cust.setFields(215, “Santini”, “Linda”);
cust.outputData();
cust.setBalDue(147.95);
cust.outputBalDue();
return 0;
}
![]() Figure 2: Person class hierarchy |
class Person {
protected:
int idNum;
char lastName[20];
char firstName[15];
public:
void setFields(int num, char last[], char first[]);
void outputData();
};
object.base_class::function();
#include<iostream.h>
#include<string.h>
class Person
{
private:
int idnum;
char lastName[20];
char firstName[15];
public:
void setFields(int, char[], char[]);
void outputData( );
};
void Person::setFields(int num, char last[], char first[])
{
idnum = num;
strcpy(lastName, last);
strcpy(firstName, first);
}
void Person::outputData( )
{
cout<< “ID#”<< idnum << “ Name: “<< firstName << “ “<< lastName << endl;
}
class Employee:public Person
{
private:
int dept;
double hourlyRate;
public:
void setFields(int, char[], char[], int, double);
};
void Employee::setFields(int num, char last[], char first[], int dept, double sal)
{
Person::setFields(num, last, first);
dept = dep;
hourlyRate = sal;
}
int main()
{
Person aPerson;
aPerson.setFields(123, “Kroening”, “Ginny”);
aPerson.outputData();
cout<< endl<<endl;
Employee worker;
worker.Person::setFields(777,”John”, “Smith”);
worket.outputData();
worker.setFields(987,”Lewis”, “Kathy”, 6, 23.55);
worker.outputData();
return 0;
}
![]() Figure 3: Execution of constructors and destructors in a class hierarchy. |
#include<iostream.h>
#include<string.h>
class Person
{
private:
int idnum;
char lastName[20];
char firstName[15];
public:
Person();
void setFields(int, char[], char[]);
void outputData( );
};
Person::Person(){
cout << “Base class constructor call “<< endl;
}
void Person::setFields(int num, char last[], char first[])
{
idnum = num;
strcpy(lastName, last);
strcpy(firstName, first);
}
void Person::outputData( )
{
cout<< “ID#”<< idnum << “ Name: “<< firstName << “ “<< lastName << endl;
}
class Customer:public Person
{
private:
double balanceDue;
public:
Customer();
void setBalDue;
void outputBalDue( );
};
Customer::Customer(){
cout << “Derived constructor called” << endl;
}
void Customer::setBalDue(double bal)
{
balanceDue = bal;
}
void Customer::outputBalDue()
{
cout<< “Balance due $” << balanceDue<< endl;
}
int main()
{
Customer cust;
cust.setFields(215, “Santini”, “Linda”);
cust.outputData();
cust.setBalDue(147.95);
cust.outputBalDue();
return 0;
}
Comments, questions, feedback, criticisms?