001_linuxC++之_类的引入

(一) C++类的引入,图片的程序比较好看,文中程序不贴出来
(二) 知识点
1. 成员函数的存取权限:公有的(public),保护的(protectd),私有的(private)
2. 第27行this->age是类中的。Age是第20行输入的Age
 1 #include <stdio.h>
 2 
 3 class Person{
 4 private:
 5     char *name;
 6     int age;
 7     char *work;
 8 
 9 public:
10     void setName(char *name);
11     int setAge(int Age);
12     void printInfo(void);
13 };
14 
15 void Person::setName(char *name)
16 {
17     this->name = name;
18 }
19 int Person::setAge(int Age)
20 {
21     if(Age < 0 || Age > 150)
22     {
23         this->age = 0;
24         return -1;
25     }
26     this->age = Age;
27     return 0;
28 }
29 void Person::printInfo(void)
30 {
31     printf("name = %s, age = %d, work = %s\n",name,age,work);
32 }
33 
34 int main(int argc,char ** argv)
35 {
36     Person per;
37     per.setName("zhangsan");
38     per.setAge(200);
39     per.printInfo();
40     return 0;
41 }
View Code

 

 

posted @ 2018-09-22 14:50  陆小果哥哥  阅读(199)  评论(0编辑  收藏  举报