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)..

  1. 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.

  2. Write a C++ statement that changes the side length of object square1 to 10.

    Square1.sidelength = 10

  3. Write a C++ statement that prints the side length of object square1.

    cin >> square1;

  4. 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;
};

Here is an example :
5. Write a C++ statement that defines a Square object named square1 with side length 5.

Square square1 = Square(5);

The above is assuming the class is Square is similar to this :

class Square{
private:
 int len_;
public:
  Square(const int Length) { len_ = Length; }
};

6. Write a C++ statement that changes the side length of object square1 to 10.

square.setLength( 10 );

The above assumes the class Square is defined as :

class Square{
private:
 int len_;
public:
 Square(const int Length) { len_ = Length; }
 void setLength(const int newLen) { len_ = newLen; }
}

7. Write a C++ statement that prints the side length of object square1.

It should be something like this :

square.printLength();

Assuming the class Square is similar to below :

class Square{
private:
 int len_;
public:
 Square(const int Length) { len_ = Length; }
 void setLength(const int newLen) { len_ = newLen; }
 void printLength(){ cout <<"Length = " << length << endl; }
}

8. Write a C++ statement that prints the area of object square1.

<you>"cin >> "area = " >> square1 * square1 ; "
Not quite right unless you overload the operator *;

It should be something like this :

square.printArea();

And Square is define as :

class Square{
private:
 int len_;
public:
 Square(const int Length) { len_ = Length; }
 void setLength(const int newLen) { len_ = newLen; }
 void printLength(){ cout <<"Length = " << len_<< endl; }
 void printArea(){ cout << len_ * len_ << endl; }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.