Question

In: Computer Science

answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...

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

  1. a constructor is a functiom
  2. the name of the constructor is the same name as the class
  3. the constructor does not return anything (not even void)
  4. the code for the constructor is run when the object is created

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

  1. declare and instantiate (create) three Box objects named b1, b2, and b3.

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

  1. run the display() method for each of the objects    

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

Solutions

Expert Solution

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


Related Solutions

This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
Consider that you want to rename all files of a given type (e.g. file extension) to...
Consider that you want to rename all files of a given type (e.g. file extension) to a different filetype. For example, you want to change the file extension of all txt files to text. Write a shell script rename.sh (3%) that: takes two options as arguments. The first argument is the filetype you want to change. The second argument is the filetype you want to change files into. check that the two arguments are actually given. If not, the script...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end of this question. This assignment will help you solve a problem using recursion Description A subsequence is when all the letters of a word appear in the relative order of another word. Pin is a subsequence of Programming ace is a subsequence of abcde bad is NOT a subsequence abcde as the letters are not in order This assignment you will create a Subsequence...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
If we rename y=x^2, f(x)=x^2, could we rename the inverse f^-1(x)? Explain your answer
If we rename y=x^2, f(x)=x^2, could we rename the inverse f^-1(x)? Explain your answer
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT