编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle和Square,
//文件名:main.cpp#include<iostream>#include<string>//预处理命令usingnamespacestd;//使用标准命名空间st...
// 文件名: main.cpp
#include <iostream>
#include <string> // 预处理命令
using namespace std; // 使用标准命名空间std
const double PI = 3.1415926; // 常量PI
// 形状类
class Shape
{
public:
virtual void ShowArea() const = 0;
static double sum;
};
class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) // 构造函数
{ sum += PI * radius * radius; }
virtual void ShowArea() const // 显示圆形相关信息
{
cout << "圆形:" << endl;
cout << "半径:" << radius << endl; // 显示半径
cout << "面积:" << PI * radius * radius << endl; // 显示面积
}
};
class Rectangle: public Shape
{
private:
double height; // 高
double width; // 宽
public:
Rectangle(double h, double w): height(h), width(w) // 构造函数
{ sum += height * width; }
virtual void ShowArea() const // 显示矩形相关信息
{
cout << "矩形:" << endl;
cout << "高:" << height << endl; // 显示高
cout << "宽:" << width << endl; // 显示宽
cout << "面积:" << height * width << endl; // 显示面积
}
};
class Square: public Shape
{
private:
double length;
public:
Square(double l):length(l)
{sum+=length*length;}
virtual void ShowArea() const
{
cout << "正方形:" << endl;
cout << "长度:" << length << endl;
cout << "面积:" << length * length << endl;
}
};
double Shape::sum = 0;
int main()
{
Circle c1(3.0);
Rectangle r1(4.2,5.5);
Square s1(1.2);
Shape *arr[3]={&c1,&r1,&s1};
for(int i=0;i<3;i++)
{
cout<<arr[i]->ShowArea()<<endl;
}
delete arr;
return 0;
}
运行有错 求指教 展开
#include <iostream>
#include <string> // 预处理命令
using namespace std; // 使用标准命名空间std
const double PI = 3.1415926; // 常量PI
// 形状类
class Shape
{
public:
virtual void ShowArea() const = 0;
static double sum;
};
class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) // 构造函数
{ sum += PI * radius * radius; }
virtual void ShowArea() const // 显示圆形相关信息
{
cout << "圆形:" << endl;
cout << "半径:" << radius << endl; // 显示半径
cout << "面积:" << PI * radius * radius << endl; // 显示面积
}
};
class Rectangle: public Shape
{
private:
double height; // 高
double width; // 宽
public:
Rectangle(double h, double w): height(h), width(w) // 构造函数
{ sum += height * width; }
virtual void ShowArea() const // 显示矩形相关信息
{
cout << "矩形:" << endl;
cout << "高:" << height << endl; // 显示高
cout << "宽:" << width << endl; // 显示宽
cout << "面积:" << height * width << endl; // 显示面积
}
};
class Square: public Shape
{
private:
double length;
public:
Square(double l):length(l)
{sum+=length*length;}
virtual void ShowArea() const
{
cout << "正方形:" << endl;
cout << "长度:" << length << endl;
cout << "面积:" << length * length << endl;
}
};
double Shape::sum = 0;
int main()
{
Circle c1(3.0);
Rectangle r1(4.2,5.5);
Square s1(1.2);
Shape *arr[3]={&c1,&r1,&s1};
for(int i=0;i<3;i++)
{
cout<<arr[i]->ShowArea()<<endl;
}
delete arr;
return 0;
}
运行有错 求指教 展开
展开全部
delete arr;
arr 是数组,释放内存的语法是:
delete[] arr;
arr 是数组,释放内存的语法是:
delete[] arr;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已修改,你看看
int main()
{
Circle c1(3.0);
Rectangle r1(4.2,5.5);
Square s1(1.2);
Shape *arr[3]={&c1,&r1,&s1};
for(int i=0;i<3;i++)
{
//cout<<arr[i]->ShowArea()<<endl; //错误
arr[i]->ShowArea();
}
//delete arr; //不是new出来的对象,不用delete
return 0;
}
int main()
{
Circle c1(3.0);
Rectangle r1(4.2,5.5);
Square s1(1.2);
Shape *arr[3]={&c1,&r1,&s1};
for(int i=0;i<3;i++)
{
//cout<<arr[i]->ShowArea()<<endl; //错误
arr[i]->ShowArea();
}
//delete arr; //不是new出来的对象,不用delete
return 0;
}
追问
运行没错了 结果一闪而过.....是不是程序还是有问题啊?
追答
按Ctrl+F5运行!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询