定义一个datatype类,能处理包含字符型,整形,浮点型3种类型的数据,给出其构造函数。
1 #include <iostream> 2 using namespace std; 3 4 class DataType{ 5 private: 6 char a; 7 int n; 8 float x; 9 enum { 10 character, 11 integer, 12 floating, 13 } vartype; 14 public: 15 DataType(char a) 16 { 17 vartype=character; 18 this->a=a; 19 } 20 DataType(int n) 21 { 22 vartype=integer; 23 this->n=n; 24 } 25 DataType(float x) 26 { 27 vartype=floating; 28 this->x=x; 29 } 30 void print() 31 { 32 switch(vartype){ 33 case character: 34 cout<<"zifu"<<a<<endl; 35 break; 36 case integer: 37 cout<<"zhengshu"<<n<<endl; 38 break; 39 case floating: 40 cout<<"fudiashu"<<x<<endl; 41 break; 42 } 43 } 44 }; 45 int main() 46 { 47 DataType a('c'),b(1),c(1.44F); 48 a.print(); 49 b.print(); 50 c.print(); 51 }
标签:12,DataType,character,vartype,课后,习题 From: https://www.cnblogs.com/Lyh3012648079/p/17350905.html