// dog.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
class Rectangle {
public:
Rectangle(int x, int y){
length= x ;
width=y ;
}
Rectangle(int a){
side=a;
}
void area1();
void area2();
private:
int length;
int width;
int side;
int square;
};
int _tmain(int argc, _TCHAR* argv[])
{ int x, y,cls,a;
cout << "请选择计算长方形面积输入1 计算正方形面积输入2:" << endl;
cin >> cls;
if (cls == 1) {
cout << "请输入长方形的长和宽:" << endl;
cin >>x >>y;
Rectangle(x, y);
void area1();
}
else if(cls==2){
cout << "请输入正方形的边长"<< endl;
cin>>a;
Rectangle(a);
void area2();
}
return 0;
}
void Rectangle::area1(){
square=length*width;
cout<<"长方形的面积是"<<square<<endl;}
void Rectangle::area2(){
square=side*side;
cout<<"正方形的面积是"<<square<<endl;}
重载函数与内联结合计算面积,无默认构造函数是什么意思
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 晓月清风・▽ ・ 2021-09-27 20:32关注
你好像没有实例化对象
Rectangle是构造函数,只有创建对象时才能调用添加一个类成员函数setside()
void Rectangle::setside(int n) { side = a; }
34行应该是
Rectangle rect(x, y);
35行:
rect.area1();
40 41行去掉
然后在那添加:rect.setside(a); rect.area2();
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 yolov8边框坐标
- ¥15 matlab中使用gurobi时报错
- ¥15 WPF 大屏看板表格背景图片设置
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真