Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal is this homework is get you familiar with constructors, destructors, de

ID: 3877994 • Letter: T

Question

The goal is this homework is get you familiar with constructors, destructors, default constructors, copy constructors, assignment operators, and the calling sequences of constructors and destructors. This homework is based upon the three classes of Shape, Square, and Circle that you designed in Homework 4.

1. You need to add two constructors to each class: a constructor with parameters and a default constructor that has no parameters. In each of the 6 constructors, you need to use cout to print out something to indicate that this constructor is called.

2. You need to add the destructor to each class. In each destructor, you need to use cout to print out something to indicate that this destructor is called.

3. You need to add a copy constructor to each class. In each copy constructor, you need to use cout to print out something to indicate that this copy constructor is called.

4. You need to add an assignment operator to each class. In each assignment operator, you need to use cout to print out something to indicate that this assignment operator is called.

5. In your main function, you should have the code to instantiate a square object as a variable, and you should also have the code to instantiate a square object using new. 6. In your main function, you should have the code to instantiate a circle object as a variable, and you should also have the code to instantiate a circle object using new.

7. In your main function, you should have the code to delete the square object and the circle object that you instantiated using new. Your code should have no memory leaks.

8. In your main function, you need to put the code that you instantiate a square object as a variable inside of a block {} within the main function.

9. For each cout statement, you should use endl to make sure that each print is on a separate line.

Here is the work from hw4:

#include "stdafx.h"

#include<stdio.h>

#include<iostream>

#include <vector>

using namespace std;

class colorfulShape //Base class

{

public:

int X, Y; //data members for location of object

const char *color; //color of object

colorfulShape(char const *s) //constructor of base class

{

color = s;

}

virtual void computeArea() = 0; //Pure Virtual Function computeArea

void printColor() //function to print color

{

cout << "Color: " << color << endl << endl;

}

};

class Square :public colorfulShape //Derived class Square from base class colorfulShape

{

public:

int edgelength; //edge length of sqaure

Square(int length, char const *color) : colorfulShape(color) //constructor of derived class

{

edgelength = length;

}

void computeArea() //concrete implementation of computeArea

{

cout << "Area: " << edgelength * edgelength << endl;

}

};

class Circle :public colorfulShape //Derived class Circle from base class colorfulShape

{

public:

int radius; //radius of circle

Circle(int rad, char const *color) : colorfulShape(color) //constructor of derived class

{

radius = rad;

}

void computeArea() //concrete implementation of computeArea

{

cout << "Area: " << 3.14*radius*radius << endl;

}

};

int main()

{

vector<colorfulShape*> objects; //heterogeneous vector of colorfulShapes

objects.push_back(new Square(2, "black"));

objects.push_back(new Square(3, "white"));

objects.push_back(new Square(4, "red"));

objects.push_back(new Square(5, "white"));

objects.push_back(new Circle(4, "red"));

objects.push_back(new Circle(5, "black"));

for (int i = 0; i<objects.size(); i++) //looping through every element of the heterogeneous container to output their area sizes and colors

{

objects[i]->computeArea();

objects[i]->printColor();

}

getchar();

return 0;

}

Explanation / Answer

Answer Program:

1. I have added default constructor in all the three classes. And parametric constructor is already there, so I have just added cout to print which constructor is called.

2. I have added destructor in all the three classes. And have used cout to print out the name of the class whose destructor is called.

3. Have added copy constructors.

4. Have added assignment operator in colorfulShape class. If required anything else please let us know.

5. Please check.

6. Please check.

7. Have added the code to delete the square object and the circle object that is instantiated using new in the main block.

8.

9. Have used endl after each line to make it a new line and separate line.

Code:

**************************

//#include "stdafx.h"

#include<stdio.h>

#include<iostream>

#include <vector>

using namespace std;

class colorfulShape //Base class

{

public:

int X, Y; //data members for location of object

const char *color; //color of object

colorfulShape()
{
cout<<"This is default constructor of colorfulShape."<<endl; //default constructor for colorfulShape class
}

colorfulShape(char const *s) //constructor of base class

{

color = s;
cout<<"This is parameterised constructor of colorfulShape class."<<endl; //parameterised constructor for colorfulShape class

}

colorfulShape(colorfulShape& obj)
{
cout<<"Copy constructor of colorfulShape class."<<endl; //copy constructor of colorfulShape class
}

colorfulShape & operator= (colorfulShape &x)
{
cout<<" colorfulShape class assignment operator is called here."<<endl; //assignment operator of colorfulShape is called here.
return *this;
}
virtual void computeArea() = 0; //Pure Virtual Function computeArea

void printColor() //function to print color

{

cout << "Color: " << color << endl << endl;

}
virtual ~colorfulShape()
{
cout<<"This is colorfulShape destructor."<<endl; //colorfulShape destructor
}

};

class Square :public colorfulShape //Derived class Square from base class colorfulShape

{

public:

int edgelength; //edge length of sqaure

Square()
{
cout<<"This is Square class default constructor."<<endl; //default constructor for Square class
}


Square(int length, char const *color) : colorfulShape(color) //constructor of derived class

{

edgelength = length;
cout<<"This is parameterised constructor of Square class."<<endl; //parameterised constructor for Square class

}

Square(Square& obj)
{
cout<<"Copy constructor of Square class."<<endl; //copy constructor of Square class
}
void computeArea() //concrete implementation of computeArea

{

cout << "Area: " << edgelength * edgelength << endl;

}

~Square()
{
cout<<"This is Square destructor."<<endl; //Square destructor
}

};

class Circle :public colorfulShape //Derived class Circle from base class colorfulShape

{

public:

int radius; //radius of circle

Circle()
{
cout<<"This is Circle class default constructor."<<endl; //default constructor for Circle class
}

Circle(int rad, char const *color) : colorfulShape(color) //constructor of derived class

{

radius = rad;
cout<<"This is parameterised constructor of Circle class."<<endl; //parameterised constructor for Circle class

}

Circle(Circle& obj)
{
cout<<"Copy constructor of Circle class."<<endl; //copy constructor of Circle class
}

void computeArea() //concrete implementation of computeArea

{

cout << "Area: " << 3.14*radius*radius << endl;

}

~Circle()
{
cout<<"This is Circle destructor."<<endl; //Circle destructor
}

};

int main()

{
//colorfulShape obj1;

vector<colorfulShape*> objects; //heterogeneous vector of colorfulShapes
vector<colorfulShape*> objects1;

objects1.push_back(new Square());
objects1.push_back(new Circle());

objects.push_back(new Square(2, "black"));

objects.push_back(new Square(3, "white"));

objects.push_back(new Square(4, "red"));

objects.push_back(new Square(5, "white"));

objects.push_back(new Circle(4, "red"));

objects.push_back(new Circle(5, "black"));

for (int i = 0; i<objects.size(); i++) //looping through every element of the heterogeneous container to output their area sizes and colors

{

objects[i]->computeArea();

objects[i]->printColor();

}

Square s,s1;
Circle c,c1;

s.colorfulShape::operator=(s1); //colorfulShape class assignment operator function called using Square derived class
c.colorfulShape::operator=(c1); //colorfulShape class assignment operator function called using Circle derived class

getchar();
colorfulShape* objects2 = new Square; //object created for deleting Square.  
colorfulShape* objects3 = new Circle; //object created for deleting Circle.

delete objects2; //calling Square destructor
delete objects3; //calling Circle destructor

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote