如何使用类获取矩形面积?
How to get area of rectangle using class?
我的任务是使用class计算矩形的面积和周长。输入函数应该在类内。到目前为止,我编写了代码,但似乎有一些我无法检测到的错误。
一些帮助将不胜感激。
矩形.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
public:
int width, length;
Rectangle();
Rectangle(int width1,int length1);
void getRectangle();
int getArea();
int getPerimeter();
};
#endif // RECTANGLE_H
矩形.cpp
// oporer ta class
#include<iostream>
#include "rectangle.h"
Rectangle::Rectangle()
{
width=0;
length=0;
}
Rectangle::Rectangle(int width1,int length1)
{
width=width1;
length=length1;
}
void Rectangle::getRectangle()
{
cin>>width>>length;
}
int Rectangle::getArea()
{
return (width*length);
}
int Rectangle::getPerimeter()
{
return (width+length)*2
}
// oporer ta rectangle cpp
主.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle paraFirst();
paraFirst.getRectangle();
return 0;
}
// main fucntion
假设#include
在您的本地设置中工作,我在这里看到两个拼写错误:
cin>>width>>length;
必须std::cin >> width >> length;
return (width+length)*2
后缺少分号
可能是主要问题:
Rectangle paraFirst();
被解析为不带参数并返回Rectangle
的函数的声明。另请参阅最烦人的解析。要调用默认构造函数,只需使用
Rectangle paraFirst;
或
Rectangle paraFirst{};
相关文章:
- 使用C++获取程序的 ASM
- 如何使用C++获取/设置OBS中的垂直滚动过滤器属性?
- 使用C 获取Linux进程列表和状态
- MacOS - 如何使用 C/C++ 获取具有 PID 的进程用户/所有者
- 如何使用C 获取周期十进加点号的平方根
- 使用 C/C++ 获取错误消息MSB6006错误代码 2
- 使用 c++ 获取两个日期与时间字符串之间的差异(以秒为单位)
- 使用 C# 获取 OpenGL 版本的最简单方法
- 我有一个构造函数,但我需要将输出返回到 main.cpp.我无法使用获取字符串功能!!任何想法如何实现这一目标?
- 如何在 Windows 中使用 C++ 获取 MAC 地址
- 使用C 获取Java对象来调用Java方法
- 如何将命令写入TTYACM0设备,并使用C/C 获取所得信息数据
- 是否可以使用 c++ 获取与 WinCE 中指定进程关联的所有 Dll 的内存使用情况?
- 如何使用g++获取C++头文件(.hpp)的程序集代码
- 使用 C/C++ 获取正在运行的 PID 的完整路径,而无需使用系统功能 (Linux)
- 如何使用C++获取SQL数据库的总大小
- 如何使用C++获取自动重置事件的状态?
- 在 AIX 上使用 C++ 获取系统型号名称
- 在 Z3 中使用 C++ 获取 Unsat 核心,并Z3_parse_smtlib2_string
- 使用 C++ 获取默认的 Windows 文本编辑器.exe路径