重复通过类向量C ++

时间:2015-03-25 20:47:21

标签: c++ class vector

我仍然围绕着课程,我仍然是C ++的新手。我的任务是:

  

创建三个与继承无关的小类 - 类   建筑,汽车和自行车。给每个班级一些独特的   与其他人没有共同的属性和行为   类。

     

编写一个只有纯虚拟的抽象类CarbonFootprint   getCarbonFootprint方法。

     

让每个类继承自该抽象类和   实现getCarbonFootprint方法来计算合适的   该课程的碳足迹(查看一些解释的网站   如何计算碳足迹)。

     

编写一个创建三者中每个对象的应用程序   类,在向量中放置指向这些对象的指针   CarbonFootprint指针,然后遍历向量,   多态调用每个对象的getCarbonFootprint方法。

     

对于每个对象,打印一些识别信息和对象   碳足迹。

我无法弄清楚如何遍历vector <CarbonFootPrint>。我也不知道正在创建的对象是否实际被放入此向量中。到目前为止我的代码是:

#include <iostream>
#include <vector>
using namespace std;

class CarbonFootPrint
{
    //class declarations
public:
    virtual double getCarbonFootPrint();
};

//class implementation
double CarbonFootPrint::getCarbonFootPrint()
{
    return 0;
}

class Building : CarbonFootPrint
{
    //class declarations
public:
    Building(double e = 0, int m = 12); //constructor
    ~Building(); //destructor
    double setElectric();
    virtual double getCarbonFootPrint();

private:
    double electric;
    int months;
};

//class implementation
Building::Building(double e, int m)
{
    electric = e;
    months = m;
}

Building::~Building()
{

}

double Building::setElectric()
{
    cout << "Enter your monthly electric in KWH: " << endl;
    cin >> electric;
    return electric;
}

double Building::getCarbonFootPrint()
{
     //I would like to print out the variable information for each object created
    //and then 
    cout << "The carbon footprint for this house is " << endl;
    //when it iterates through the vector.
    return(electric * months);
}


class Car : CarbonFootPrint
{
public:
    Car(double = 0, double = 0); //constructor
    ~Car(); //destructor
    double setYearlyMiles();
    double setAverageMPG();
    virtual double getCarbonFootPrint();

 private:
    double yearlyMiles, averageMPG;
    int co2 = 9;
 };

//class implementation
Car::Car(double ym, double mpg)
{
    yearlyMiles = ym;
    averageMPG = mpg;
}

Car::~Car()
{

}

double Car::setYearlyMiles()
{
    cout << "Enter in your yearly miles: " << endl;
    cin >> yearlyMiles;
    return yearlyMiles;
}

double Car::setAverageMPG()
{
    cout << "Enter in your average miles per gallon: " << endl;
    cin >> averageMPG;
    return averageMPG;
}

double Car::getCarbonFootPrint()
{
    //I would like to print out the variable information for each object created
    //and then 
    cout << "The carbon footprint for this car is " << endl;
    //when it iterates through the vector.
    return((yearlyMiles * averageMPG) * co2);
}

class Bicycle : CarbonFootPrint
{
public:
    Bicycle(double = 0, int = 34); //constructor
    ~Bicycle(); //destructor
    double setMiles(); 
    virtual double getCarbonFootPrint();

private:
    int calories;
    double miles;
};

//class implementation
Bicycle::Bicycle(double m, int c)
{
    miles = m;
    calories = c;
}

Bicycle::~Bicycle()
{

}

double Bicycle::setMiles()
{
    cout << "Enter in number of miles: " << endl; 
    cin >> miles;
    return miles;
}

double Bicycle::getCarbonFootPrint()
{
    //I would like to print out the variable information for each object created
    //and then 
    cout << "The carbon footprint for this bicycle is " << endl;
    //when it iterates through the vector.
    return (miles * calories);
}

这是我的主要计划:

int main()
{
    vector <CarbonFootPrint> *list;
    int answer, i;

    cout << "Welcome to the Carbon Footprint Calculator!\n" << endl;

    do
    {
        cout << "Main Menu\n" << endl;
        cout << "1: Set house info.\n" << endl;
        cout << "2: Set car info.\n" << endl;
        cout << "3: Set bicycle info.\n" << endl;
        cout << "4: Get carbon footprint for all items set.\n" << endl;
        cin >> answer;

        switch (answer)
        {
            case 1:
            {
                cout << "\n" << endl;
                Building *anotherBuilding;
                anotherBuilding = new Building;
                anotherBuilding->setElectric();
                cout << "\n" << endl;

                break;
            }

            case 2:
            {
                cout << "\n" << endl;
                Car *anotherCar;
                anotherCar = new Car;
                anotherCar->setYearlyMiles();
                anotherCar->setAverageMPG();
                cout << "\n" << endl;

                break;
            }

            case 3:
            {
                cout << "\n" << endl;
                Bicycle *anotherbike;
                anotherbike = new Bicycle;
                anotherbike->setMiles();
                cout << "\n" << endl;

                break;
            }

            case 4:
            {

                //have it iterate through the vector and print out each carbon footprint.
                break;
            }

            default:
            {
                cout << answer << " is not a valid option" << endl;

                break;
            }
        }
    }
    while (answer != 4);

    system("pause");
    return 0;
}

非常感谢任何帮助或指导!谢谢你的时间!

3 个答案:

答案 0 :(得分:0)

你错过了一件至关重要的事情:

  

编写一个创建三个类中每个类的对象的应用程序,在CarbonFootprint指针的向量中放置指向这些对象的指针,然后迭代遍历向量,以多态方式调用每个对象的getCarbonFootprint方法。

而不是

vector <CarbonFootPrint> *list; // Pointer to a vector of CarbonFootPrint objects.

您需要使用

vector <CarbonFootPrint*> list; // A vector of CarbonFootPrint pointers.

而不是

{
    cout << "\n" << endl;
    Building *anotherBuilding;
    anotherBuilding = new Building;
    anotherBuilding->setElectric();
    cout << "\n" << endl;

    break;
}

使用

{
    cout << "\n" << endl;
    Building *anotherBuilding;
    anotherBuilding = new Building;
    anotherBuilding->setElectric();

    // Add the pointer to the list of pointers.
    list.push_back(anotherBuilding);
    cout << "\n" << endl;

    break;
}

对其他对象类型进行类似的更改。

最后,在对象上调用getCarbonFooPrint()

for (auto item : list )
{
   item->getCarbonFooPrint();
}

并删除对象:

for (auto item : list )
{
   delete item;
}

答案 1 :(得分:0)

Re:您对R Sahu的帖子的评论(我太新了,不允许对其他帖子发表评论)

您无法访问基类,因为它默认为private

class Building : CarbonFootPrint
class Car : CarbonFootPrint
class Bicycle : CarbonFootPrint

都是私下继承自CarbonFootPrint,这代表了“有一个”的关系,在这种情况下,虽然从语义上来说汽车有一个碳足迹,你实际上试图制作一个“is-a”关系,因为这些都是实现基类的对象,CarbonFootPrint的更好名称可能是CarbonFootPrintProducer

这里的修复只是让它们全部公开继承

class Name : public Base

答案 2 :(得分:-1)

R Sahu几乎涵盖了所有这一切,我看到的唯一一点就是;

  

使用纯虚拟getCarbonFootprint方法编写一个抽象类CarbonFootprint。

你的类不是抽象的,因为函数不是纯虚函数,为此只需添加= 0

virtual double getCarbonFootPrint() = 0;

纯虚函数是不计算实现的函数,任何包含纯虚函数的类都称为“抽象”,无法实例化。在这种情况下,它可以帮助您识别CarbonFootPrint而不是CarbonFootPrint*向量,因为实例化将由编译器选取,尽管知道模板编译器错误,您已经有了一对这个简单错误的一百行错误消息。


完全公开:实际上可以定义纯虚函数,例如;

virtual double getCarbonFootPrint() = 0;

double CarbonFootPrint::getCarbonFootPrint()
{
    // some code
    return 0;
}
然后可以从任何派生类

调用

double Building::getCarbonFootPrint()
{
    return CarbonFootPrint::getCarbonFootPrint();
}

这是合法的C ++,允许您定义默认实现,而CarbonFootprint仍然是一个抽象类。