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

Can i have a single template object based on the type given by user?

Like the functionality i want to achieve is as under: but it will not work. How can i achieve the same functionality?
Expand|Select|Wrap|Line Numbers
  1.  
  2. # include <iostream.h>
  3. # include <conio.h>
  4.  
  5. template <class T>
  6. class Test
  7. {
  8.     T a;
  9.     public:
  10.         void set()
  11.         {
  12.             cin>>a;
  13.         }
  14.         void display()
  15.         {
  16.             cout<<"A is"<<a<<endl;
  17.         }
  18. };
  19.  
  20. int main()
  21. {
  22.     int choice;
  23.     cin>>choice;
  24.     if(choice == 1)
  25.     {
  26.         Test<int> obj;
  27.     }
  28.     else if(choice ==2)
  29.     {
  30.         Test<char> obj;
  31.  
  32.     }
  33.     else
  34.     {
  35.         Test<float> obj;
  36.  
  37.     }
  38.     obj.set();
  39.     obj.display();
  40.     return 0;
  41. }
  42.  
Feb 23 '11 #1
1 1189
hype261
207 100+
I haven't tested this out, but I believe you can solve this problem using an interface class.

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. class Interface
  4. {
  5. public:
  6. virtual void set() = 0;
  7. virtual void display() = 0;
  8. };
  9.  
  10. template<typename T>
  11. class Test : public Interface
  12. {
  13. public:
  14. virtual void set()
  15. {
  16. cin >> a;
  17. }
  18.  
  19. virtual void display()
  20. {
  21. cout << a;
  22. }
  23. private:
  24. T a;
  25. };
  26.  
  27. int main()
  28. {
  29. Interface * obj;
  30.  
  31. int choice;
  32. cin >> choice;
  33.  
  34. if(choice == 1) 
  35.     { 
  36.         obj = new Test<int>(); 
  37.     } 
  38.     else if(choice ==2) 
  39.     { 
  40.         obj = new Test<char>(); 
  41.  
  42.     } 
  43.     else 
  44.     { 
  45.         obj = new Test<float>; 
  46.  
  47.     } 
  48.     obj.set(); 
  49.     obj.display(); 
  50.     delete obj;
  51.     return 0;
  52. }
  53.  
Feb 23 '11 #2

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

Similar topics

2
by: meng | last post by:
is it possible to use a single connection object shared by several tasks where each task is handled by a thread? these tasks call stored procedures that return record sets, no editing, update or...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
1
by: Harold | last post by:
Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something Here are 2 methods. One is the DataGrid's ItemDataBound event to catch...
7
by: Daniel Walzenbach | last post by:
Hello, I want to create a Word XML file based on the input users make in a VB.NET application. I imagine creating a template in Word and saving it as a XML file. I then want to fill the...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
1
by: Friends | last post by:
Hi I need to set security for row level but not based on Database user's login. It should be based on the user table login. For the particular user I need to allow only the particular records to...
1
by: ChasW | last post by:
Im looking for some technical clarification regarding exactly what the differences, if any, are between the two pieces of code are, aside from the obvious syntactical differences. int x = 5; ...
2
gchq
by: gchq | last post by:
This is the first time I've attempted to bring data into a DataTable from XML - that part (now) works OK, but it's blowing out at the select (line 23 below) statement with "Unable to cast object of...
2
by: Hvid Hat | last post by:
Hi I've been messing around with adding attributes to certain nodes. I've looked at FAQ at http://www.dpawson.co.uk/ without finding what I'm looking for. Is it possible to have a template...
1
by: markla | last post by:
Hi, I have an Entity data model built in Entity Framework, which sources data primarily from an MS SQL 2008 database, and sources some static (data dictionary) values from code-based objects. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.