Again please someone check my code

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 13
Reputation: shahid is an unknown quantity at this point 
Solved Threads: 0
shahid shahid is offline Offline
Newbie Poster

please someone check this code

 
0
  #1
Jul 13th, 2005
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:

  1. //header files
  2.  
  3. #include<iostream.h> // For I/O Operations
  4. #include<conio.c> // For Console Operations
  5. #include<math.h>
  6.  
  7. // Template Class Definition
  8.  
  9. // Template Class Name
  10.  
  11. template <class T>
  12. //Triangle class definition
  13. class Rectangle
  14. {
  15. //hidden part of the class
  16. private:
  17. T width();
  18.  
  19. T length;
  20.  
  21. //data member of template type named Perm for perimeter of the rectangle
  22. T perm;
  23.  
  24. //Interface of the class
  25. public:
  26.  
  27. //member function to find the area of Tirangle
  28. float area();
  29.  
  30. //member function to find the perimeter of Triangle
  31. T perimeter();
  32.  
  33.  
  34. //member function to display the values
  35. void display();
  36.  
  37. //member function to input the data values
  38. void input();
  39.  
  40. //member function to set width
  41. void width();
  42.  
  43. //member function to get width
  44. T getwidth();
  45.  
  46. //member function to set lenght
  47. void lenght(T);
  48.  
  49. //member function to get lenght
  50. T getlenght();
  51.  
  52. //constructor
  53. Rectangle();
  54.  
  55. //destructor
  56. ~Rectangle();
  57. };
  58.  
  59. //setter function of lenght
  60. template <class T>
  61.  
  62. void Rectangle<T>::setlenght(T l)
  63.  
  64. {
  65. //setting lenght
  66. lenght = l;
  67. }
  68.  
  69.  
  70. //setter function of width
  71. template <class T>
  72.  
  73. Rectangle<T>::setwidth(T w)
  74.  
  75. {
  76. //setting width
  77. width = w;
  78. }
  79.  
  80. //constructor definition to initiailize data members
  81. template <class T>
  82.  
  83. //constructors definition
  84. Rectangle<T>::Rectangle()
  85.  
  86. {
  87. //initializing lenght
  88. lenght = 0;
  89. //initializing widthp
  90. width = 0;
  91. //initializing Perm1
  92. perm = 0;
  93. }
  94.  
  95. //input function to take input
  96. template <class T>
  97.  
  98. //display message to take input
  99. cout<<"\t\tlenght of rectangle:\t\t";
  100. //taking input of lenght
  101. cin>>lenght;
  102.  
  103. //display message to take input
  104. cout<<"\n\t\twidth of rectangle:\t\t";
  105. //taking input of width
  106. cin>>width;
  107.  
  108. //calling display
  109. display();
  110.  
  111. }
  112.  
  113.  
  114. //display function to display input values
  115. template <class T>
  116.  
  117. //displaying values
  118. void Recatngle <T>::display()
  119.  
  120. {
  121. //displaying lenght of the Triangle entered by user
  122. cout<<"\nLenght of the Rectangle is:\t"<<getlenght();
  123.  
  124. //displaying base of the Triangle entered by user
  125. cout<<"\nWidth of the Rectangle is:\t\t"<<getwidth();
  126.  
  127.  
  128.  
  129. //calling and displaying area of the TriRectangle
  130. cout<<"\nArea of the Rectangle is:\t\t"<<area();
  131.  
  132. //calling perimeter and assigning to perm
  133. perm = perimeter();
  134.  
  135. //displaying perimeter of the Triangle
  136. cout<<"\nPerimeter of the Rectagle is:\t\t"<<perm;
  137.  
  138. }
  139.  
  140. //area function to calculate area
  141. template <class T>
  142.  
  143. //calculating area
  144.  
  145. float Rectagle <T>::area()
  146. {
  147.  
  148. //returning ares of the Rectangle
  149. return (length * width);
  150. }
  151.  
  152. //perimeter function to calculate perimeter
  153. template <class T>
  154.  
  155. //calculate perimeter
  156. T Rectangle <T>::perimeter()
  157. {
  158.  
  159. //returning perimeter of the Rectangle
  160. return (2 *(length + width);
  161. }
  162.  
  163. //main function
  164.  
  165. void main()
  166.  
  167. {
  168.  
  169. // creating the object of Rectangle with int as it's data type
  170. Rectangle<int> T;
  171.  
  172. // creating the object of Rectangle with float as it's data type
  173. Rectangle<float> T;
  174.  
  175. // creating the object of Rectangle with double as it's data type
  176. Rectangle<double> T;
  177.  
  178. cout<<"\nEnter the length of the rectangle as integer:\n\n";
  179. //calling input function of T1
  180. T. input();
  181. cout<<"\t__________________________________\n";
  182. cout<<"\nEnter the width of the rectangle as integer:\n\n";
  183. //calling input function
  184. T. input();
  185. cout<<"\nEnter the length of the rectangle as float:\n\n";
  186. //calling input function of T1
  187. T. input();
  188. cout<<"\t__________________________________\n";
  189. cout<<"\nEnter the width of the rectangle as float:\n\n";
  190. //calling input function
  191. T. input();
  192. cout<<"\nEnter the length of the rectangle as double:\n\n";
  193. //calling input function of T1
  194. T. input();
  195. cout<<"\t__________________________________\n";
  196. cout<<"\nEnter the width of the rectangle as double:\n\n";
  197. //calling input function
  198. T. input();
  199.  
  200. getch();
  201.  
  202. }//end main
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: please someone check this code

 
0
  #2
Jul 13th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: shahid is an unknown quantity at this point 
Solved Threads: 0
shahid shahid is offline Offline
Newbie Poster

Again please someone check my code

 
0
  #3
Jul 13th, 2005
<< 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

  1. #include<iostream.h> // For I/O Operations
  2. #include<conio.c> // For Console Operations
  3. #include<math.h>
  4.  
  5. // Template Class Definition
  6.  
  7. // Template Class Name
  8.  
  9. template <class T>
  10. //Triangle class definition
  11. class Rectangle
  12. {
  13. //hidden part of the class
  14. private:
  15. T width();
  16.  
  17. T length;
  18.  
  19. //data member of template type named Perm for perimeter of the rectangle
  20. T perm;
  21.  
  22. //Interface of the class
  23. public:
  24.  
  25. //member function to find the area of Tirangle
  26. float area();
  27.  
  28. //member function to find the perimeter of Triangle
  29. T perimeter();
  30.  
  31.  
  32. //member function to display the values
  33. void display();
  34.  
  35. //member function to input the data values
  36. void input();
  37.  
  38. //member function to set width
  39. void width();
  40.  
  41. //member function to get width
  42. T getwidth();
  43.  
  44. //member function to set lenght
  45. void lenght(T);
  46.  
  47. //member function to get lenght
  48. T getlenght();
  49.  
  50. //constructor
  51. Rectangle();
  52.  
  53. //destructor
  54. ~Rectangle();
  55. };
  56.  
  57. //setter function of lenght
  58. template <class T>
  59.  
  60. void Rectangle<T>::setlenght(T l)
  61.  
  62. {
  63. //setting lenght
  64. lenght = l;
  65. }
  66.  
  67.  
  68. //setter function of width
  69. template <class T>
  70.  
  71. void Rectangle<T>::setwidth(T w)
  72.  
  73. {
  74. //setting width
  75. width = w;
  76. }
  77.  
  78. //constructor definition to initiailize data members
  79. template <class T>
  80.  
  81. //constructors definition
  82. Rectangle<T>::Rectangle()
  83.  
  84. {
  85. //initializing lenght
  86. lenght = 0;
  87. //initializing widthp
  88. width = 0;
  89. //initializing Perm1
  90. perm = 0;
  91. }
  92.  
  93. //input function to take input
  94. template <class T>
  95. {
  96. //display message to take input
  97. cout<<"\t\tlenght of rectangle:\t\t";
  98. //taking input of lenght
  99. cin>>lenght;
  100.  
  101. //display message to take input
  102. cout<<"\n\t\twidth of rectangle:\t\t";
  103. //taking input of width
  104. cin>>width;
  105.  
  106. //calling display
  107. display();
  108.  
  109. }
  110.  
  111.  
  112. //display function to display input values
  113. template <class T>
  114.  
  115. //displaying values
  116. void Rectangle <T>::display()
  117.  
  118. {
  119. //displaying lenght of the Triangle entered by user
  120. cout<<"\nLenght of the Rectangle is:\t"<<getlenght();
  121.  
  122. //displaying base of the Triangle entered by user
  123. cout<<"\nWidth of the Rectangle is:\t\t"<<getwidth();
  124.  
  125.  
  126.  
  127. //calling and displaying area of the Rectangle
  128. cout<<"\nArea of the Rectangle is:\t\t"<<area();
  129.  
  130. //calling perimeter and assigning to perm
  131. perm = perimeter();
  132.  
  133. //displaying perimeter of the Rectangle
  134. cout<<"\nPerimeter of the Rectangle is:\t\t"<<perm;
  135.  
  136. }
  137.  
  138. //area function to calculate area
  139. template <class T>
  140.  
  141. //calculating area
  142.  
  143. float Rectangle <T>::area()
  144. {
  145.  
  146. //returning ares of the Rectangle
  147. return (length * width);
  148. }
  149.  
  150. //perimeter function to calculate perimeter
  151. template <class T>
  152.  
  153. //calculate perimeter
  154. T Rectangle <T>::perimeter()
  155. {
  156.  
  157. //returning perimeter of the Rectangle
  158. return (2 *(length + width);
  159. }
  160.  
  161. //main function
  162.  
  163. void main()
  164.  
  165. {
  166.  
  167. // creating the object of Rectangle with int as it's data type
  168. Rectangle<int> T;
  169.  
  170. // creating the object of Rectangle with float as it's data type
  171. Rectangle<float> T;
  172.  
  173. // creating the object of Rectangle with double as it's data type
  174. Rectangle<double> T;
  175.  
  176. cout<<"\nEnter the length of the rectangle as integer:\n\n";
  177. //calling input function of T
  178. T. input();
  179. cout<<"\t__________________________________\n";
  180. cout<<"\nEnter the width of the rectangle as integer:\n\n";
  181. //calling input function
  182. T. input();
  183. cout<<"\nEnter the length of the rectangle as float:\n\n";
  184. //calling input function of T
  185. T. input();
  186. cout<<"\t__________________________________\n";
  187. cout<<"\nEnter the width of the rectangle as float:\n\n";
  188. //calling input function
  189. T. input();
  190. cout<<"\nEnter the length of the rectangle as double:\n\n";
  191. //calling input function of T1
  192. T. input();
  193. cout<<"\t__________________________________\n";
  194. cout<<"\nEnter the width of the rectangle as double:\n\n";
  195. //calling input function
  196. T. input();
  197.  
  198. getch();
  199.  
  200. }//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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: Again please someone check my code

 
0
  #4
Jul 13th, 2005
  1.  
  2. #include<iostream> // For I/O Operations//<--Dieter : use iostream
  3. //conio.C ?????
  4. //math.h is not used in your code. Furthermore, the right name is <cmath>
  5. using namespace std;
  6.  
  7. // Template Class Definition
  8.  
  9. // Template Class Name
  10.  
  11. template <class T>
  12. //Triangle class definition
  13. class Rectangle
  14. {
  15. //hidden part of the class
  16. private:
  17. T width; //<--Dieter : brackets removed
  18.  
  19. T length;
  20.  
  21. T perm;//Dieter : the perm parameter is useless
  22.  
  23. //Interface of the class
  24. public:
  25.  
  26. //member function to find the area of Tirangle
  27. T area(); //<--Dieter: why use float when you write a template class ?
  28.  
  29. //member function to find the perimeter of Triangle
  30. T perimeter();
  31.  
  32.  
  33. //member function to display the values
  34. void display();
  35.  
  36. //member function to input the data values
  37. void input();
  38.  
  39. //member function to set width
  40. void setwidth(T);//Dieter : its better when you can input something
  41.  
  42. //member function to get width
  43. T getwidth();
  44.  
  45. //member function to set lenght
  46. void setlenght(T);
  47.  
  48. //member function to get lenght
  49. T getlenght();
  50.  
  51. //constructor
  52. Rectangle();
  53.  
  54. //destructor
  55. ~Rectangle();
  56.  
  57. };
  58.  
  59. //setter function of lenght
  60. template <class T>
  61. void Rectangle<T>::setlenght(T l)
  62. {
  63. //setting lenght
  64. length= l; //<--syntax error
  65. }
  66.  
  67.  
  68. //setter function of width
  69. template <class T>
  70.  
  71. void Rectangle<T>::setwidth(T w)
  72.  
  73. {
  74. //setting width
  75. width = w;
  76. }
  77.  
  78. //constructor definition to initiailize data members
  79. template <class T>
  80.  
  81. //constructors definition
  82. Rectangle<T>::Rectangle()
  83.  
  84. {
  85. //initializing lenght
  86. length= 0;
  87. //initializing widthp
  88. width = 0;
  89. //initializing Perm1
  90. perm = 0;
  91. }
  92.  
  93. //input function to take input
  94. template <class T>
  95. void Rectangle<T>::input() //<--Dieter : this line was missing
  96. {
  97. //display message to take input
  98. cout<<"\t\tlenght of rectangle:\t\t";
  99. //taking input of lenght
  100. cin>>length;//Dieter : syntax error
  101.  
  102. //display message to take input
  103. cout<<"\n\t\twidth of rectangle:\t\t";
  104. //taking input of width
  105. cin>>width;
  106.  
  107. //calling display
  108. display();
  109.  
  110. }
  111.  
  112.  
  113. //display function to display input values
  114. template <class T>
  115.  
  116. //displaying values
  117. void Rectangle <T>::display()
  118.  
  119. {
  120. //displaying lenght of the Triangle entered by user
  121. cout<<"\nLenght of the Rectangle is:\t"<<getlenght();
  122.  
  123. //displaying base of the Triangle entered by user
  124. cout<<"\nWidth of the Rectangle is:\t\t"<<getwidth();
  125.  
  126.  
  127.  
  128. //calling and displaying area of the Rectangle
  129. cout<<"\nArea of the Rectangle is:\t\t"<<area();
  130.  
  131. //calling perimeter and assigning to perm
  132. perm = perimeter();
  133.  
  134. //displaying perimeter of the Rectangle
  135. cout<<"\nPerimeter of the Rectangle is:\t\t"<<perm<<"\n";
  136.  
  137. }
  138.  
  139. //area function to calculate area
  140. template <class T>
  141.  
  142. //calculating area
  143.  
  144. T Rectangle <T>::area()
  145. {
  146.  
  147. //returning ares of the Rectangle
  148. return (length * width);
  149. }
  150.  
  151. //perimeter function to calculate perimeter
  152. template <class T>
  153.  
  154. //calculate perimeter
  155. T Rectangle <T>::perimeter() //<--Dieter : changed this line too.
  156. {
  157.  
  158. //returning perimeter of the Rectangle
  159. return (2 *(length + width)); //<--Dieter added closing bracket.
  160. }
  161.  
  162. //<--Dieter : your destructor was declared but never defined
  163. template <class T>
  164. Rectangle<T>::~Rectangle()
  165. {
  166. }
  167.  
  168. //<--Dieter : the getters getlenght and getwidth are not defined
  169. template <class T>
  170. T Rectangle<T>::getlenght()
  171. {
  172. return length;
  173. }
  174.  
  175. template <class T>
  176. T Rectangle<T>::getwidth()
  177. {
  178. return width;
  179. }
  180.  
  181.  
  182. //main function
  183.  
  184. int main() //<--Dieter :main must return an int
  185.  
  186. {
  187.  
  188. // creating the object of Rectangle with int as it's data type
  189. Rectangle<int> Ti; //Dieter your 3 objects CANT HAVE THE SAME NAME !!!
  190.  
  191. // creating the object of Rectangle with float as it's data type
  192. Rectangle<float> Tf;
  193.  
  194. // creating the object of Rectangle with double as it's data type
  195. Rectangle<double> Td;
  196.  
  197. //<--Dieter : see modifications below
  198.  
  199. cout<<"\nEnter the length of the rectangle as integer:\n\n";
  200. //calling input function of T
  201. Ti. input();
  202. cout<<"\t__________________________________\n";
  203. cout<<"\nEnter the length of the rectangle as float:\n\n";
  204. //calling input function of T
  205. Tf. input();
  206. cout<<"\t__________________________________\n";
  207. cout<<"\nEnter the length of the rectangle as double:\n\n";
  208. //calling input function of T1
  209. Td. input();
  210.  
  211. cout<<"Press [enter]."<<endl;
  212. getch();
  213.  
  214. }//end main
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2763 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC