天天看點

(C++ 面向對象)設計一個類,計算長方形的面積和周長。

#include<iostream>
using namespace std; 
class Rectangle
{
    private:             //資料段 
	double length;
	double width;         
    public:                  //函數段 
	void set(double l,double w) //擷取資料
        {  
		length = l;
		width=w;
	}
	double getGirth()  //求周長
	{
		return 2 *(length+width);
	}       
	
	double getArea()//求面積 
	{
		return  length* width;
	}           	
};         //定義類 
 
 
int main() 
{
	Rectangle myclass; //定義一個自己的類 
	myclass.set(3,4);   //輸入資料 
	double girth,area;
	girth = myclass.getGirth();    //調用類中的函數 
	area = myclass.getArea();
	cout << "矩形的周長" << girth << endl; //輸出周長和面積 
	cout << "矩形的面積" << area << endl;
	return 0;
}
           

各位看客覺得有用請給個贊哦