C++ program to use Class Template

//class Box with l, b, h as general type data members
//and appropriate functions to calculate Volume of a box
//calculate volume of two boxes,
//one having dimensions in integer and other in double

#include <iostream>
using namespace std;
template <class V>
class Box
{
    private:
        V l,b,h;
    public:
        Box()
        {
            l=b=h=0;
        }
        Box(V x,V y, V z)
        {
            l=x;
            b=y;
            h=z;
        }
        V Volume()
        {
            return l*b*h;
        }
};
int main()
{
    Box <int> Bi(2,4,6);
    Box <double> Bd(2.6,4.7,1.1);
   
cout<<endl<<"Volume with integer dimension is: "<<Bi.Volume();
    cout<<endl<<"Volume with double dimension is: "<<Bd.Volume();
    return 0;
}

No comments:

Post a Comment