物件導向程式設計C++

Jesse_suning發表於2018-10-27

下面是一組研究長方形問題的C++程式碼,執行出錯,懇請程式設計大佬幫我指正錯誤,萬分感謝。

// oop.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"



#include<iostream>

using namespace std;



class Rectangle

{

Rectangle();

Rectangle(float _len,float _wid);

float perimeter();

float Area();

float GetLength();

float GetWidth();

void SetLength(float _len);

void SetWidth(float _wid);

void Printf();

private:

float len,wid;

};

Rectangle::Rectangle()

{

len=0;

wid=0;

}

Rectangle::Rectangle(float _len,float _wid)

{

len=_len;

wid=_wid;

}

float Rectangle::perimeter()

{

return 2*(len+wid);

}

float Rectangle::Area()

{

return len*wid;

}

float Rectangle::GetLength()

{

return len;

}

float Rectangle::GetWidth()

{

return wid;

}

void Rectangle::SetLength(float _len)

{

len=_len;

}

void Rectangle::SetWidth(float _wid)

{

wid=_wid;

}

void Rectangle::Printf()

{

cout<<"length: "<<len<<"width: "<<wid<<endl;

}





int main(int argc, char* argv[])

{

float len,wid;

cout<<"input length and width";

cin>>len>>wid;

Rectangle r,r1(len,wid);

r.SetLength(10);

r.SetWidth(5);

r.Printf();

r.Printf();

cout<<"area is"<<r1.Area()<<endl;



return 0;

}

相關文章