In: Computer Science
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;
}
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;
}