Class, enum and #ifdef

Aug 4, 2009 at 4:15pm
Exercise 8 from Thinking in C++ Vol 1 reads (Chapter 7):
Modify SuperVar so that there are #ifdefs around all the vartype code as described in the section on enum. Make vartype a regular and public enumeration (with no instance) and modify print( ) so that it requires a vartype argument to tell it what to do.

Below I include the original code, followed by the program amended by myself. I cannot workout why, in the amended program, only

A.print('d');

seems to be executed properly. It appears that
B.print(12322);
C.print(1.44F);
are not processed by SuperVar::print

Your comments will be gratefully received.




//: C07:SuperVar.cpp
// A super-variable
#include <iostream>
using namespace std;

class SuperVar {
enum {
character,
integer,
floating_point
} vartype; // Define one
union { // Anonymous union
char c;
int i;
float f;
};
public:
SuperVar(char ch);
SuperVar(int ii);
SuperVar(float ff);
void print();
};

SuperVar::SuperVar(char ch) {
vartype = character;
c = ch;
}

SuperVar::SuperVar(int ii) {
vartype = integer;
i = ii;
}

SuperVar::SuperVar(float ff) {
vartype = floating_point;
f = ff;
}

void SuperVar::print() {
switch (vartype) {
case character:
cout << "character: " << c << endl;
break;
case integer:
cout << "integer: " << i << endl;
break;
case floating_point:
cout << "float: " << f << endl;
break;
}
}

int main() {
SuperVar A('c'), B(12), C(1.44F);
A.print();
B.print();
C.print();
} ///:~


//////////////////////////////////////////////////////////////////

My amended code







//: C07:SuperVar.cpp
// A super-variable
#include <iostream>
#include <conio>
#include <assert.h>

using namespace std;

class SuperVar {
enum VarType {
character,
integer,
floating_point
}vartype ;
#ifdef CHECK_VARTYPE
#define CHECK_VARTYPE
VarType checkvartype; //Define one
#endif
union { // Anonymous union
char c;
int i;
float f;
};
public:
SuperVar(char ch);
SuperVar(int ii);
SuperVar(float ff);
void print(VarType vartype);
};

SuperVar::SuperVar(char ch) {
#ifdef CHECK_VARTYPE
checkvartype = character;
#endif
c = ch;
}

SuperVar::SuperVar(int ii) {
#ifdef CHECK_VARTYPE
checkvartype = integer;
#endif
i = ii;
}

SuperVar::SuperVar(float ff) {
#ifdef CHECK_VARTYPE
checkvartype = floating_point;
#endif
f = ff;
}

void SuperVar::print(enum VarType) {
#ifdef CHECK_VARTYPE
assert(checkvartype == vartype); //Define one
#endif
switch (vartype) {
case character:
cout << "character: " << c << endl;
getch();
break;
case integer:
cout << "integer: " << i << endl;
getch();
break;
case floating_point:
cout << "float: " << f << endl;
getch();
break;
}
}

int main() {
SuperVar A('d'), B(12322), C(1.44F);
A.print('d');
B.print(12322);
C.print(1.44F);
} ///:~
Aug 4, 2009 at 5:00pm
The SuperVar::print function takes an enum as the arg. It doesn't take float, int, char, or any other type. You are calling print with the wrong kind of values. Honestly, I am surprised that your amended program compiles. I didn't think that an enum could be implicitly constructed like that during a function call.
Aug 5, 2009 at 6:45pm
Many thanks for the response. I have run various amended versions of the original program in order to attempt to complete the exercises in the book. The results obtaining are the same as first suggested. Am I correct in concluding that the question cannot be done?

The process has been useful because, hopefully, I understand better how enum and union work. However, I have two remaining questions:
1 . Enum VarType and union do not appear to be linked by a command. Do they always go together? In the original code it is evident that the information they provide is complementary. The examples I found in the Internet are not explicit.
2 . What is the effect of making one public and the other private?
Topic archived. No new replies allowed.