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

Recommended Answers

All 3 Replies

There is many problems with your code : it is full of syntax errors, and doesn't even compile.

Please clean it up and search a little what's wrong by yourself before posting it.

<< moderator edit: merged threads >>

I try towrite a c++ program using template class. program take input leght and width of a rectangle as integer, double and float from user and given output area and perimeter of rectangle. but my program given some error. please someone check this code and correct this or write this in a better way. thanks
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>

void 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 Rectangle <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 Rectangle
    cout<<"\nArea of the Rectangle is:\t\t"<<area();
    
    //calling perimeter and assigning to perm
    perm = perimeter();
    
    //displaying perimeter of the Rectangle
        cout<<"\nPerimeter of the Rectangle is:\t\t"<<perm;
    
}
    
//area function to calculate area     
template <class T>

//calculating area

float Rectangle <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 T
    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 T
    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 >>

i required this type ofoutput
Enter the length of the rectangle as integer
10
Enter the width of the rectangle as integer
20
Area of rectangle is : 200
Perimeter of the rectangle is : 60

Enter the length of the rectangle as float
10.2
Enter the width of the rectangle as float
20.3
Area of rectangle is : 207.06
Perimeter of the rectangle is : 61

Enter the length of the rectangle as double
12.33
Enter the width of the rectangle as double
25.36
Area of rectangle is : 312.689
Perimeter of the rectangle is : 75.38

#include<iostream> // For I/O Operations//<--Dieter : use iostream
//conio.C ?????
//math.h is not used in your code. Furthermore, the right name is <cmath>
using namespace std;

// Template Class Definition

// Template Class Name

template <class T>
//Triangle class definition
class Rectangle
{
//hidden part of the class
private:
T width; //<--Dieter : brackets removed

T length;

T perm;//Dieter : the perm parameter is useless

//Interface of the class
public:

//member function to find the area of Tirangle
T area(); //<--Dieter: why use float when you write a template class ?

//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 setwidth(T);//Dieter : its better when you can input something

//member function to get width
T getwidth();

//member function to set lenght
void setlenght(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
length= l; //<--syntax error
}


//setter function of width
template <class T>

void 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
length= 0;
//initializing widthp
width = 0;
//initializing Perm1
perm = 0;
}

//input function to take input
template <class T>
void Rectangle<T>::input() //<--Dieter : this line was missing
{
//display message to take input
cout<<"\t\tlenght of rectangle:\t\t";
//taking input of lenght
cin>>length;//Dieter : syntax error

//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 Rectangle <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 Rectangle
cout<<"\nArea of the Rectangle is:\t\t"<<area();

//calling perimeter and assigning to perm
perm = perimeter();

//displaying perimeter of the Rectangle
cout<<"\nPerimeter of the Rectangle is:\t\t"<<perm<<"\n";

}

//area function to calculate area
template <class T>

//calculating area

T Rectangle <T>::area()
{

//returning ares of the Rectangle
return (length * width);
}

//perimeter function to calculate perimeter
template <class T>

//calculate perimeter
T Rectangle <T>::perimeter() //<--Dieter : changed this line too.
{

//returning perimeter of the Rectangle
return (2 *(length + width)); //<--Dieter added closing bracket.
}

//<--Dieter : your destructor was declared but never defined
template <class T>
Rectangle<T>::~Rectangle()
{
}

//<--Dieter : the getters getlenght and getwidth are not defined
template <class T>
T Rectangle<T>::getlenght()
{
	return length;
}

template <class T>
T Rectangle<T>::getwidth()
{
	return width;
}


//main function

int main() //<--Dieter :main must return an int

{

// creating the object of Rectangle with int as it's data type
Rectangle<int> Ti;  //Dieter your 3 objects CANT HAVE THE SAME NAME !!!

// creating the object of Rectangle with float as it's data type
Rectangle<float> Tf;

// creating the object of Rectangle with double as it's data type
Rectangle<double> Td;

//<--Dieter : see modifications below

cout<<"\nEnter the length of the rectangle as integer:\n\n";
//calling input function of T
Ti. input();
cout<<"\t__________________________________\n";
cout<<"\nEnter the length of the rectangle as float:\n\n";
//calling input function of T
Tf. input();
cout<<"\t__________________________________\n";
cout<<"\nEnter the length of the rectangle as double:\n\n";
//calling input function of T1
Td. input();

cout<<"Press [enter]."<<endl;
getch();

}//end main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.