i write c++ program using template class that program take lenght and width of rectangle as integer, float and double and display the area and perimeter of rectangle. but my program does not run. please some one check this mention mistake or write this in a better way
code:
//header files
#include<iostream.h> // For I/O Operations
#include<conio.c> // For Console Operations
#include<math.h>
// Template Class Definition
// Template Class Name
template <class T>
//Triangle class definition
class Rectangle
{
//hidden part of the class
private:
T width();
T length;
//data member of template type named Perm for perimeter of the rectangle
T perm;
//Interface of the class
public:
//member function to find the area of Tirangle
float area();
//member function to find the perimeter of Triangle
T perimeter();
//member function to display the values
void display();
//member function to input the data values
void input();
//member function to set width
void width();
//member function to get width
T getwidth();
//member function to set lenght
void lenght(T);
//member function to get lenght
T getlenght();
//constructor
Rectangle();
//destructor
~Rectangle();
};
//setter function of lenght
template <class T>
void Rectangle<T>::setlenght(T l)
{
//setting lenght
lenght = l;
}
//setter function of width
template <class T>
Rectangle<T>::setwidth(T w)
{
//setting width
width = w;
}
//constructor definition to initiailize data members
template <class T>
//constructors definition
Rectangle<T>::Rectangle()
{
//initializing lenght
lenght = 0;
//initializing widthp
width = 0;
//initializing Perm1
perm = 0;
}
//input function to take input
template <class T>
//display message to take input
cout<<"\t\tlenght of rectangle:\t\t";
//taking input of lenght
cin>>lenght;
//display message to take input
cout<<"\n\t\twidth of rectangle:\t\t";
//taking input of width
cin>>width;
//calling display
display();
}
//display function to display input values
template <class T>
//displaying values
void Recatngle <T>::display()
{
//displaying lenght of the Triangle entered by user
cout<<"\nLenght of the Rectangle is:\t"<<getlenght();
//displaying base of the Triangle entered by user
cout<<"\nWidth of the Rectangle is:\t\t"<<getwidth();
//calling and displaying area of the TriRectangle
cout<<"\nArea of the Rectangle is:\t\t"<<area();
//calling perimeter and assigning to perm
perm = perimeter();
//displaying perimeter of the Triangle
cout<<"\nPerimeter of the Rectagle is:\t\t"<<perm;
}
//area function to calculate area
template <class T>
//calculating area
float Rectagle <T>::area()
{
//returning ares of the Rectangle
return (length * width);
}
//perimeter function to calculate perimeter
template <class T>
//calculate perimeter
T Rectangle <T>::perimeter()
{
//returning perimeter of the Rectangle
return (2 *(length + width);
}
//main function
void main()
{
// creating the object of Rectangle with int as it's data type
Rectangle<int> T;
// creating the object of Rectangle with float as it's data type
Rectangle<float> T;
// creating the object of Rectangle with double as it's data type
Rectangle<double> T;
cout<<"\nEnter the length of the rectangle as integer:\n\n";
//calling input function of T1
T. input();
cout<<"\t__________________________________\n";
cout<<"\nEnter the width of the rectangle as integer:\n\n";
//calling input function
T. input();
cout<<"\nEnter the length of the rectangle as float:\n\n";
//calling input function of T1
T. input();
cout<<"\t__________________________________\n";
cout<<"\nEnter the width of the rectangle as float:\n\n";
//calling input function
T. input();
cout<<"\nEnter the length of the rectangle as double:\n\n";
//calling input function of T1
T. input();
cout<<"\t__________________________________\n";
cout<<"\nEnter the width of the rectangle as double:\n\n";
//calling input function
T. input();
getch();
}//end main
<< moderator edit: added [code][/code] tags >>