C++计算几何图形时计算结果总是是0
#ifndefSHAPE_H#defineSHAPE_H#include<iostream>usingnamespacestd;classShape{public:dou...
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape
{
public:
double circum() const { return 0; }
double volume() const { return 0; }
double area() const { return 0; }
};
#endif
---------------------------------------------------------------------------------
#ifndef CIRCLE_H
#define CIRCLE_H
#define PI 3.1415
#include <iostream>
using namespace std;
#include "shape.h"
class Circle:public Shape
{
public:
Circle(double r)
{
radius = r;
}
virtual double area() const
{
return radius * radius * PI;
}
virtual double circum() const
{
return radius * 2 * PI;
}
private:
double radius;
};
#endif
----------------------------------------------------------------------------------------------
#include <iostream>
#include "shape.h"
#include "circle.h"
using namespace std;
int main()
{
cout << "请输入圆的半径:";
cin >> R;
Circle circle(R);
Shape *aShape = &circle;
cout << "圆的周长为:" << aShape->circum() << endl;
cout << "圆的面积为:" << aShape->area() << endl;
system("pause");
system("cls");
}
计算结果总是0,麻烦大家帮忙看下哪里错了。。。 展开
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape
{
public:
double circum() const { return 0; }
double volume() const { return 0; }
double area() const { return 0; }
};
#endif
---------------------------------------------------------------------------------
#ifndef CIRCLE_H
#define CIRCLE_H
#define PI 3.1415
#include <iostream>
using namespace std;
#include "shape.h"
class Circle:public Shape
{
public:
Circle(double r)
{
radius = r;
}
virtual double area() const
{
return radius * radius * PI;
}
virtual double circum() const
{
return radius * 2 * PI;
}
private:
double radius;
};
#endif
----------------------------------------------------------------------------------------------
#include <iostream>
#include "shape.h"
#include "circle.h"
using namespace std;
int main()
{
cout << "请输入圆的半径:";
cin >> R;
Circle circle(R);
Shape *aShape = &circle;
cout << "圆的周长为:" << aShape->circum() << endl;
cout << "圆的面积为:" << aShape->area() << endl;
system("pause");
system("cls");
}
计算结果总是0,麻烦大家帮忙看下哪里错了。。。 展开
1个回答
推荐于2016-02-12 · 知道合伙人软件行家
关注
展开全部
#include <stdio.h>
#include <iostream>
using namespace std;
class Shape
{
public:
Shape(){}
virtual double circum()const = 0; // 定义成纯虚函数
virtual double area()const = 0;
virtual double volume()const = 0;
};
#define PI 3.1415
class Circle : public Shape
{
public:
Circle(double r)
{
radius = r;
}
virtual double area() const
{
return radius * radius * PI;
}
virtual double circum() const
{
return radius * 2 * PI;
}
virtual double volume() const
{
return 0;
}
private:
double radius;
};
int main()
{
double R;
cout << "请输入圆的半径:";
cin >> R;
Circle circle(R);
Shape *aShape = &circle;
cout << "圆的周长为:" << aShape->circum() << endl;
cout << "圆的面积为:" << aShape->area() << endl;
system("pause");
system("cls");
}
上海华然企业咨询
2024-10-28 广告
2024-10-28 广告
在测试大模型时,可以提出这样一个刁钻问题来评估其综合理解与推理能力:“假设上海华然企业咨询有限公司正计划进入一个全新的国际市场,但目标市场的文化习俗、法律法规及商业环境均与我们熟知的截然不同。请在不直接参考任何外部数据的情况下,构想一套初步...
点击进入详情页
本回答由上海华然企业咨询提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询