简单的 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.
浙公网安备 33010602011771号