(一) C++类的引入,图片的程序比较好看,文中程序不贴出来
(二) 知识点
- 成员函数的存取权限:公有的(public),保护的(protectd),私有的(private)
- 第27行this->age是类中的。Age是第20行输入的Age
#include <stdio.h>
class Person{
private:
char *name;
int age;
char *work;
public:
void setName(char *name);
int setAge(int Age);
void printInfo(void);
};
void Person::setName(char *name)
{
this->name = name;
}
int Person::setAge(int Age)
{
if(Age < 0 || Age > 150)
{
this->age = 0;
return -1;
}
this->age = Age;
return 0;
}
void Person::printInfo(void)
{
printf("name = %s, age = %d, work = %s\n",name,age,work);
}
int main(int argc,char ** argv)
{
Person per;
per.setName("zhangsan");
per.setAge(200);
per.printInfo();
return 0;
}