Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Cons
- {
- public:
- enum {INT, DOUBLE, CHAR, STRING} value_type;
- union
- {
- int i;
- double d;
- char c;
- std::string s;
- };
- Cons(char new_char)
- {
- this->my_type = CHAR;
- this->c = new_char;
- }
- Cons(double new_double)
- {
- this->my_type = DOUBLE;
- this->d = new_double;
- }
- Cons(int new_int)
- {
- this->my_type = INT;
- this->i = new_int;
- }
- //Cons(std::string new_string)
- //{
- //this->my_type = STRING;
- //this->s = new_string;
- //}
- int get_type()
- {
- return my_type;
- }
- private:
- int my_type = 0;
- };
- std::string cons_to_string(Cons& cons)
- {
- switch(cons.get_type())
- {
- case Cons::INT: return std::to_string(cons.i);
- case Cons::DOUBLE: return std::to_string(cons.d);
- case Cons::CHAR: return std::string(1, cons.c);
- //case Cons::STRING: return cons.s;
- }
- }
- int main()
- {
- cout << "Hello World" << endl;
- Cons my_array[] = {Cons(32), Cons('h'), Cons(45.5), Cons('H')};
- for(Cons& c : my_array)
- {
- std::cout << cons_to_string(c) << std::endl;
- }
- std::cout << "Hello world!" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement