(一)程序修改001_linuxC++之_类的引入
(二)修改成为.h和.c文件
1 #include <stdio.h> 2 #include "person.h" 3 4 int main(int argc,char ** argv) 5 { 6 Person per; 7 per.setName("zhangsan"); 8 per.setAge(200); 9 per.printInfo(); 10 return 0; 11 }
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 };
1 #include <stdio.h> 2 #include "person.h" 3 4 void Person::setName(char *name) 5 { 6 this->name = name; 7 } 8 int Person::setAge(int Age) 9 { 10 if(Age < 0 || Age > 150) 11 { 12 this->age = 0; 13 return -1; 14 } 15 this->age = Age; 16 return 0; 17 } 18 void Person::printInfo(void) 19 { 20 printf("name = %s, age = %d, work = %s ",name,age,work); 21 }