//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
#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
#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
| DaniWeb Message | |
| Cancel Changes | |