Question

In: Computer Science

C++ Programming Convert problems 6 in to template classes, of week 5 and week 4. Test...

C++ Programming

Convert problems 6 in to template classes, of week 5 and week 4.

Test each with Implicit int, float, double, long int.

Test each with explicit int, float, double, long int.

Problem 6:

//Box.h

#include<iostream>

using namespace std;

class Box

{

public:

   double Length;

   double Width;

   double Height;

public:

   //Default constructor

   Box();

   //Parameterized constructor

   Box(double ,double,double);

   //Setter methods

   void setWidth ( double n ) ;

   void setDepth ( double n ) ;

   void setHeight ( double n );

   //getter methods

   double getWidth () ;

   double getHeight ();

   double getDepth () ;

   //Methods to calculate area and volume of box

   double calcArea () ;

   double calcVolume () ;

};

//Box.cpp

#include <iostream>

#include "Box.h"

using namespace std;

//default constructor

Box::Box()

{

   Length = 0;

   Width = 0;

   Height = 0;

}

//parameterized constructor

Box::Box(double l,double w, double h)

{

   Length = l;

   Width = w;

   Height = h;

}

void Box::setWidth ( double n )

{

   Width = n;

}

void Box::setDepth ( double n )

{

   Length = n;

}

void Box::setHeight ( double n )

{

   Height = n;

}

double Box::getWidth ()

{

   return Width;

}

double Box::getHeight ()

{

   return Height;

}

double Box::getDepth ()

{

   return Length;

}

double Box::calcArea ()

{

   return 2*(Length*(Width+Height)+Width*Height);

}

double Box::calcVolume ()

{

   return Length*Width*Height;

}

------

//main.cpp

#include<iostream>

#include<string>

#include "Box.h"

using namespace std;

//start of main method

int main()

{

cout<<"Name";

   string myName= "yourname";

cout<<"Name: "<<myName<<endl;

   //default constructor

Box B1;

   cout<<"Testing default constructor"<<endl;

cout << "Height = " << B1.getHeight() << endl;

cout << "Area = " << B1.calcArea() << endl;

cout << "Volume = " << B1.calcVolume() << endl;

   //parmeter constructor

Box B2(1,2,3);

   cout<<"Testing parameterized constructor"<<endl;

cout << "Height = " << B2.getHeight() << endl;

cout << "Area = " << B2.calcArea() << endl;

cout << "Volume = " << B2.calcVolume() << endl;

Box B3;

B3.setWidth(3);

B3.setDepth(5);

B3.setHeight(10);

   cout<<"Testing functions "<<endl;

cout << "Height = " << B3.getHeight() << endl;

cout << "Area = " << B3. calcArea() << endl;

cout << "Volume = " << B3.calcVolume() << endl;

   system("pause");

   return 0;

}

Solutions

Expert Solution

If you have any doubts, please give me comment...

Box.h

#ifndef BOX_H

#define BOX_H

//Box.h

#include <iostream>

using namespace std;

template <class T>

class Box

{

public:

    T Length;

    T Width;

    T Height;

public:

    //Default constructor

    Box();

    //Parameterized constructor

    Box(T, T, T);

    //Setter methods

    void setWidth(T n);

    void setDepth(T n);

    void setHeight(T n);

    //getter methods

    T getWidth();

    T getHeight();

    T getDepth();

    //Methods to calculate area and volume of box

    double calcArea();

    double calcVolume();

};

#endif

Box.cpp

//Box.cpp

#include <iostream>

#include "Box.h"

using namespace std;

//default constructor

template<class T>

Box<T>::Box()

{

    Length = 0;

    Width = 0;

    Height = 0;

}

//parameterized constructor

template<class T>

Box<T>::Box(T l, T w, T h)

{

    Length = l;

    Width = w;

    Height = h;

}

template<class T>

void Box<T>::setWidth(T n)

{

    Width = n;

}

template<class T>

void Box<T>::setDepth(T n)

{

    Length = n;

}

template<class T>

void Box<T>::setHeight(T n)

{

    Height = n;

}

template<class T>

T Box<T>::getWidth()

{

    return Width;

}

template<class T>

T Box<T>::getHeight()

{

    return Height;

}

template<class T>

T Box<T>::getDepth()

{

    return Length;

}

template<class T>

double Box<T>::calcArea()

{

    return 2 * (Length * (Width + Height) + Width * Height);

}

template<class T>

double Box<T>::calcVolume()

{

    return Length * Width * Height;

}

main.cpp

#include <iostream>

#include <string>

#include "Box.h"

using namespace std;

//start of main method

int main()

{

    cout << "Name";

    string myName = "yourname";

    cout << "Name: " << myName << endl;

    //default constructor

    Box<int> B11;

    cout<<"\n\nDatatype: int"<<endl;

    cout << "Testing default constructor" << endl;

    cout << "Height = " << B11.getHeight() << endl;

    cout << "Area = " << B11.calcArea() << endl;

    cout << "Volume = " << B11.calcVolume() << endl;

    //parmeter constructor

    Box<int> B12(1, 2, 3);

    cout << "Testing parameterized constructor" << endl;

    cout << "Height = " << B12.getHeight() << endl;

    cout << "Area = " << B12.calcArea() << endl;

    cout << "Volume = " << B12.calcVolume() << endl;

    Box<int> B13;

    B13.setWidth(3);

    B13.setDepth(5);

    B13.setHeight(10);

    cout << "Testing functions " << endl;

    cout << "Height = " << B13.getHeight() << endl;

    cout << "Area = " << B13.calcArea() << endl;

    cout << "Volume = " << B13.calcVolume() << endl;

    //float

    cout<<"\n\nDatatype: float"<<endl;

    Box<float> B21;

    cout << "Testing default constructor" << endl;

    cout << "Height = " << B21.getHeight() << endl;

    cout << "Area = " << B21.calcArea() << endl;

    cout << "Volume = " << B21.calcVolume() << endl;

    //parmeter constructor

    Box<float> B22(1, 2, 3);

    cout << "Testing parameterized constructor" << endl;

    cout << "Height = " << B22.getHeight() << endl;

    cout << "Area = " << B22.calcArea() << endl;

    cout << "Volume = " << B22.calcVolume() << endl;

    Box<float> B23;

    B23.setWidth(3.0);

    B23.setDepth(5.0);

    B23.setHeight(10.0);

    cout << "Testing functions " << endl;

    cout << "Height = " << B23.getHeight() << endl;

    cout << "Area = " << B23.calcArea() << endl;

    cout << "Volume = " << B23.calcVolume() << endl;

    //double

    cout<<"\n\nDatatype: double"<<endl;

    Box<double> B31;

    cout << "Testing default constructor" << endl;

    cout << "Height = " << B31.getHeight() << endl;

    cout << "Area = " << B31.calcArea() << endl;

    cout << "Volume = " << B31.calcVolume() << endl;

    //parmeter constructor

    Box<double> B32(1.0, 2.0, 3.0);

    cout << "Testing parameterized constructor" << endl;

    cout << "Height = " << B32.getHeight() << endl;

    cout << "Area = " << B32.calcArea() << endl;

    cout << "Volume = " << B32.calcVolume() << endl;

    Box<double> B33;

    B33.setWidth(3.0);

    B33.setDepth(5.0);

    B33.setHeight(10.0);

    cout << "Testing functions " << endl;

    cout << "Height = " << B33.getHeight() << endl;

    cout << "Area = " << B33.calcArea() << endl;

    cout << "Volume = " << B33.calcVolume() << endl;

    //long int

    cout<<"\n\nDatatype: long int"<<endl;

    Box<long int> B41;

    cout << "Testing default constructor" << endl;

    cout << "Height = " << B41.getHeight() << endl;

    cout << "Area = " << B41.calcArea() << endl;

    cout << "Volume = " << B41.calcVolume() << endl;

    //parmeter constructor

    Box<long int> B42(1, 2, 3);

    cout << "Testing parameterized constructor" << endl;

    cout << "Height = " << B42.getHeight() << endl;

    cout << "Area = " << B42.calcArea() << endl;

    cout << "Volume = " << B42.calcVolume() << endl;

    Box<long int> B43;

    B43.setWidth(3);

    B43.setDepth(5);

    B43.setHeight(10);

    cout << "Testing functions " << endl;

    cout << "Height = " << B43.getHeight() << endl;

    cout << "Area = " << B43.calcArea() << endl;

    cout << "Volume = " << B43.calcVolume() << endl;

    // system("pause");

    return 0;

}


Related Solutions

C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
5. Eight students were given a placement test and after a week of classes were given...
5. Eight students were given a placement test and after a week of classes were given again a placement test at the same level. Here are their scores. Before 71 78 80 90 55 65 76 77 After 75 71 89 92 61 68 80 81 (a) Test whether the scores improved after 1 week by performing a student test. (b) Test whether the scores improved after 1 week by performing a sign test.
In C++, Convert the math expression (2+3)+4*(6-5) to post fix form and evaluate the post fix...
In C++, Convert the math expression (2+3)+4*(6-5) to post fix form and evaluate the post fix expression “3 2 * 3 – 2 /” to a value.
Convert 6∘ to radians, correct to 4 decimal places. 6∘= ____________ rad (4 dec. places). Convert...
Convert 6∘ to radians, correct to 4 decimal places. 6∘= ____________ rad (4 dec. places). Convert 4.75 rad to degrees, correct to 4 decimal places. 4.75 rad = ____________ degrees (4 dec. places).   Question b: (2 points) Determine the length of an arc of a circle with radius 6 metres that subtends a central angle of 300∘ to two decimal places. Arc length, s= ____________ m. c A circular wheel of radius 0.55 metres is spinning at a rate of...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are as follows: The base class isPerson. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The classshould also have a static data member called nextID which is used to assignanID numbertoeach object created(personID). All data members must be private. Create the following member functions: Accessor functions to allow access to first name and last name (updates and retrieval). These functions should...
In C programming. If a grammatically correct input is entered, the program should convert the two...
In C programming. If a grammatically correct input is entered, the program should convert the two strings to two integer arrays and perform the given operation on each array location. For all operations, consider each corresponding array position independently (e.g., there are no carries). The resulting array should then be converted back to a string, and finally printed. If the two input strings are not the same length, then each output character beyond the length of the shorter string should...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT