⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectangle.cpp

📁 1.设计并测试一个名为Rectangle的矩形类
💻 CPP
字号:
//1.设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角
//与右上角两个点的坐标,能计算矩形的面积。

#include<iostream>
using namespace std;

class Rectangle
{
public:
	Rectangle(float x1,float x2,float y1,float y2);     //带参构造函数
	~Rectangle(){}                                      //析构函数
	float GetArea() 
    {
        return abs(rightx-leftx)*abs(topy-bottomy);     //矩形面积
    }
private:                                                //私有数据成员
	float leftx;
	float rightx;
	float topy;
	float bottomy;

};

Rectangle::Rectangle(float x1,float x2,float y1,float y2)//构造函数的实现
{
	leftx=x1;
	rightx=x2;
	topy=y2;
	bottomy=y1;
}

float  main()                                            //测试函数
{
	float left,right,bottom,top;
	cout<<"请输入左下角坐标:";
	cin>>left>>bottom;
	cout<<"请输入右上角坐标:";
	cin>>right>>top;
    Rectangle rect(left,right,bottom,top); 
    cout<<"矩形的面积是:"<<rect.GetArea()<<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -