In: Computer Science
answer in c++
First – take your Box02.cpp file and rename the file Box03.cpp
Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1;
I know you have lost sleep over this. So here is the answer……….
When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items.
Each class definition is automatically given a ‘default’ constructor. This constructor has no arguments and just creates your data items and methods when you create an object.
That is why we could say Box b1; The default constructor Box was given to us.
I know you remember the rules for naming a function.
return type function name (parameters)
any data type starts with a lower any number of
case letter data types
use ‘camel casing’ separated by ,’s
Here is another concept to ‘memorize’. The rules of naming a constructor, what a constructor does, and when a constructor is executed.
Rules for a constructor…………………..
What we have been doing is running the ‘default’ constructor that is automatically added to a class.
First – have only the code
Box b1;
in your main function. Compile Box03.cpp. Should compile using the ‘default’ constructor Box().
Now………………………………
1. add a constructor to your Box class named
Box(double x)
this method (constructor) has one parameters (x) and sets the length, width,
and height to x.
remember a constructor does not return anything, same name as the class
Box(double x)
{
setLength(x);
setWidth(x);
setHeight(x);
}
Do not change any code in main(). Try and compile Box03. You should get
an error saying that the Box() (the default) constructor is missing.
We get the default constructor only if we write no constructors of our own
As soon as we write a constructor we lose the default constructor. So we
have to write our own ‘default’ constructor – a constructor with no parameters.
2. Write the default constructor. Takes no parameters. Sets the length, width,
and height to 1.
3. Write a third constructor. This constructor has three parameters (l, w, h) and
sets the length to l, width to w, and height to h.
Since constructors are just special methods, we are using the principle of over loading that we will look at in the next Box assignment. We have three methods (constructors), named the same, but with different parameter lists.
leave the other functions in the Box class. Do not change these functions.
Test the Box class – a suggested course of action – in main
Each object should use a different constructor.
Box b1 = new Box;
Box b2 = new Box(23.7);
Box b3 = new Box(11.1, 22.2, 33.3);
b1.display();
b2,display();
b3.display();
should see the new values in the three Box objects
CODE
#include <iostream> using namespace std; class Box { private: double length; double width; double height; public: double getLength() {return length;} double getWidth() {return width;} double getHeight() {return height;} void setLength(double l) {length = l;} void setWidth(double w) {width = w;} void setHeight(double h) {height = h;} double initialize(double x); double calcVolume(); double calcArea(); void display(); }; void Box::display() { cout << endl; cout << "Length = " << getLength() << "\n"; cout << "Width = " << getWidth() << "\n"; cout << "Height = " << getHeight() << "\n"; cout << "Volume = " << calcVolume() << "\n"; cout << "Area = " << calcArea() << "\n"; cout << endl; } double Box::calcVolume() { return width * length * height; } double Box::calcArea() { return length * width; } double Box::initialize(double x) { width = x; length = x; height = x; return 0; } void testBox01() { Box b1; b1.display(); b1.setLength(13); b1.setWidth(4); b1.setHeight(9); b1.display(); cout << "Length: " << b1.getLength() << "\n"; cout << "Width: " << b1.getWidth() << "\n"; cout << "Height: " << b1.getHeight() << "\n"; } void testBox02() { Box b1; b1.display(); b1.initialize(5); b1.display(); } int main() { testBox01(); testBox02(); return 0; }
Solution: Complete code has been provided below. Comments have been placed to depict the functionality of the code added to the given code.
Notes : - The three different constructors are created in the the Box Class.
In the main function, the code to test the new constructors has been placed inside the followig comments.
/*Code to test the overloaded constructors */
/*End of the code */
2. in the given example the code has been given to use New operator while creating the objects.That will throw error as new() can be used with pointers only. Have added the correct code without using the new().
#include <iostream>
using namespace std;
class Box
{
private:
double length;
double width;
double height;
public:
double getLength() {return length;}
double getWidth() {return width;}
double getHeight() {return height;}
void setLength(double l) {length = l;}
void setWidth(double w) {width = w;}
void setHeight(double h) {height = h;}
double initialize(double x);
double calcVolume();
double calcArea();
void display();
/*Default Constructor */
Box()
{
setLength(1);
setWidth(1);
setHeight(1);
}
/*Another Constructor */
/*This will error out if there is no default constructor*/
Box(double x)
{ setLength(x);
setWidth(x);
setHeight(x);
}
/*Third Constructor with 3 parameters */
Box(double l,double w,double h)
{ setLength(l);
setWidth(w);
setHeight(h);
}
};
void Box::display()
{
cout << endl;
cout << "Length = " << getLength() << "\n";
cout << "Width = " << getWidth() << "\n";
cout << "Height = " << getHeight() << "\n";
cout << "Volume = " << calcVolume() << "\n";
cout << "Area = " << calcArea() << "\n";
cout << endl;
}
double Box::calcVolume()
{
return width * length * height;
}
double Box::calcArea()
{
return length * width;
}
double Box::initialize(double x)
{
width = x;
length = x;
height = x;
return 0;
}
void testBox01()
{
Box b1;
b1.display();
b1.setLength(13);
b1.setWidth(4);
b1.setHeight(9);
b1.display();
cout << "Length: " << b1.getLength() << "\n";
cout << "Width: " << b1.getWidth() << "\n";
cout << "Height: " << b1.getHeight() << "\n";
}
void testBox02()
{
Box b1;
b1.display();
b1.initialize(5);
b1.display();
}
int main() {
testBox01();
testBox02();
/*Code to test the overloaded constructors */
Box b1 ;
Box b2 (23.7);
Box b3 (11.1, 22.2, 33.3);
b1.display();
b2.display();
b3.display();
/*End of the code */
return 0;
}
output: (The output in green is the result of adding new code )
Length = 1
Width = 1
Height = 1
Volume = 1
Area = 1
Length = 13
Width = 4
Height = 9
Volume = 468
Area = 52
Length: 13
Width: 4
Height: 9
Length = 1
Width = 1
Height = 1
Volume = 1
Area = 1
Length = 5
Width = 5
Height = 5
Volume = 125
Area = 25
Length = 1
Width = 1
Height = 1
Volume = 1
Area = 1
Length = 23.7
Width = 23.7
Height = 23.7
Volume = 13312.1
Area = 561.69
Length =
11.1
Width = 22.2
Height = 33.3
Volume = 8205.79
Area = 246.42