Tuesday, July 14, 2009

C++ Help Pllllease!!?

Hello, now for this class, I have to add accessor and mutator functions for all the data fields, but I have no idea how... any help is appreciated!!!





#include %26lt;iostream%26gt;


using namespace std;





class Rectangle


{


public:


Rectangle()


{


height= 0;


width = 0;


}





Rectangle(double h, double w)


{


height = h;


width = w;


}





double getHeight() // retrive the height to print in main


{


return height;


}





double getWidth() // retrive the width to print in main


{


return width;


}





double getArea()const


{


return height * width;


}





double getPerimeter()


{


return ((height * 2) + (width * 2));


}





private:


double height;


double width;


};





int main()


{


Rectangle rectangle1(4,40);


Rectangle rectangle2(3.5,35.9);





cout %26lt;%26lt;"The area of Rectangle 1 is: " %26lt;%26lt; rectangle1.getArea() %26lt;%26lt; endl;


cout %26lt;%26lt;"The perimeter of Rectangle 1 is: " %26lt;%26lt; rectangle1.getPerimeter() %26lt;%26lt; endl;


cout %26lt;%26lt;"The area of Rectangle 2 is: " %26lt;%26lt; rectangle2.getArea() %26lt;%26lt; endl;


cout %26lt;%26lt;"The perimeter of Rectangle 2 is: " %26lt;%26lt; rectangle2.getPerimeter()

C++ Help Pllllease!!?
You can use mutators with the following prototype





void setHeight(double h);


void setWidth(double w);





Here are the code changes required





#include %26lt;iostream%26gt;


using namespace std;





class Rectangle


{


public:


Rectangle()


{


height= 0;


width = 0;


}





Rectangle(double h, double w)


{


height = h;


width = w;


}





double getHeight() // retrive the height to print in main


{


return height;


}





double getWidth() // retrive the width to print in main


{


return width;


}





/////////////////////////////////////...


void setHeight(double h) // set the height -- mutator


{


height = h;


}





void setWidth(double w) // set the width -- mutator


{


width = w;


}


/////////////////////////////////////...








double getArea()const


{


return height * width;


}





double getPerimeter()


{


return ((height * 2) + (width * 2));


}





private:


double height;


double width;


};





int main()


{


Rectangle rectangle1(4,40);


Rectangle rectangle2(3.5,35.9);





cout %26lt;%26lt;"The area of Rectangle 1 is: " %26lt;%26lt; rectangle1.getArea() %26lt;%26lt; endl;


cout %26lt;%26lt;"The perimeter of Rectangle 1 is: " %26lt;%26lt; rectangle1.getPerimeter() %26lt;%26lt; endl;


cout %26lt;%26lt;"The area of Rectangle 2 is: " %26lt;%26lt; rectangle2.getArea() %26lt;%26lt; endl;


cout %26lt;%26lt;"The perimeter of Rectangle 2 is: " %26lt;%26lt; rectangle2.getPerimeter();





/////////////////////////////////////...


// setting new values to rectangle 2


rectangle2.setHeight(5.55);


rectangle2.setWidth(1.11);





cout%26lt;%26lt;"\n AREA WITH NEW VALUES "%26lt;%26lt;endl;


cout %26lt;%26lt;"The area of Rectangle 2 is: " %26lt;%26lt; rectangle2.getArea() %26lt;%26lt; endl;


cout %26lt;%26lt;"The perimeter of Rectangle 2 is: " %26lt;%26lt; rectangle2.getPerimeter();





/////////////////////////////////////...





}
Reply:Accessors are your get functions (getHeight, getWidth). They provide access to the private variables in your class. You already have those.





What your class doesn't have is the mutators which are set functions (setHeight, setWidth). They need to take in a double parameter and make the private variable (eg. private double height) equal to that parameter.





Hope that gives you some clues.


No comments:

Post a Comment