作业矩形C++代码

作业矩形C++代码

作业矩形C++代码

文件Rectangle.h

#include <iostream>
using namespace std;
class Rectangle {
public:
    int height;
    int width;
public:
    //构造函数
    Rectangle();
    Rectangle(int height, int width);
    int getPerimeter();
    int getArea();
    void print();
};

Rectangle.cpp

#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle() {
    cout<<"无参函数被调用了"<<endl;
}
Rectangle::Rectangle(int height, int width)  {
    this->height = height;
    this->width = width;
}
int Rectangle::getPerimeter() {
    return (height+width)*2;
}
int Rectangle::getArea() {
    return (height*width);
}
void Rectangle::print() {
    cout<<"长度是:"<<height<<",宽度是:"<<width<<endl;
}

TestRectangle.cpp

#include <iostream>
#include "Rectangle.h"
using namespace std;
int main() {
    int height;
    int width;
    cout<<"请输入长度"<<endl;
    cin>>height;
    cout<<"请输入宽度"<<endl;
    cin>>width;
    Rectangle rectangle(height,width);
    rectangle.print();
    int r = rectangle.getPerimeter();
    cout<<"周长是"<<r<<endl;
    int a = rectangle.getArea();
    cout<<"面积是"<<a<<endl;
    return 0;
}

 

 

上一篇:Django个人博客搭建教程---基于dwebsocket的实时日志系统


下一篇:vue 由 clearTimeout无法清除定时器引发的vue 周期函数,事件代码执行顺序思考