1.Rectangle.h
#include<iostream>
using namespace std;
class Rectangle {
private:
int height;
int width;
public:
Rectangle();
Rectangle(int height, int width);
int getPerimeter();
int getPArea();
void print();
};
2.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 (width+height)*2; } int Rectangle::getPArea(){ ? ? return width*height; } void Rectangle::print(){ ? ? cout<<"长度是:"<<width<<",高度是:"<<height<<endl; }
3.TestRectangle.cpp
#include <iostream> #include "Rectangle.h"
using namespace std; int main() { ? ? int height,width; ? ? cout <<"请输入长度"<<endl; ? ? cin>>width; ? ? cout <<"请输入高度"<<endl; ? ? cin>>height; ? ? Rectangle rectangle(height,width); ? ? rectangle.print(); ? ? int r = rectangle.getPArea(); ? ? int p = rectangle.getPerimeter(); ? ? cout<<"面积是:"<<r<<endl; ? ? cout<<"周长是:"<<p<<endl; ? ? return 0; }
?题目要求:
?
|