小言_互联网的博客

C++入门之程序结构

282人阅读  评论(0)

目录

类的引入与访问控制

权限

this 

程序结构

namespace

using

cout


类的引入与访问控制

在C语言中可以用struct来类比一个对象,但是struct比较局限,不能像C++可以有自己的成员函数并且可以访问内部成员,在C++中用class来定义一个类,因此struct和class的区别就在于struct不可以含有成员函数而C++可以,对于struct和class的主要区别在于默认的存取权限不同,struct默认是public,而class默认是private,在下面之所以将变量设置为私有属性,是保护一些重要的特性,避免用户做出错误的操作


  
  1. class Person {
  2. private:
  3. char *name;
  4. int age;
  5. char *work;
  6. public:
  7. void setName(char *n)
  8. {
  9. name = n;
  10. }
  11. int setAge(int a)
  12. {
  13. if (a < 0 || a > 150)
  14. {
  15. age = 0;
  16. return -1;
  17. }
  18. age = a;
  19. return 0;
  20. }
  21. void printInfo(void)
  22. {
  23. printf( "name = %s, age = %d, work = %s\n", name, age, work);
  24. }
  25. };

权限

对于private表示这些变量和函数只能在内部被使用,public表示类外的程序可使用,还有一种protected,对于private和protected的区别,后面再学习

类中属性 private protected public
内部可见性 可见 可见 可见
外部内可见性 不可见 不可见 可见

定义一个类之后,我们怎么去使用,对于上述类,我们可以用"Person per"来使用,在C++我们称per为Person这个类的对象,由于class中的private为私有成员,因此"per.name = "zhangsan";"在外部直接访问对象中私有成员会出错,利用此对象与C结构体一样


  
  1. int main(int argc, char **argv)
  2. {
  3. Person per;
  4. // per.name = "zhangsan";
  5. per.setName( "zhangsan");
  6. per.setAge( 200);
  7. per.printfInfo();
  8. return 0;
  9. }

this 

在类中通过提供函数来设置私有变量,当局部变量与类成员名字冲突时,可以通过术语this来表示,而this实质为一个指针,在C++中有一个就近原则,如下例子中,对于this->name为类中的name,而变量name与形参更近则表示传参的name


  
  1. class Person {
  2. private:
  3. char *name;
  4. int age;
  5. char *work;
  6. public:
  7. void setName(char *name)
  8. {
  9. this->name = name;
  10. }
  11. int setAge(int age)
  12. {
  13. if (age < 0 || age > 150)
  14. {
  15. this->age = 0;
  16. return -1;
  17. }
  18. this->age = age;
  19. return 0;
  20. }
  21. void printInfo(void)
  22. {
  23. printf( "name = %s, age = %d, work = %s\n", name, age, work);
  24. }
  25. };

对于类中的函数成员可以只声明而不定义其函数体,在外部可以通过::引用来定义类中的函数


  
  1. class Person {
  2. private:
  3. char *name;
  4. int age;
  5. char *work;
  6. public:
  7. void setName(char *name);
  8. int setAge(int age);
  9. void printInfo(void);
  10. };
  11. void Person::setName(char *name)
  12. {
  13. this->name = name;
  14. }
  15. int Person::setAge(int age)
  16. {
  17. if (age < 0 || age > 150)
  18. {
  19. this->age = 0;
  20. return -1;
  21. }
  22. this->age = age;
  23. return 0;
  24. }
  25. void Person::printInfo(void)
  26. {
  27. printf( "name = %s, age = %d, work = %s\n", name, age, work);
  28. }

 

程序结构

对于一个程序分多个人写,假设有两个人A和B,对于A来说写main.cpp,他只关心怎么用Person这个类,对于B来说为写Person这个类,只需要提供Person.h和Person.cpp给main.cpp使用,如果后面来个新人C假设写一个dog类,跟B一样的任务

B负责person.cpp和person.h


  
  1. /person.cpp/
  2. #include <stdio.h>
  3. #include "person.h"
  4. namespace A {
  5. void Person::setName(char *name)
  6. {
  7. this->name = name;
  8. }
  9. int Person::setAge(int age)
  10. {
  11. if (age < 0 || age > 150)
  12. {
  13. this->age = 0;
  14. return -1;
  15. }
  16. this->age = age;
  17. return 0;
  18. }
  19. void Person::printInfo(void)
  20. {
  21. printf( "name = %s, age = %d, work = %s\n", name, age, work);
  22. }
  23. void printVersion(void)
  24. {
  25. printf( "Person v1, by weidongshan\n");
  26. }
  27. }
  28. /person.h/
  29. #include <stdio.h>
  30. namespace A {
  31. class Person {
  32. private:
  33. char *name;
  34. int age;
  35. char *work;
  36. public:
  37. void setName(char *name);
  38. int setAge(int age);
  39. void printInfo(void);
  40. };
  41. void printVersion(void);
  42. }

C负责dog.cpp和dog.h


  
  1. /dog.cpp/
  2. #include <stdio.h>
  3. #include "dog.h"
  4. namespace C {
  5. void Dog::setName(char *name)
  6. {
  7. this->name = name;
  8. }
  9. int Dog::setAge(int age)
  10. {
  11. if (age < 0 || age > 20)
  12. {
  13. this->age = 0;
  14. return -1;
  15. }
  16. this->age = age;
  17. return 0;
  18. }
  19. void Dog::printInfo(void)
  20. {
  21. printf( "name = %s, age = %d\n", name, age);
  22. }
  23. void printVersion(void)
  24. {
  25. printf( "Dog v1, by weidongshan\n");
  26. }
  27. }
  28. /dog.h/
  29. namespace C {
  30. class Dog {
  31. private:
  32. char *name;
  33. int age;
  34. public:
  35. void setName(char *name);
  36. int setAge(int age);
  37. void printInfo(void);
  38. };
  39. void printVersion(void);
  40. }

namespace

上述代码中对于B和C两个的类成员外都有一个版本打印函数并且函数名字相同,对于C语言和C++来说这么写会冲突,但是C++可以避免这种问题,引出命名空间的概念"namespace",对于A写main.cpp的人来说,可以通过::来引用命名空间的同名的函数


  
  1. #include <stdio.h>
  2. #include "person.h"
  3. #include "dog.h"
  4. int main(int argc, char **argv)
  5. {
  6. A::Person per;
  7. per.setName( "zhangsan");
  8. per.setAge( 16);
  9. per.printInfo();
  10. C::Dog dog;
  11. dog.setName( "wangcai");
  12. dog.setAge( 1);
  13. dog.printInfo();
  14. A::printVersion();
  15. C::printVersion();
  16. return 0;
  17. }

using

利用using来使用namespace,对于"using A::Person;",把A::Person放入global namespace, 以后就可以使用Person来表示"A::Person",在main函数中就是local namespace,因此"Person per"前提需要using来包含命名空间


  
  1. #include <stdio.h>
  2. #include "person.h"
  3. #include "dog.h"
  4. using A::Person;
  5. using C::Dog;
  6. using A::printVersion;
  7. using C::printVersion;
  8. int main(int argc, char **argv)
  9. {
  10. /* local namespace */
  11. //using A::Person;
  12. //using C::Dog;
  13. Person per;
  14. per.setName( "zhangsan");
  15. per.setAge( 16);
  16. per.printInfo();
  17. Dog dog;
  18. dog.setName( "wangcai");
  19. dog.setAge( 1);
  20. dog.printInfo();
  21. A::printVersion();
  22. C::printVersion();
  23. return 0;
  24. }

使用"using namespace A" 把命名空间全部包含进来,把空间内所有的都导进来,两个都导进来之后就必须指定冲突函数的空间,但是对于printVersion,一样都需要"A::printVersion"来区别调用,当有冲突的函数里面直接使用命名函数就可以了,所以包含namespace也是一样的道理


  
  1. using namespace A;
  2. using namespace C;
  3. int main(int argc, char **argv)
  4. {
  5. /* local namespace */
  6. //using A::Person;
  7. //using C::Dog;
  8. Person per;
  9. per.setName( "zhangsan");
  10. per.setAge( 16);
  11. per.printInfo();
  12. Dog dog;
  13. dog.setName( "wangcai");
  14. dog.setAge( 1);
  15. dog.printInfo();
  16. A::printVersion();
  17. C::printVersion();
  18. return 0;
  19. }

cout

在C++中一般用cout来打印,cout是标准std命名空间,需要定义iostream头文件


  
  1. #include <iostream>
  2. using namespace std;
  3. void Person::printfInfo(void){
  4. // printf("name = %s, age = %d, work = %s\n", name, age, work);
  5. cout<< "name = "<<name<< " age = "<<age<< " work = "<<work<< endl;
  6. }
  7. void printVersion(void)
  8. {
  9. // printf("Person v1, by xiaoma\n");
  10. cout<< "Person v1, by xiaoma"<< endl;
  11. }
  12. //若没有包含命名空间std,则可以用::来引用
  13. void Person::printfInfo(void){
  14. std:: cout<< "name = "<<name<< " age = "<<age<< " work = "<<work<< std:: endl;
  15. }
  16. void printVersion(void)
  17. {
  18. std:: cout<< "Person v1, by xiaoma"<< std:: endl;
  19. }

 

 


转载:https://blog.csdn.net/weixin_45775710/article/details/117195871
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场