「C++」クラスの継承のサンプル

書式
class 子クラス: public 親クラス
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <string>
using namespace std;
// 親クラス
class Vtest {
public:
string strA = "study skill";
void testA() {
cout << "6677\n" ;
}
};
// 子クラス
class Child: public Vtest {
public:
string strB = "in arkgame";
};
int main() {
Child cft;
cft.testA();
cout << cft.strA + " " + cft.strB;
return 0;
}
#include <iostream> #include <string> using namespace std; // 親クラス class Vtest { public: string strA = "study skill"; void testA() { cout << "6677\n" ; } }; // 子クラス class Child: public Vtest { public: string strB = "in arkgame"; }; int main() { Child cft; cft.testA(); cout << cft.strA + " " + cft.strB; return 0; }
#include <iostream>
#include <string>
using namespace std;

// 親クラス
class Vtest {
  public: 
    string strA = "study skill";
    void testA() {
      cout << "6677\n" ;
    }
};

// 子クラス
class Child: public Vtest {
  public: 
    string strB = "in arkgame";
};

int main() {
  Child cft;
  cft.testA();
  cout << cft.strA + " " + cft.strB;
  return 0;
}

実行結果
6677
study skill in arkgame

C++

Posted by arkgame