473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to use TEMPLATE in C++

1
I am new in C++, and learning the langauge by my self only, while practicing i come across one question for which i have no idea, the question is to calculate area and perimeter of rectangle, the code i wrote was


#include <iostream>
using namespace std;
int main ()
{
// declaration of variables
float length; //length of a rectangle
float width; //width of a rectangle
float area; //area of a rectangle
float perim; //perimeter of a rectangle
// prompt for and read the input values
cout << "Enter the length of the rectangle" << endl;
cin >> length;
cout << "Enter the width of the rectangle" << endl;
cin >> width;
//Calculate the area and the perimeter
area = length*width;
perim = 2.0*length + 2.0*width;
//Display the result
cout << "The area is " << area << endl;
cout << "The perimeter is " << perim << endl;

return 0;

}

BUT I WHAT I NEED IS

The program will read the values of length and width from the user trough keyboard and passes these values to the function calculate(T x, T y). The function calculates the area and perimeter of the rectangle and displays the result on the screen. The function returns no value.

Can someone HELP?

thankx
Jul 26 '06 #1
2 3900
I am new in C++, and learning the langauge by my self only, while practicing i come across one question for which i have no idea, the question is to calculate area and perimeter of rectangle, the code i wrote was


#include <iostream>
using namespace std;
int main ()
{
// declaration of variables
float length; //length of a rectangle
float width; //width of a rectangle
float area; //area of a rectangle
float perim; //perimeter of a rectangle
// prompt for and read the input values
cout << "Enter the length of the rectangle" << endl;
cin >> length;
cout << "Enter the width of the rectangle" << endl;
cin >> width;
//Calculate the area and the perimeter
area = length*width;
perim = 2.0*length + 2.0*width;
//Display the result
cout << "The area is " << area << endl;
cout << "The perimeter is " << perim << endl;

return 0;

}

BUT I WHAT I NEED IS

The program will read the values of length and width from the user trough keyboard and passes these values to the function calculate(T x, T y). The function calculates the area and perimeter of the rectangle and displays the result on the screen. The function returns no value.

Can someone HELP?

thankx

Hi,
Your code is already doing the same what you need. The only thing is you want the calculation of perimeter to be done by another fuction called Calculate( ); so just cut copy paste the code inside the fuction Calculate( ); like

void Calculate(float Tx, float Ty)
{
float area;

area = (2*Tx) + (2*Ty);
"
"
"
}

But I dont think that u are asking this, if not then pls elobrate ur question.
Jul 26 '06 #2
Banfa
9,065 Expert Mod 8TB
Templates basically allow you to write functions or classes where the types of the variables are determined at compile time rather than when you write the source. You can define 2 things in a template, a type or a value.

For your case only a type is required

Expand|Select|Wrap|Line Numbers
  1. template<class CalcType> void Calculate(CalcType x, CalcType y)
  2. {
  3.     CalcType area;
  4.     CalcType perim;
  5.  
  6.     //Calculate the area and the perimeter
  7.     area = x*y;
  8.     perim = 2.0*x + 2.0*y;
  9.     //Display the result
  10.     cout << "The area is " << area << endl;
  11.     cout << "The perimeter is " << perim << endl;
  12. }
  13.  
This defines a function where the type used in the calculation is not set. The compiler will instanciate versions of the function for all the types that we need, so for the following code

Expand|Select|Wrap|Line Numbers
  1.     float fp_width = 5.3;
  2.     float fp_height = 7.1;
  3.  
  4.     int int_width = 3;
  5.     int int_height = 9;
  6.  
  7.     Calculate(fp_width, fp_height);
  8.  
  9.     Calculate(int_width, int_height);
  10.  
the compiler will instanciate the following 2 versions of calculate automatically

Expand|Select|Wrap|Line Numbers
  1. void Calculate(float x, float y)
  2. {
  3.     float area;
  4.     float perim;
  5.  
  6.     //Calculate the area and the perimeter
  7.     area = x*y;
  8.     perim = 2.0*x + 2.0*y;
  9.     //Display the result
  10.     cout << "The area is " << area << endl;
  11.     cout << "The perimeter is " << perim << endl;
  12. }
  13.  
  14. void Calculate(int x, int y)
  15. {
  16.     int area;
  17.     int perim;
  18.  
  19.     //Calculate the area and the perimeter
  20.     area = x*y;
  21.     perim = 2.0*x + 2.0*y;
  22.     //Display the result
  23.     cout << "The area is " << area << endl;
  24.     cout << "The perimeter is " << perim << endl;
  25. }
  26.  
note if I want to I can force the compiler to use a specific type when I call the function

[/code]
Calculate<long>(int_width, int_height);
[code]

in the above code although both the variables are of type int I have told the compiler to instanciate a function that uses the type long for it's parameters and variables. On a system where int is 2 bytes this clearly has the advantage of preventing maths overflow inside the Calculate function.


Although it is not clear fom this case sometimes it is necessary to manual instanciate a version of the function with specific TYPEs. This is possible, for instance

Expand|Select|Wrap|Line Numbers
  1. template<> void Calculate(char *x, char *y)
  2. {
  3.     cout << "It is not possible to calculate the area and perimiter of variables of type char * " << endl;
  4. }
  5.  
This link as a half decent tutorial

http://www.is.pku.edu.cn/~qzy/cpp/vc-stl/templates.htm
Jul 26 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.