6 Replies - 502 Views - Last Post: 23 April 2013 - 03:10 PM Rate Topic: -----

#1 nephtyhephty  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 25-September 12

How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 02:21 PM

I want to make 1 object that can hold a char, and integers, and how can I do that since I am limited to making each object a specific type. For example: bstree<int> obj;

Is there a way to making it: bstree<int,char> obj; so it can hold either an int or a char?

main.cpp------------------------
#include <iostream>
#include <vector>
#include "bst.h"
#include "TNode.h"


using namespace std;
int main()
{
bstree<int> obj;

obj.insert(5);
    cin.get();
    return 0;
}



TNode.h----------------------------------------------------
#include <iostream>
#include <cstddef>
#include <vector>
#ifndef TNode_h
#define TNode_h
using namespace std;
template <class T>
class tnode{
public:
    tnode *left;
    tnode *right;
    T key;

    tnode(T &data)
    {
    key = data;
    left = NULL;
    right = NULL;
    }





};
#endif


bst.h-------------------------------------------------
#include <iostream>
#include <cstddef>
#include "TNode.h"

using namespace std;
template <class T>
class bstree
{

public:



    bstree()
    {
        root = NULL;
        parent = NULL;
    }

    void findmin()
    {

    }

    void findmax()
    {

    }
    void findkey(tnode<T>* root, T key)
    {

    }
    void insert(T k)
    {
        if (root == NULL)
        root = new tnode<T>(k);






    }











private:
        tnode<T> *root;
        tnode<T> *parent;



};



Is This A Good Question/Topic? 0
  • +

Replies To: How to make a Class object that uses templates to hold any Data type

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5303
  • View blog
  • Posts: 16,518
  • Joined: 25-December 09

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 02:34 PM

Quote

Is there a way to making it: bstree<int,char> obj; so it can hold either an int or a char?


Yes, you can have a class that can hold any one type, that is the purpose of templates.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 nephtyhephty  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 02:37 PM

View Postjimblumberg, on 23 April 2013 - 02:34 PM, said:

Quote

Is there a way to making it: bstree<int,char> obj; so it can hold either an int or a char?


Yes, you can have a class that can hold any one type, that is the purpose of templates.

Jim

There is no way to make it hold any data type?
so for example, lets say I make a class object, I can pass in an int, and then pass in a char. For example
bstree obj1;
obj1.insert(5);
obj1.insert(6);
obj1.insert('a');
obj1.insert('+');



Edit: I am trying to do this because I am making a tree where you take in Chars, and Integers, and you have to make the char's that include " + - * /" to be the parents. So that way you can easily make an infix equation.

This post has been edited by nephtyhephty: 23 April 2013 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5303
  • View blog
  • Posts: 16,518
  • Joined: 25-December 09

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 02:50 PM

Quote

so for example, lets say I make a class object, I can pass in an int, and then pass in a char. For example

Yes you can make a class that can hold the different type of variables. For example you could overload your insert() function to allow you to store different types of variables.

class Trial
{
   public:
      void insert(char value); // Inserts charValue, sets isInt to false.
      void insert(int value); // Inserts ti intValue, sets isInt to true.
   private;
      bool isInt;
      int intValue;
      char charValue;
};



Jim
Was This Post Helpful? 1
  • +
  • -

#5 nephtyhephty  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 03:03 PM

View Postjimblumberg, on 23 April 2013 - 02:50 PM, said:

Quote

so for example, lets say I make a class object, I can pass in an int, and then pass in a char. For example

Yes you can make a class that can hold the different type of variables. For example you could overload your insert() function to allow you to store different types of variables.

class Trial
{
   public:
      void insert(char value); // Inserts charValue, sets isInt to false.
      void insert(int value); // Inserts ti intValue, sets isInt to true.
   private;
      bool isInt;
      int intValue;
      char charValue;
};



Jim

Sorry for asking so many newbie question. But how would I make it that the obj that i created in the main can take more one data type.

Because currently I have my obj in main intialized to be an int. For ex: bstree<int> obj;
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1265
  • View blog
  • Posts: 4,979
  • Joined: 09-June 09

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 03:04 PM

Templates are not magic. When you instantiate a template class at compile time, that object is generated to work with the specified type. What I am essentially saying is that you cannot change the type of a template class during run time.

You have some options however.

1) If the integers are just '0'-'9', then why not just make it hold all chars. If the integers are greater or less than that range then this approach won't work
2) You can use a third party library called boost and use the boost::any or boost::variant types.
3) You can store only integers and then have a flag which will indicate that the data is type char
i.e.
   enum { TYPE_CHAR, TYPE_INT };
   struct Node {
      int data;
      int data_type;
   }
   

4) You can to some quick and dirty inheritance

i.e.
struct Node {
   Node *next;
   Node(Node *nxt): next(nxt) {}
};

struct IntNode: public Node {
   int data;
   IntNode(int i)
      :Node(NULL), data(i) {}
};

struct CharNode: public Node {
   char data;

   CharNode(char c)
      :Node(NULL), data(c) {}
};

class List {
   Node *head;
public
   List(): head(NULL) {}
   
   void add(Node *element) {
      element->next = head;
      head = element;
   }
};

int main() {
   List l;
   l.add(new CharNode('c'));
   l.add(new IntNode(999));
   
   return 0;
}



5) Or the simpliest, go back to the C days and use void pointers

struct Node {
   void *data;
   int bytes;
   Node *next;

   Node(void *data, int size, Node *nxt) 
      :data(data), bytes(size), next(nxt) {}
};

class list {
   Node *head;
public:
   List() : head(NULL) {}

   void add(void *data, int bytes) {
      Node *node = new Node(data, bytes, NULL);
      node->next = head;
      head = next;
   }
};
   

int main() {
   List l;
   l.add(new char('c'), sizeof(char));
   l.add(new int(9999), sizeof(int));
   
   return 0;
}



6) Or you can use Jim's way and create a variable for every type that might be stored in the container.

This post has been edited by jjl: 23 April 2013 - 03:06 PM

Was This Post Helpful? 2
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5303
  • View blog
  • Posts: 16,518
  • Joined: 25-December 09

Re: How to make a Class object that uses templates to hold any Data type

Posted 23 April 2013 - 03:10 PM

Quote

Because currently I have my obj in main intialized to be an int. For ex: bstree<int> obj;


In the sample I provided you would use bstree<Trial> obj;.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1