Welcome Guest ( Log In | Register)



 
Closed TopicStart new topic
> Templates In C++
jibnet
post Jun 5 2006, 10:03 AM
Post #1


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 4-June 06
Member No.: 24,718



QUOTE
What's better than having several classes that do the same thing to different datatypes? One class that lets you choose which datatype it acts on.

Templates are a way of making your classes more abstract by letting you define the behavior of the class without actually knowing what datatype will be handled by the operations of the class. In essence, this is what is known as generic programming; this term is a useful way to think about templates because it helps remind the programmer that a templated class does not depend on the datatype (or types) it deals with. To a large degree, a templated class is more focused on the algorithmic thought rather than the specific nuances of a single datatype. Templates can be used in conjunction with abstract datatypes in order to allow them to handle any type of data. For example, you could make a templated stack class that can handle a stack of any datatype, rather than having to create a stack class for every different datatype for which you want the stack to function. The ability to have a single class that can handle several different datatypes means the code is easier to maintain, and it makes classes more reusable.

The basic syntax for declaring a templated class is as follows:

template <class a_type> class a_class {...};
The keyword 'class' above simply means that the identifier a_type will stand for a datatype. NB: a_type is not a keyword; it is an identifier that during the execution of the program will represent a single datatype. For example, you could, when defining variables in the class, use the following line:

a_type a_var;
and when the programmer defines which datatype 'a_type' is to be when the program instantiates a particular instance of a_class, a_var will be of that type.

When defining a function as a member of a templated class, it is necessary to define it as a templated function:

template<class a_type> void a_class<a_type>::a_function(){...}

When declaring an instance of a templated class, the syntax is as follows:

a_class<int> an_example_class;

An instantiated object of a templated class is called a specialization; the term specialization is useful to remember because it reminds us that the original class is a generic class, whereas a specific instantiation of a class is specialized for a single datatype (although it is possible to template multiple types).

Usually when writing code it is easiest to precede from concrete to abstract; therefore, it is easier to write a class for a specific datatype and then proceed to a templated - generic - class. For that brevity is the soul of wit, this example will be brief and therefore of little practical application.

We will define the first class to act only on integers.
CODE

class calc
{
  public:
    int multiply(int x, int y);
    int add(int x, int y);
};
int calc::multiply(int x, int y)
{
  return x*y;
}
int calc::add(int x, int y)
{
  return x+y;
}

We now have a perfectly harmless little class that functions perfectly well for integers; but what if we decided we wanted a generic class that would work equally well for floating point numbers? We would use a template.
CODE

template <class A_Type> class calc
{
  public:
    A_Type multiply(A_Type x, A_Type y);
    A_Type add(A_Type x, A_Type y);
};
template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y)
{
  return x*y;
}
template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y)
{
  return x+y;
}

To understand the templated class, just think about replacing the identifier A_Type everywhere it appears, except as part of the template or class definition, with the keyword int. It would be the same as the above class; now when you instantiate an
object of class calc you can choose which datatype the class will handle.

calc <double> a_calc_class;
Templates are handy for making your programs more generic and allowing your code to be reused later.


Notice from serverph:
COPIED without proper QUOTES as reported by Avalon.
Sourced from: http://www.cprogramming.com/tutorial/templates.html
Warning served. Quote tags added.
Go to the top of the page
 
+Quote Post
Avalon
post Jun 5 2006, 11:49 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 630
Joined: 12-August 05
From: Melbourne, Australia
Member No.: 10,624



IPB Image

This post has been edited by Avalon: Jun 5 2006, 01:16 PM
Go to the top of the page
 
+Quote Post
Marekwronczyn
post Jun 5 2006, 12:04 PM
Post #3


Newbie
*

Group: Members
Posts: 7
Joined: 5-June 06
Member No.: 24,748



I did not know, that templates are for " + + " C
Can anybody praise behind assistance program written these templates?
I apologize for errors
Go to the top of the page
 
+Quote Post
jibnet
post Jun 5 2006, 12:45 PM
Post #4


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 4-June 06
Member No.: 24,718



I believe its not spamming.. I saw a lots of post here where users wanted to learn C programming thats why i published this information. And anyways how can u call my information SPAM. there isnt a single place where i have written that i am the author for information. Its just sharing of information.
Go to the top of the page
 
+Quote Post
serverph
post Jun 5 2006, 05:06 PM
Post #5


Ancient Enigma
Group Icon

Group: [MODERATOR]
Posts: 1,796
Joined: 11-July 04
From: under the stars
Member No.: 76



CLOSED.
Go to the top of the page
 
+Quote Post

Closed TopicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Excellent Free Website Templates(25)
  2. My First Website(3)
  3. Open Source Web Design(52)
  4. Php Scripts And Website Templates(7)
  5. Templates(10)
  6. A Couple Of My Templates.(1)
  7. Review My Templates(18)
  8. Free Website Building Graphics.(21)
  9. Free Professional Website Templates(12)
  10. Html/css Website Hosting Templates Request - You Will Be Paid Off(6)
  11. Mambo Templates(9)
  12. Confirmation Code For Registration Users(3)
  13. Blogger Templates(9)
  14. Free Web Site Templates(0)
  15. Please Someone Help With Css Templates(4)
  1. Four Free Templates(15)
  2. Free Templates For Your Site(3)
  3. The Best And High Quality Templates(1)
  4. Free Website Templates(22)
  5. Php Templates(8)
  6. Joomla Templates(2)
  7. Free Clan Templates(6)
  8. Good And Professional Web Templates Should Be Free(13)
  9. Kind Of Templates(1)
  10. Free Clan Templates And A Cms(3)
  11. 2 Templates(8)
  12. Best Free Html/flash Templates(13)
  13. Writing On Templates(7)
  14. 750 Best Free Templates Of The Internet!(12)
  15. Phpbb 2 Free Themes/templates(5)


 



- Lo-Fi Version Time is now: 16th October 2008 - 02:30 AM