2012-11-04 2 views
0

기본적으로 사각형 클래스 인 초급 C++ 프로그램을 작성했습니다. 프로그램이 잘 컴파일, 내가 가지고있는 유일한 문제는 너비에 대한 제로를 반환하고 나는 이유를 파악할 수 없다는 것입니다. 나는 누군가가 나에게 잘못 가고 나를 도와 줄 수 있는지 정말로 고맙게 생각합니다.기본을 반환하는 C++ 프로그램

#pragma once 
class theRectangle 
{ 
private: 
    int height; 
    int width; 

public: 


    theRectangle(); 

    theRectangle(int _height, int _width); 

    int calcArea() const; 

    int calcPerimeter() const; 

    bool is_Square()const; 

    int Get_Height() const; 

    void Set_Height(int _hight); 

    int Get_Width() const; 

    void Set_Width(int _width); 

}; 



#include "theRectangle.h" 
#include <iostream> 
using namespace std; 
//default constructor 
theRectangle::theRectangle() 
{ 
    height = 0; 
    width = 0; 
} 

//parameterized constructor 
theRectangle::theRectangle(int _height, int _width) 
{ 

    height = _height; 
    width = _width; 

} 
//get height function 
int theRectangle::Get_Height() const 
{ 

    return height; 
} 
//get width function 
int theRectangle::Get_Width() const 
{ 

    return width; 
} 

//calculate area function 
int theRectangle::calcArea() const 
{ 
    return (height*width); 
} 
//calculate perimiter function 
int theRectangle::calcPerimeter() const 
{ 
    return (height+height+width+width); 
} 
//is square function 
bool theRectangle::is_Square()const 
{ 
    if(height == width) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

//set height function 
void theRectangle::Set_Height(int _height) 
{ 
    height = _height; 
} 
//set width function 
void theRectangle::Set_Width(int _width) 
{ 
    height = _width; 
} 





#include "theRectangle.h" 
#include <iostream> 
#include <string> 


using namespace std; 

void printRectangle(const theRectangle& rec); 

int main() 
{ 



    theRectangle rectangleONE; 
    int number; 
    int numbertwo; 


    cout << "\n Please type the height the 1st rectangle: "; 
    cin >> number; 
    rectangleONE.Set_Height(number); 
    cout << "\n Please type the width of the 1st rectangle: "; 
    cin >> numbertwo; 
    rectangleONE.Set_Width(numbertwo); 


    theRectangle rectangleTWO; 
    int numberthree; 
    int numberfour; 
    //ask user/save data 
    cout << "\n Please type the height of the 2nd rectangle: "; 
    cin >> numberthree; 
    rectangleTWO.Set_Height(numberthree); 
    cout << "\n Please type the width of the 2nd rectangle: "; 
    cin >> numberfour; 
    rectangleTWO.Set_Width(numberfour); 


    theRectangle rectangleTHREE; 
    int numberfive; 
    int numbersix; 


    cout << "\n Please type the height of the 3rd rectangle: "; 
    cin >> numberfive; 
    rectangleTHREE.Set_Height(numberfive); 
    cout << "\n Please type the width of the 3rd rectangle: "; 
    cin >> numbersix; 
    rectangleTHREE.Set_Width(numbersix); 

    //print data 
    cout << "\nFor rectangle one: "; 
    printRectangle(rectangleONE); 
    cout << "\nFor rectangle two: "; 
    printRectangle(rectangleTWO); 
    cout << "\nFor rectangle three: "; 
    printRectangle(rectangleTHREE); 

system("PAUSE"); 
return 0; 
} 


//print rectangle function 
// Purpose: print info on rectangle 
// Parameters:none 
// Returns: none 
void printRectangle(const theRectangle& rec) 
{ 
    cout << "\nHeight is: " << rec.Get_Height(); 
    cout << "\nWidth is: "  << rec.Get_Width(); 
    cout << "\nArea is: "  << rec.calcArea(); 
    cout << "\nPerimeter is: " << rec.calcPerimeter(); 
    if (rec.is_Square()) 
     cout << "\n This rectangle is a Square. "; 

} 

답변

5

귀하의 setWidth 기능은 실제로 0으로 width 멤버 세트를 떠나, 당신의 height 멤버 변수를 설정하는 것입니다 :

//set width function 
void theRectangle::Set_Width(int _width) 
{ 
    height = _width; 
} 

그것은해야한다 :

//set width function 
void theRectangle::Set_Width(int _width) 
{ 
    width = _width; 
} 
+0

하하 난, 감사 블라인드 충당하고 . –

+1

복사/붙여 넣기의 위험! – aardvarkk

+0

@JayRogers 도움이되어 기쁘다. 이와 같은 실수는 쉽게 만들 수 있으며, 다행스럽게도 해결하기 쉽습니다. 내 대답이 도움이된다면 옆에있는 녹색 체크를 클릭하여 동의하십시오. 감사. –

관련 문제