Carbon Footprint Calculator in C++ Assignment Sample
You need to model a simple carbon footprint calculator using object inheritance. There should be 3 classes with different behavior, Bicycle, Car and House. A bicycle produces 0 carbon footprint, a house produces a carbon footprint of 134 per square foot, and a car produces 20 for each gallon used. For more C++ programming assignments contact us for a quote.
Solution:
Bicycle.h
/*
* Bicycle.h
*/
#ifndef CARBONFOOTPRINT_BICYCLE_H_
#define CARBONFOOTPRINT_BICYCLE_H_
#include “CarbonFootprint.h”
class Bicycle: public CarbonFootprint
{
public:
Bicycle(); //constructor
virtual ~Bicycle()
{
} //destructor
virtual void getCarbonFootprint() const override;
};
#endif /* CARBONFOOTPRINT_BICYCLE_H_ */
Bicycle.cpp
/*
* Bicycle.cpp
*/
#include
#include “Bicycle.h”
using namespace std;
Bicycle::Bicycle()
{
}
void Bicycle::getCarbonFootprint() const
{
cout << “Bicycle: 0” << endl;
}
Building.h
/*
* Building.h
*/
#ifndef CARBONFOOTPRINT_BUILDING_H_
#define CARBONFOOTPRINT_BUILDING_H_
#include “CarbonFootprint.h”
class Building: public CarbonFootprint
{
public:
Building(const double); //constructor
virtual ~Building()
{
} // destructor
virtual void getCarbonFootprint() const override;
private:
double squareFeet;
};
#endif /* CARBONFOOTPRINT_BUILDING_H_ */
Building.cpp
/*
* Building.cpp
*/
#include
#include “Building.h”
using namespace std;
//constructor for Building object
Building::Building(const double square)
{
squareFeet = square;
}
//override getCarbonFootprint virtual function
void Building::getCarbonFootprint() const
{
cout << “Building with “<< squareFeet << ” square feet:” << endl << squareFeet * 134 << endl;;
}
Car.h
/*
* Car.h
*/
#ifndef CARBONFOOTPRINT_CAR_H_
#define CARBONFOOTPRINT_CAR_H_
#include “CarbonFootprint.h”
class Car: public CarbonFootprint
{
public:
Car(const double); //constructor
virtual ~Car()
{
} //destructor
virtual void getCarbonFootprint() const override;
private:
double gallonsOfGas;
};
#endif /* CARBONFOOTPRINT_CAR_H_ */
Car.cpp
/*
* Car.cpp
*/
#include
#include “Car.h”
using namespace std;
//constructor for a Car object
Car::Car(const double carGallons)
{
gallonsOfGas = carGallons;
}
//override CarbonFootprint’s virtual function
void Car::getCarbonFootprint() const
{
double myCarbonFootprint = gallonsOfGas * 20;
cout << “Car that has used ” << gallonsOfGas << ” gallons of” << endl << myCarbonFootprint << endl;
}
CarbonFootprint.h
/*
* CarbonFootprint.h
*/
#ifndef CARBONFOOTPRINT_CARBONFOOTPRINT_H_
#define CARBONFOOTPRINT_CARBONFOOTPRINT_H_
class CarbonFootprint
{
public:
virtual ~CarbonFootprint()
{
}
; // virtual destructor
//abstract function for calculating carbon footprint
virtual void getCarbonFootprint() const = 0;
};
#endif /* CARBONFOOTPRINT_CARBONFOOTPRINT_H_ */
main.cpp
#include
#include “CarbonFootprint.h”
#include “Bicycle.h”
#include “Building.h”
#include “Car.h”
using namespace std;
int main() {
vector list;
// add elements to list
list.push_back(new Bicycle());
list.push_back(new Building(2500));
list.push_back(new Car(10));
// display carbon footprint of each object
for (size_t i{0}; i < list.size(); ++i) {
list[i]->getCarbonFootprint();
}
// release elements of list
for (size_t i = 0; i < list.size(); ++i) {
delete list[i];
}
}