a C++ newbie here.. I am doing the question at the end of Ch3 which is about classes, objects, and constructors.
I'm stuck on a few that asks you to write a C++ statement for each: (On 5, I wasn't sure how to give a value in the same statement.. not sure about the rest either)..
Write a C++ statement that defines a Square object named square1 with side length 5.
square1 (int sidelength); // created a Square object called square1 with a side length, how do I give it a value in the same statement.
Write a C++ statement that changes the side length of object square1 to 10.
Square1.sidelength = 10
Write a C++ statement that prints the side length of object square1.
cin >> square1;
Write a C++ statement that prints the area of object square1.
cin >> "area = " >> square1 * square1 ;
(here is the exp of the square class)
class Square
{
public:
Square(int initialLength)
{
length = initialLength;
}
int getLength()
{
return length;
}
void setLength(int newLength)
{
length = newLength;
}
int area()
{
return length*length;
}
private:
int length;
};