| | |
Again please someone check my code
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 13
Reputation:
Solved Threads: 0
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:
<< moderator edit: added [code][/code] tags >>
code:
C++ Syntax (Toggle Plain Text)
//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
•
•
Join Date: May 2005
Posts: 13
Reputation:
Solved Threads: 0
<< 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
<< 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
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
C++ Syntax (Toggle Plain Text)
#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
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
C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- could you check my code ? (C)
- Code Check, Please (ASP)
- check this code pleeeeeeeeeease (C++)
- Check the code!! (C++)
- Would like for someone to check this to see if works and any help if it does not (Java)
- DrawHouse code help... (Java)
- DrawHouse Code Problem (Java)
Other Threads in the C++ Forum
- Previous Thread: Creating Amplitude in C++.net
- Next Thread: hexadecimal.cpp
Views: 2763 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





