#include<iostream>
using namespace std;
class X {
public:
int m; // data member
int mf(int v) { int old = m; m=v; return old; } // function member
};
int main()
{
X var; // var is a variable of type X
var.m = 7; // access var’s data member m
int x = var.mf(9); // call var’s member function mf()
//运行完上面的一步,var.mf(9)返回的数值是var.m也就是7,给了x
//而把数值9赋值给了var.m
cout<<var.m<<endl;
cout<<x<<endl;
return 0;
}
/*
class X { // this class’ name is X
public: // public members -- that’s the interface to users
// (accessible by all)
// functions
// types
// data (often best kept private)
private: // private members -- that’s the implementation details
// (accessible by members of this class only)
// functions
// types
// data
};
*/
类成员默认是私有的 (private)
struct X { //struct的访问控制属性都是public int m; // … };
等价于 class X { public: int m; // … };
#include<iostream>
//#include<iomanip>
using namespace std;
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
int main()
{
Month m = feb;
int n = m; // ok: we can get the numeric value of a Month
Month mm = Month(7); // convert int to Month (unchecked)
cout<<mm<<endl<<n<<endl;
return 0;
}