一个类
数据成员:
int age;
char name[20];
数据成员的访问控制权限为private。
成员函数:
int getAge();//用于获取对象的年龄
char *getName();//用于获取对象的名字
成员函数和构造函数的访问控制权限为public
**礼貌求问:这个char *getName()怎么实现获取啊,一旦加上指针变成数组就不会用了
main函数是这样的:
int main()
{
Human jessic(12,"jessic");
cout<<"the human age is "<<jessic.getAge()<<endl;
cout<<"the name is "<<jessic.getName()<<endl;
}**
如果是char *getName() {return name;},main函数就报错了
改成
const char*getName() {return name;}
#include <iostream>
#include <cstring>
using namespace std;
class Human
{
public:
Human(int _age,char _name[])
{
age = _age;
for(int i=0;i<strlen(_name);++i)
name[i] = _name[i];
}
const char* getName(){return name;}
int getAge(){return age;}
private:
int age;
char name[20];
};
int main()
{
Human jessic(12,"jessic");
cout<<"the human age is "<<jessic.getAge()<<endl;
cout<<"the name is "<<jessic.getName()<<endl;
return 0;
}