Advertisement
Guest User

Clown_pantaloons.cpp

a guest
Nov 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Cons
  6. {
  7.     public:
  8.     enum {INT, DOUBLE, CHAR, STRING} value_type;
  9.     union
  10.     {
  11.         int i;
  12.         double d;
  13.         char c;
  14.         std::string s;
  15.     };
  16.     Cons(char new_char)
  17.     {
  18.         this->my_type = CHAR;
  19.         this->c = new_char;
  20.     }
  21.    
  22.     Cons(double new_double)
  23.     {
  24.         this->my_type = DOUBLE;
  25.         this->d = new_double;
  26.     }
  27.    
  28.     Cons(int new_int)
  29.     {
  30.         this->my_type = INT;
  31.         this->i = new_int;
  32.     }
  33.    
  34.     //Cons(std::string new_string)
  35.     //{
  36.         //this->my_type = STRING;
  37.         //this->s = new_string;
  38.     //}
  39.    
  40.     int get_type()
  41.     {
  42.         return my_type;
  43.     }
  44.    
  45.     private:
  46.     int my_type = 0;
  47.    
  48. };
  49.  
  50. std::string cons_to_string(Cons& cons)
  51. {
  52.     switch(cons.get_type())
  53.     {
  54.     case Cons::INT: return std::to_string(cons.i);
  55.     case Cons::DOUBLE: return std::to_string(cons.d);
  56.     case Cons::CHAR: return std::string(1, cons.c);
  57.     //case Cons::STRING: return cons.s;
  58.     }
  59. }
  60.  
  61. int main()
  62. {
  63.    cout << "Hello World" << endl;
  64.    Cons my_array[] = {Cons(32), Cons('h'), Cons(45.5), Cons('H')};
  65.    for(Cons& c : my_array)
  66.    {
  67.        std::cout << cons_to_string(c) << std::endl;
  68.    }
  69.    std::cout << "Hello world!" << std::endl;
  70.    return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement