简单的 constructor 和 method

#include <iostream>
#include <string.h>
using namespace std;

class Hello
{
private:
  int a;
public:
  Hello();                         //constructor
  void IntroduceSelf();      //method
};

Hello::Hello()
{
  a = 5;
  cout << "a is: " << a << endl;
}

void Hello::IntroduceSelf()  //donot forget void
{
  cout << "I am " << a << " years old." << endl;
}



int main()
{
  Hello sayhello;               //auto run constructor
  sayhello.IntroduceSelf();    //run method


  return 0;
}

  

# ./a.out
a is: 5
I am 5 years old.

  

posted on 2019-08-16 13:19  cdekelon  阅读(97)  评论(0)    收藏  举报

导航