Question

Must be in C++. Governments and companies worldwide are becoming increasingly concerned with carbon footprints (annual...

Must be in C++. 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 class CarbonFootprint 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Header.h

#pragma once
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>

using namespace std;


//abstract parent class
class CarbonFootprint {
public:
   virtual void getCarbonFootprint();

};
void CarbonFootprint::getCarbonFootprint()
{
   cout << "Parent Display" << endl;

}
//child classes
class Building : public CarbonFootprint {

public:
   virtual void getCarbonFootprint();
   int walls = 4;
   int SQFT = 800;


};
void Building::getCarbonFootprint() {
   cout << endl;
   cout << "Building displayed" << endl;
   double carbon = 0.859;
   cout << "Your building emits " << carbon << " tons of CO2" << endl;
   cout << "Your home has " << walls << " walls" << endl;
   cout << "Your home is " << SQFT << " Square feet" << endl;
   cout << endl;


}

class Car : public CarbonFootprint {
public:
   virtual void getCarbonFootprint();
   int tanksize = 10;
   int mpg = 45;

};

void Car::getCarbonFootprint() {
   cout << endl;
   cout << "Car displayed" << endl;
   int carbon = 4;
   cout << "Your car emits " << carbon << "tons of CO2" << endl;
   cout << "Your car has a gas tank size of: " << tanksize << endl;
   cout << "Your car gets " << mpg << " miles per gallon." << endl;
   cout << endl;

}

class Bicycle : public CarbonFootprint {

public:
   virtual void getCarbonFootprint();
   string frametype = "J Frame";

};
void Bicycle::getCarbonFootprint() {


   cout << endl;
   cout << "Bicycle displayed" << endl;
   int carbon = 0;
   cout << "Your bicycle emits " << carbon << " tons of CO2" << endl;
   cout << "Your bicycle frame is " << frametype << endl;
   cout << endl;


}


main.cpp

//preprocessor directives
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include"Header.h"

using namespace std;


int main()
{

   char loop = 'y';
   int selection;

   //while loop for program
   while (loop == 'y' || loop == 'Y')
   {

   cout << "**************************************************" << endl;
   cout << "Hello! Welcome to Rico's carbon footprint program." << endl;
   cout << "**************************************************" << endl;
   cout << "Emissions are a big threat to our planet. Do your part, see what emissions you cause by using my program. " << endl;
   cout << "What would you like to do?" << endl;
   cout << endl;
   cout << "1. Display possessions, their attributes and carbon emission" << endl;


   //vector
   vector<CarbonFootprint *> CarbonTrak;

  
   CarbonTrak.push_back(new Building());
   CarbonTrak.push_back(new Car());
   CarbonTrak.push_back(new Bicycle());

   cin >> selection;
   switch (selection)
   {
   case 1:
       //for loop that iterates through vector
       for (vector<CarbonFootprint*>::iterator iter = CarbonTrak.begin(); iter != CarbonTrak.end(); ++iter) {
           (*iter)->getCarbonFootprint();
       }
       break;
   }
   cout << "If you would like to return to the main menu, press 'y', to quit press 'n'.(y/n)" << endl;
   cin >> loop;
   system("cls");
   }// endl while loop

  

   cout << "Thank you for using my program! Enjoy your day." << endl;
   system("pause");
   return 0;
  
}


Add a comment
Know the answer?
Add Answer to:
Must be in C++. Governments and companies worldwide are becoming increasingly concerned with carbon footprints (annual...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT