練習しゅうよう:タイプ変換たいぷへんかん
- #include <iostream>
- #include <string.h>
- //#include "../header/class.hpp"
- using namespace std;
- class Test {
- public:
- //
- Test(int i) {
- this->i = this->INT;
- this->type = 1;
- }
-
- //
- Test(char s[10]) {
- strcpy(this->s, s);
- this->type = this->STRING;
- }
-
- void show() {
- if (this->type == this->INT) {
- cout << i << endl;
- } else {
- cout << s << endl;
- }
- }
- private:
- int i;
- char s[10];
- int type;
- enum {INT = 1, STRING};
- };
-
- int main() {
- //
- Test t1 = 1;
- //
- Test t2 = "string";
-
- t1.show();
- t2.show();
- return 0;
- }
出力:1 string