「C++入門」クラスのコンストラクタを使うサンプル

書式
class クラス名
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
// クラスの定義
class TestClass {
public: // アクセス修飾子
TestClass() { // コンストラクタ
cout << "Study skill and become smart"<<endl;
cout << "this is a test";
}
};
int main() {
TestClass myObj; //オブジェクトを作成
return 0;
}
#include <iostream> using namespace std; // クラスの定義 class TestClass { public: // アクセス修飾子 TestClass() { // コンストラクタ cout << "Study skill and become smart"<<endl; cout << "this is a test"; } }; int main() { TestClass myObj; //オブジェクトを作成 return 0; }
#include <iostream>
using namespace std;

// クラスの定義
class TestClass {    
  public:           // アクセス修飾子
    TestClass() {     // コンストラクタ
      cout << "Study skill and become smart"<<endl;
      cout << "this is a test";
    }
};

int main() {
  TestClass myObj;    //オブジェクトを作成
  return 0;
}

実行結果
Study skill and become smart
this is a test

C++

Posted by arkgame