C++学习 classes

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值