gpt4 book ai didi

c++ - 如何声明指向类对象的指针 vector ?

转载 作者:行者123 更新时间:2023-12-02 14:06:59 25 4
gpt4 key购买 nike

问题是我正在努力制作 Shape 类的对象。我声明了 vector ,但不知道如何将其连接到类 Shape 及其对象。代码本身有一个基类,即Shape,然后是两个子类CircleRectancle。代码的想法是在主函数中使用 vector ,并为圆形区域和矩形区域提供多种情况。这是代码:

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

class Shape
{
public:
virtual double GetArea() const = 0
{

}

private:

};

class Circle : public Shape
{
public:
Circle(double p, double r) // constructor
{
pi = p;
radius = r;
}

Circle() : pi(3.14), radius(0) {} // default constructor

void SetRadius(double value)
{
radius = value;
}

double GetRadius() const
{
return radius;
}

double GetArea()
{
double area = pi * radius * radius;
return area;
}

private:
double pi = 3.14;
double radius;
};

class Rectangle : public Shape
{
public:
Rectangle(double a, double b) // constructor
{
sideA = a;
sideB = b;
}

Rectangle() : sideA(0), sideB(0) {} // default constructor

void SetSideA(double value)
{
sideA = value;
}

double getSideA() const
{
return sideA;
}

void SetSideB(double val)
{
sideB = val;
}

double getSideB() const
{
return sideB;
}

double getArea()
{
double Area = sideA * sideB;
return Area;
}

private:
double sideA;
double sideB;
};

int main()
{
vector<Shape*> shape;
return 0;
}

最佳答案

你想要多态性。您只需使用圆形或矩形的构造函数,例如:

vector<Shape*> shape(1);
if(/* case is circle*/)
shape[0] = new Circle();
else
shape[0] = new Rectangle();
<小时/>

但是,您需要删除基类中纯虚方法的定义,因为它是 inside the class ,并且仅声明它。

然后,您需要使用完全相同相同的原型(prototype)来实现该方法,因此您需要将 Circle 中的方法标记为 const也。矩形类也是如此,您也犯了一个拼写错误,因为方法的名称是“GetArea”,而不是“getArea”。

<小时/>

完整的最小工作示例:

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

class Shape
{
public:
virtual double GetArea() const = 0;
virtual ~Shape() {};
};

class Circle : public Shape
{
public:
Circle(double p, double r) // constructor
{
pi = p;
radius = r;
}

Circle() : pi(3.14), radius(0) {} // default constructor

void SetRadius(double value)
{
radius = value;
}

double GetRadius() const
{
return radius;
}

double GetArea() const
{
double area = pi * radius * radius;
return area;
}

private:
double pi = 3.14;
double radius;
};

class Rectangle : public Shape
{
public:
Rectangle(double a, double b) // constructor
{
sideA = a;
sideB = b;
}

Rectangle() : sideA(0), sideB(0) {} // default constructor

void SetSideA(double value)
{
sideA = value;
}

double getSideA() const
{
return sideA;
}

void SetSideB(double val)
{
sideB = val;
}

double getSideB() const
{
return sideB;
}

double GetArea() const
{
double Area = sideA * sideB;
return Area;
}

private:
double sideA;
double sideB;
};

int main()
{
vector<Shape*> shape(2);
shape[0] = new Circle(3.14, 1);
shape[1] = new Rectangle(2, 3);

for(auto s : shape)
std::cout << "Shape area = " << s->GetArea() << endl;

// When you are done, delete the dynamically allocated memory.
// You can use smart pointers in order to avoid doing this manually (and maybe forget!)
delete shape[0];
delete shape[1];
return 0;
}

输出:

形状面积 = 3.14形状面积 = 6

其中第一个区域来自圆形,第二个区域来自矩形。

我建议您阅读:Virtual destructor in polymorphic classesWhat is the 'override' keyword in C++ used for?

<小时/>

完成所有这些练习后,您应该真正开始使用 smart pointers ,例如像这样 std::vector<std::unique_ptr<Shape>> shape; ,而不是原始指针。这样,您就不必担心删除手动动态分配的内存。

关于c++ - 如何声明指向类对象的指针 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59272504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com