Shape * sp; sp=new Circle(10); cout<<sp->GetPerim ()<<endl; sp=new Rectangle(6,4); cout<<sp->GetPerim()<<endl;
时间: 2024-04-26 09:26:58 浏览: 17
这段代码创建了一个指向 Shape 基类的指针 sp,并通过 new 运算符在堆上分别创建了 Circle 和 Rectangle 对象,然后分别调用了它们的 GetPerim 函数输出周长。
具体来说,sp=new Circle(10); 语句创建了一个半径为 10 的 Circle 对象,并将其地址赋给 sp 指针;cout<<sp->GetPerim()<<endl; 语句通过指针调用了 Circle 类中的 GetPerim 函数计算圆的周长,并输出结果。
接着,sp=new Rectangle(6,4); 语句创建了一个长宽分别为 6 和 4 的 Rectangle 对象,并将其地址赋给 sp 指针;cout<<sp->GetPerim()<<endl; 语句通过指针调用了 Rectangle 类中的 GetPerim 函数计算矩形的周长,并输出结果。
需要注意的是,这里使用了多态性,即通过基类指针调用派生类的函数。由于 GetPerim 函数在 Shape 基类中被声明为虚函数,因此在运行时会根据对象的实际类型调用相应的函数,实现了多态性。
相关问题
声明一个Shape抽象类,在此基础上派生出矩形Rectangle和圆Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( )函数计算对象的周长。完成类的设计,其中PI取3.14159。 主程序如下(不能修改): int main() { double radius; //圆半径 double a,b; //矩形长和宽 cin >> radius >> a >> b; Circle c(radius); Rectangle rect(a, b); Shape* pShapeArray[] = {&c, &rect}; for (int i = 0; i < 2; i++) { cout << "perimeter:" << pShapeArray[i]->GetPerim( ) << endl; cout << "area:" << pShapeArray[i]->GetArea() << endl; } return 0; }
以下是Shape、Rectangle和Circle类的设计:
```c++
#include <iostream>
using namespace std;
const double PI = 3.14159;
class Shape {
public:
virtual double GetArea() = 0; // 纯虚函数,计算面积
virtual double GetPerim() = 0; // 纯虚函数,计算周长
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double GetArea() {
return length * width;
}
double GetPerim() {
return 2 * (length + width);
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double GetArea() {
return PI * radius * radius;
}
double GetPerim() {
return 2 * PI * radius;
}
};
```
在主程序中,定义一个Shape类型的指针数组,将Circle和Rectangle对象的地址存储在其中。通过循环遍历该数组,分别调用每个对象的GetArea和GetPerim函数输出结果。
```c++
int main() {
double radius;
double a, b;
cin >> radius >> a >> b;
Circle c(radius);
Rectangle rect(a, b);
Shape* pShapeArray[] = { &c, &rect };
for (int i = 0; i < 2; i++) {
cout << "perimeter:" << pShapeArray[i]->GetPerim() << endl;
cout << "area:" << pShapeArray[i]->GetArea() << endl;
}
return 0;
}
```
#include <iostream> using namespace std; class Shape { public: Shape(){ } ~Shape(){ } (1) ; }; class Rectangle : public Shape{ public: (2) L(i),W(j) { } ~Rectangle(){ } double GetPerim() { return 2 * (L + W); } private: double L,W; }; class Circle :public Shape{ public: (3) R(r) { } double GetPerim() { return 3.14 * 2 * R; } private: double R; }; int main(){ (4) ; sp = new Circle(10); cout<<sp->GetPerim()<<endl (5) ; sp = new Rectangle(6, 4); (6) ; delete sp; return 0; }//输出62.8 20
题目中给出的代码不完整,无法进行编译和执行。在缺失的部分填写代码后,可以得到输出为62.8和20。
完整的代码如下所示:
```
#include <iostream>
using namespace std;
class Shape {
public:
Shape(){ }
virtual ~Shape(){} // 1. 在基类中添加虚析构函数
virtual double GetPerim() = 0; // 2. 在基类中添加纯虚函数
};
class Rectangle : public Shape{
public:
Rectangle(double i, double j) : L(i), W(j){ }
~Rectangle(){ }
double GetPerim() { return 2 * (L + W); }
private:
double L, W;
};
class Circle :public Shape{
public:
Circle(double r) : R(r){ }
double GetPerim() { return 3.14 * 2 * R; }
private:
double R;
};
int main(){
Shape *sp = nullptr; // 4. 定义指向基类的指针
sp = new Circle(10);
cout<<sp->GetPerim()<<endl; // 5. 输出圆的周长
sp = new Rectangle(6, 4);
cout<<sp->GetPerim()<<endl; // 6. 输出矩形的周长
delete sp;
return 0;
}
```
在代码中,我们做了以下修改:
1. 在基类Shape中添加了虚析构函数,以确保在通过基类指针删除派生类对象时能够正确调用派生类的析构函数。
2. 在基类Shape中添加了纯虚函数GetPerim(),以确保派生类必须实现该函数。
3. 在派生类Rectangle和Circle中分别实现了GetPerim()函数。
4. 在main函数中定义了一个指向基类的指针sp。
5. 通过指针sp创建了一个Circle对象,并调用了GetPerim()函数输出圆的周长。
6. 通过指针sp创建了一个Rectangle对象,并调用了GetPerim()函数输出矩形的周长。
因此,输出结果为62.8和20。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)