473,385 Members | 1,838 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,385 software developers and data experts.

template class

Hi... pls help to guide me how to do the below question. I have done the coding but my program is doesn't work.

QUESTION:

Given the template Pair<T1, T2>, discuss how you can represent
the following information. Try to declare a corresponding Pair object, and show the
C++ code to store and retrieve the information.
a. Name and age of a person. E.g. ( “Uncle Soo”, 21 ) , ( “Yoda” , 812 ) ,
( “Harry Potter” , 18 )
b. Length and resistance of a wire. E.g. ( 15cm, 0.15ohm ) , ( 100cm, 2.15ohm)
c. Position and color of a pixel (a pixel is a “dot” on your computer screen).
Position is represented as integer X- and Y- coordinate, with the top left corner
as the origin. E.g. ( 3, 12, Green) , (302, 516, Red) etc. You can assume there
are only three colors: Red, Green and Blue.
There are quite a number of alternatives for (c) other than using Pair<T1, T2>.
Suggests a few other solutions and compares them with using the Pair template class.

PROGRAM:

#include <iostream>
using namespace std;

template <typename T1, typename T2>
class pair
{
private:
T1 _first;
T2 _second;

public:
pair(T1 a, T2 b) : _first(a), _second(b) {}

T1 getFirst() const {return first;}
T2 getSecond() const {return second;}
};

template <typename T1, typename T2>
class pair(T1 a, T2 b)
: _first(a), _second(b)
{}

template <typename T1, typename T2>
T1 getFirst() const
{return _first;}

template <typename T1, typename T2>
T2 getSecond() const
{return _second;}

int main()
{
pair<char*, int*> example("Harry Potter", 18);
}
Aug 30 '08 #1
2 1403
Airslash
221 100+
What your teacher is trying to learn here , is the basic use of template classes.

Template classes provide a common way of defining functions and methods in handeling the data that is represented/stored inside the class.
But instead of writing a class for every possible combination of types, a template is used that allows you to declare it only once, and then change the types to what you need at any given time.

So in your example you can use the code of the template class and create the examples listed in the question.

It's a bit hard for me to explain this over the Internet, as i normally explain stuff like this face to face with demonstrations, but I suggest you google a bit into C++ templates to get the general info regarding them, and i'm sure your question will becomes more clearer then
Aug 30 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
You have various syntax errors in this code. This is the corrected code with comments. It compiles and links but it's up to you to see if it works.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. //weaknessforcats: There is a pair class in std. The compiler sees your pair class as a redeclaration and gives an error. 
  3. //using namespace std;
  4.  
  5.  
  6. template <typename T1, typename T2>
  7. class pair 
  8. {
  9. private:
  10. T1 _first;
  11. T2 _second;
  12.  
  13. public:
  14. pair(T1 a, T2 b) : _first(a), _second(b) {}
  15.  
  16. T1 getFirst() const {return first;}
  17. T2 getSecond() const {return second;}
  18. };
  19. //
  20. //weaknessforcats:this constructor is already in the pair class.
  21. //template <typename T1, typename T2>
  22. //class pair(T1 a, T2 b) 
  23. //: _first(a), _second(b)
  24. //{}
  25. //weaknessforcats:this method is already in the pair class.
  26.  
  27. //template <typename T1, typename T2>
  28. //T1 getFirst() const
  29. //{return _first;}
  30. //weaknessforcats:this method is already in the pair class.
  31. //template <typename T1, typename T2>
  32. //T2 getSecond() const
  33. //{return _second;}
  34.  
  35. int main()
  36. {
  37. pair<char*, int> example("Harry Potter", 18);
  38.  
  39. //weaknessforcats: 18 is not a pointer to an int. See above.
  40. //pair<char*, int*> example("Harry Potter", 18);
  41.  
Aug 30 '08 #3

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

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
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...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
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) ;
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.