2012-05-19 2 views
-6

Моя задача - написать основную функцию, которая определяет массив объектов с именем recs [] с размером 4. Каждый элемент recs должен быть инициализирован по ширине и длине, и мне нужно рассчитать его площадь и окружность.Как я могу написать эту программу?

#include <iostream> 
#include <cmath> 
using namespace std; 

struct recs 
{ 
    double width; 
    double length; 
}; 

class rect 
{ 
    public: 
      rect(); 
      double area; 
      double circumference; 
      void setrect(); 
      void calarea(); 
      void calcircumf(); 
      void print(); 
      friend bool operator==(rect rec1,rect rec2); 
    private: 
      double width,length; 
      }; 
      rect recs[4]; 

bool operator==(rect rec1,rect rec2) 
{ 
    return (rec1.area == rec1.area && rec1.circumference == rec2.circumference); 
} 

rect::rect() 
{ 
    width=0.0; 
    length=0.0; 
} 

void rect::setrect() 
{ 
    cout << "width = "; 
    cin >> width; 
    cout << "length = "; 
    cin >> length; 
    print(); 
} 

void rect::calarea() 
{ 
    width == length; 
    cout << "the return value true" << endl; 
} 

void rect::calcircumf() 
{ 
    width == length; 
    cout << "the return value true" << endl; 
} 

void rect::print() 
{ 
    cout << "width = " << width << endl; 
    cout << "length = " << length << endl << endl; 
    } 

int main() 
{ 
    double area; 
    double circumference; 
    double width; 
    double length; 

    rect recs[4] = { {10.0, 12.0}, {12.0, 14.0}, {14.0, 16.0}, {16.0, 18.0} }; 

    for (int i = 0; i <= 4; i++) 
    { 
     area = width * length; 
     cout << "the area "<< i << " = " << area << endl ; 

     circumference = ((width + length) + (width + length)); 
     cout << "the circumference" << i << " = " << circumference << endl; 

    }; 

    system("pause"); 
    return 0; 
} 

Я уже редактировал этот код в течение 4 часов, но я не смог его скомпилировать.

+6

Если вы получаете ошибку компилятора, то, пожалуйста, выкладывает ** точного ** сообщение об ошибке здесь. Не просто публикуйте весь свой код и говорите «он не компилируется». –

+3

Y U НЕТ ИСПОЛЬЗОВАТЬ WHITESPACEZ? –

+4

'width == length;' это зачем, кстати? –

ответ

0

Посмотрите это.

Я надеюсь, что это помогает .. :)

//My task is to write the main function which defines an array of objects called recs[] with the size of 4. 
//Each element of recs must be initialized with width and length, and I have to calculate its area and circumference. 

#include <iostream> 

using namespace std; 

class Rect{ 

//Data Abstraction - Not letting the user access the class variables 
private:  
    double width; 
    double length; 

public: 

    Rect(){   //default constructor 

     width = 0.0; 
     length = 0.0; 

    } 

    //setters for width and length 
    void setWidthAndLength(double w, double l); 

    //area and circumference calculators 
    double area(); 
    double circumference();  
}; 

void Rect :: setWidthAndLength(double w, double l){ 

width = w; 
length = l; 

} 

double Rect :: area(){ 

return width * length; 

} 


double Rect :: circumference(){ 

return 2 * width + 2 * length; 

} 

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

Rect r[4]; 
int i; 
double width,length; 

for(i=0;i<4;i++){ 

    cout<<"Enter the width and length for rectangle "<<i<<endl; 
    cin>>width>>length; 

    //set the width 
    r[i].setWidthAndLength(width,length);    

    cout<<"The Area of the Rectangle is "<<r[i].area()<<endl; 
    cout<<"The Circumference of the Rectangle is "<<r[i].circumference()<<endl<<endl; 

} 

return 0; 
} 

мне было интересно, почему я не мог сделать this.width = w;, но в любом случае ..

+0

спасибо nimish ..... это действительно помогает ..... –

+0

@NurhidayahAbdullah Полезно знать :) Если это ваше решение, отметьте его, так как это поможет кому-то столкнуться с подобной проблемой :) – nimish

+0

btw, thanx again. –