// Test program for CarbonFootprint and implementing classes.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class CarbonFootprint
{
public:
virtual void getCarbonFootprint() = 0; //pure virtual function
virtual ~CarbonFootprint()
{
cout << "\nCarbon Footprint class destroyed!" << endl;
}
};
class Car : public CarbonFootprint
{
private:
double CO2_kg_perYear;
double avg_miles_driven_annually;
double avg_miles_per_gallon;
char fuel_type;
string car;
public:
Car(double mpg) : avg_miles_per_gallon(mpg) //one-arg constructor
{
cout << "\nCar Carbon Footprint." << endl;
cout << "\n Enter the average amount of miles you drive per year: ";
cin >> avg_miles_driven_annually;
cout << " Select type of fuel for the "<< car <<": ('d' for diesel, 'u' for unleaded): ";
cin >> fuel_type;
}
void getCarbonFootprint()
{
double DIESEL_FUEL_FACTOR = 10.15;
double UNLEADED_FUEL_FACTOR = 8.91;
double gallons_used_perYear = (avg_miles_driven_annually/avg_miles_per_gallon);
bool isValid = false;
while(isValid == false){
if (fuel_type == 'd'){
CO2_kg_perYear = gallons_used_perYear * DIESEL_FUEL_FACTOR;
cout << "\n The "<< car <<"'s annual CO2 emission is: " <<
CO2_kg_perYear<<" kilograms per year" <<endl;
isValid = true;
}
else if(fuel_type == 'u'){
CO2_kg_perYear = gallons_used_perYear * UNLEADED_FUEL_FACTOR;
cout << "\n The annual CO2 emission for your car is: " <<
CO2_kg_perYear<<" kilograms per year" <<endl;
isValid = true;
}
}
}
~Car()
{
cout << "\nCar class destroyed!" << endl;
}
};
class Building : public CarbonFootprint
{
private:
double Total_CO2_kg_perYear;
double CO2_from_gas_per_year;
int kilowattHours_per_year;
double DTH_per_year;
public:
Building(int hr) : kilowattHours_per_year(hr)//constructor (two args)
{
cout << "\nBuilding carbon footprint."<< endl;
cout << " For residential gas service, enter average DTH billed per year: ";
cin >> DTH_per_year;
}
void getCarbonFootprint()
{
double thermConversionFactor = 12.98;
CO2_from_gas_per_year = ((DTH_per_year * 10)* thermConversionFactor);
Total_CO2_kg_perYear = kilowattHours_per_year + CO2_from_gas_per_year;
cout << "\n CO2 footprint in kilograms from electricity: " << kilowattHours_per_year;
cout << "\n CO2 footprint in kilograms from residential gas: " << CO2_from_gas_per_year;
cout << "\n Total CO2 footprint for the building is: " << Total_CO2_kg_perYear << endl;
}
~Building() //virtual destructor
{
cout << "\nBuilding class destroyed!" << endl;
}
};
class Bicycle : public CarbonFootprint
{
private:
int weight, minutesRidePerDay;
char speed, foodPractice;
double extraCaloriesConsumed;
double totalFoodCO2PerYear;
bool is_SpeedValid;
bool Ok;
public:
Bicycle()
{
cout << "\nBicycling carbon footprint."<< endl;
cout <<" Enter your weight: ";cin >>weight;
cout <<" Enter how many minutes do you ride daily: ";cin >> minutesRidePerDay;
cout <<" How fast do you ride?"<< endl;
cout <<" For light speed (10-12 mph) enter 'l'" << endl;
cout <<" For moderate speed (12-14 mph) enter 'm'" << endl;
cout <<" For vigorous speed (14-16 mph) enter 'v'" << endl;
cin >> speed;
bool is_SpeedValid = false;
while (is_SpeedValid == false)
{
if ((speed == 'l') || (speed == 'm') || (speed == 'v'))
{
is_SpeedValid = true;
}
}
cout <<"\n What is your food practice?"<< endl;
cout <<" For vegetarian, enter 'v'" << endl;
cout <<" For heavy meat eater, enter 'c'" << endl;
cout <<" For average, enter 'a'" << endl;
cin >> foodPractice;
bool Ok = false;
while (Ok == false)
{
if (foodPractice == 'v' ||foodPractice == 'c'||foodPractice == 'a' ){
Ok = true;
}
}
}
void getCarbonFootprint()
{
const double L_speedfactor = .0453;
const double M_speedfactor = .0606;
const double V_speedfactor = .0753;
const double VegetarianCONVERSION_FACTOR = .9066;
const double AvgCONVERSION_FACTOR = 1.11776;
const double carnivoreCONVERSION_FACTOR = 1.285;
if ( speed == 'l')
{
extraCaloriesConsumed = weight * minutesRidePerDay * L_speedfactor;
}
else if(speed == 'm')
{
extraCaloriesConsumed = weight * minutesRidePerDay * M_speedfactor;
}
else if(speed == 'v')
{
extraCaloriesConsumed = weight * minutesRidePerDay * V_speedfactor;
}
if (foodPractice == 'v' || foodPractice == 'V') //for vegetarians
{
totalFoodCO2PerYear = extraCaloriesConsumed * VegetarianCONVERSION_FACTOR;
cout << "\n The total CO2 emission for extra food annually is: " <<
totalFoodCO2PerYear<<" kilograms per year" <<endl;
}
else if(foodPractice == 'c' || foodPractice == 'C')
{
totalFoodCO2PerYear = extraCaloriesConsumed * carnivoreCONVERSION_FACTOR;
cout << "\n The total CO2 emission for extra food annually is: " <<
totalFoodCO2PerYear<<" kilograms per year" <<endl;
}
else if(foodPractice == 'a' || foodPractice == 'A')
{
totalFoodCO2PerYear = extraCaloriesConsumed * AvgCONVERSION_FACTOR;
cout << "\n The total CO2 emission for extra food annually is: " <<
totalFoodCO2PerYear<<" kilograms per year" <<endl;
}
~Bicycle()
{
cout << "\nBicycle class destroyed!" << endl;
}
}
};
int main()
{
vector< CarbonFootprint* > 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];
}
return 0;
// end main
}
~Bicycle()
{
cout << "\nBicycle class destroyed!" << endl;
}
it tells me this one is wrong and ~ not operator
and i destroyed two up, why this one wrong
Last edited on
Please use code block formatting <> its the button to the right of the text entry box as it makes code much easier to read.
Your destructor is in the getcarbonfootprint() function. Is that allowed?
I don't think it is.
Last edited on