声明:所有代码根据谭浩强老师的红皮书例子学习写的。
包含类的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;
}
本文通过谭浩强老师的红皮书中的例子介绍了如何使用C++实现简单的类与对象操作。程序定义了一个名为Student的类,其中包括两个私有成员变量a和b,并提供了设置和显示这些成员变量值的方法。
2668

被折叠的 条评论
为什么被折叠?



