Answer to Question #180889 in C++ for jordan

Question #180889

a)        Write code to create ‘Shape’ class and include following:             

  1. Create these private data members in Shape class: width (float), length (float), name (string)
  2. Create wrappers (getters/setters) for each data member
  3. Create a constructor that initialises data members and a destructor that outputs data members
  4. Create a copy constructor in the class and use it in main function
  5. Include meaningful composition in the Shape class and use it in main function

  


1
Expert's answer
2021-04-14T15:59:38-0400
#include <iostream>


using namespace std;


class Shape
{
private:
    float length;
    float width;
    string name;


public:
    void setLength(float length)
    {
        this->length = length;
    }
    void setWidth(float width)
    {
        this->width = width;
    }
    void setName(string name)
    {
        this->name = name;
    }


    float getLength() const
    {
        return length;
    }
    float getWidth() const
    {
        return width;
    }
    string getName() const
    {
        return name;
    }


    Shape(float length, float width, string name)
    {
        setLength(length);
        setWidth(width);
        setName(name);
    }
    Shape(const Shape &shape)
    {
        setLength(shape.getLength());
        setWidth(shape.getWidth());
        setName(shape.getName());
    }


    ~Shape()
    {
        cout << "name: " << name << endl;
        cout << "length: " << length << endl;
        cout << "width: " << width << endl << endl;
    }
};


int main()
{
    Shape rectangle1(5,10,"rectangle");
    Shape rectangle2 = rectangle1;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS