`

一步一步学C++2 C++类的使用

阅读更多
声明:所有代码根据谭浩强老师的红皮书例子学习写的。


包含类的C++程序
#include <iostream>
using namespace std;
class Student
{
private:
	int a;
	int b;
public:
	setname()
	{
		cin>>a;
		cin>>b;
	}
	showname()
	{
		cout<<"名字="<<a<<endl;
		cout<<"年龄="<<b<<endl;
	};
};

Student stu1,stu2;

int main()
{
	stu1.setname();
	stu2.setname();
	stu1.showname();
	stu2.showname();
	return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics