求一个程序.........满足修改的要求
修改SuperVar以便所有vartype代码前有#ifdef,让vartype成为一个常规的public枚举类型(没有实例),修改print(),使它要求vartype参数能告诉它做什么.#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();
}///:~