Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ How to Program, by Deitel, 9th Edition - Chapter 12, Making a Difference 12.

ID: 3675135 • Letter: C

Question

C++ How to Program, by Deitel, 9th Edition - Chapter 12, Making a Difference 12.16

12.16 (CarbonFootprint Abstract Class:Polymorphism) Using an abstract

class with only pure virtual functions, you can specify similar behaviors for

possibly disparate classes. Governments and companies worldwide are

becoming increasingly concerned with carbon footprints (annual releases of

carbon dioxide into the atmosphere) from buildings burning various types of

fuels for heat, vehicles burning fuels for power, and the like. Many scientists

blame these greenhouse gases for the phenomenon called global warming.

Create three small classes unrelated by inheritance—classes Building, Car and

Bicycle. Give each class some unique appropriate attributes and behaviors that

it does not have in common with other classes.

Write an abstract classCarbonFootprint with only a pure virtual

getCarbonFootprint method. Have each of your classes inherit from that

abstract class and implement the getCarbonFootprint method to calculate an

appropriate carbon footprint for that class (check out a few websites that

explain how to calculate carbon footprints).

Write an application that creates objects of each of the three classes, places

pointers to those objects in a vector of CarbonFootprint pointers, then iterates

through the vector, polymorphically invoking each object’s

getCarbonFootprint method. For each object, print some identifying

information and the object’s carbon footprint.

Explanation / Answer

test.cpp
#include <iostream>
#include <vector>

#include "CarbonFootprint.h"
#include "Car.h"
#include "Building.h"
#include "Bicycle.h"

using namespace std;

int main ()
{
      vector< CarbonFootprint * > pointers( 3 );
  
    Car car( 23.0 );
    Building building( 70.0 );
    Bicycle bicycle( 15 );

    pointers[ 0 ] = &car;
    pointers[ 1 ] = &building;
    pointers[ 2 ] = &bicycle;

    for ( int i = 0; i < 3; i++ )
   {
        pointers[ i ]->print();
        cout << pointers[ i ]->getCarbonFootprint() << endl;
   }

    return 0;
}


CarbonFootprint.cpp
#include "CarbonFootprint.h"

CarbonFootprint::CarbonFootprint()
{
}
CarbonFootprint::~CarbonFootprint()
{

}


CarbonFootprint.h
#ifndef CARBONFOOTPRINT_H
#define CARBONFOOTPRINT_H

class CarbonFootprint
{
public:
   CarbonFootprint();
   virtual ~CarbonFootprint();

   virtual double getCarbonFootprint() const = 0;
   virtual void print() const = 0;

};

#endif // CARBONFOOTPRINT_H


Car.h
#ifndef CAR_H
#define CAR_H

#include "CarbonFootprint.h"

class Car: public CarbonFootprint
{
public:
   Car( double = 0 );
  
   virtual double getCarbonFootprint() const;
   virtual double getGasCost() const;
   virtual void print() const;

private:
   double gasCost;

};

#endif // CAR_H

Car.cpp
#include <iostream>

#include "Car.h"

using namespace std;

Car::Car( double g )
    :gasCost( g )
{
}
double Car::getCarbonFootprint() const
{
    return gasCost * 0.785;
}

double Car::getGasCost() const
{
    return gasCost;
}

void Car::print() const
{
    cout << "The carbonFootprint is about Car: " ;
}

Building.h
#ifndef BUILDING_H
#define BUILDING_H

#include "CarbonFootprint.h"

class Building: public CarbonFootprint
{
public:
   Building( double = 0 );
  
   virtual double getCarbonFootprint() const;
   virtual void print() const;

   double getElectricityCost() const;

private:
   double ElectricityCost; // per degree
  
};

#endif // BUILDING_H

Building.cpp
#include <iostream>
#include "Building.h"

using namespace std;

Building::Building( double e )
    :ElectricityCost( e )
{
}

double Building::getCarbonFootprint() const
{
    return ElectricityCost * 0.785;
}

double Building::getElectricityCost() const
{
    return ElectricityCost;
}

void Building::print() const
{
    cout << "The CarbonFootprint is about Building: ";
}


Bicycle.h

#ifndef BICYCLE_H
#define BICYCLE_H

#include "CarbonFootprint.h"

class Bicycle: public CarbonFootprint
{
public:
Bicycle( double = 0 );
  
virtual double getCarbonFootprint() const;
virtual void print() const;

double getDistance() const;

private:
double distance; // per kilometer

};

#endif // BICYCLE_H

Bicycle.cpp
#include <iostream>
#include "Bicycle.h"

using namespace std;

Bicycle::Bicycle( double d )
    :distance( d )
{
}
double Bicycle::getCarbonFootprint() const
{
    return distance * 0;
}

double Bicycle::getDistance() const
{
    return distance;
}
void Bicycle::print() const
{
    cout << "The CarbonFootprint is about Bicycle: ";
}