文件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;
}