C++的使用小教程1——类的创建和使用

以你之姓@ 2024-04-18 21:15 58阅读 0赞

C++的使用小教程1——类的创建和使用

  • 1、类是什么
  • 2、类的定义
    • 2.1、声明与定义在同一文件
    • 2.2、声明与定义在不同文件
    • 2.3、C++类的创建和使用中的约定

最近进入了研究生课题组,老师布置了一个我完全不会的任务……但是我还是得学习啊,毕竟我还要养我的女朋友。
在这里插入图片描述

1、类是什么

C ++ 中的是可以通过用户定义的一种数据类型。 在类当中,既可以包含数据,也可以包含函数,在对类内的函数与数据进行调用时,存在特定的调用方法。
听起来有点复杂,所以让我们看一个类的例子。
让我们创建一个名为”person”的类。这时候我们想,对于一个”person”而言,其应该具有什么”属性”与”能力”。
假设一个person可以做的事情如下:
在这里插入图片描述
person的属性对应着类的数据,person的能力对应着类的函数
我们可以得到一个类如下:

  1. class Person
  2. {
  3. private:
  4. string name;
  5. int age;
  6. int sex;
  7. int monthlyIncome;
  8. public:
  9. void setParameter(String nameIn,int ageIn,
  10. int sexIn,int monthlyIncomeIn);
  11. void selfIntroduction();
  12. void mining();
  13. void catchMouse();
  14. };

在private里的,都是类的数据;在public里的,都是类的方法(函数)。一般而言我们通过函数来访问与修改类的数据,这样可以对类形成一定的封装,从而保护数据不会被胡乱修改与破坏。
这句话的重点是: 我们可以使用函数来访问类中的数据。
这也是C++类的封装特性的具体表现。
至此,我们就已经完成了类中数据与函数的”声明“,“声明“代表了”存在”,但是如何具体实现对应着”定义

2、类的定义

类的定义方式有很多,我们可以将类的声明和定义放在同一个文件里,也可以将二者分开,接下来我将分别讲述两种方法的实现。

2.1、声明与定义在同一文件

该例子将类的声明与定义放在同一文件中,同时在main函数中调用类。

  1. #include<iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. private:
  7. string name;
  8. int age;
  9. string sex;
  10. int monthlyIncome;
  11. public:
  12. void setParameter(string nameIn, int ageIn,
  13. string sexIn, int monthlyIncomeIn);
  14. void selfIntroduction();
  15. void mining();
  16. void catchMouse();
  17. };
  18. void Person::setParameter(string nameIn, int ageIn,
  19. string sexIn, int monthlyIncomeIn)
  20. {
  21. //ToDo: finish this function
  22. name = nameIn;
  23. age = ageIn;
  24. sex = sexIn;
  25. monthlyIncome = monthlyIncomeIn;
  26. }
  27. void Person::selfIntroduction()
  28. {
  29. cout << "My name is " << name << ", I am " << age << " years old, I am a " << sex << " , my monthlyIncome is " << monthlyIncome <<"."<< endl;
  30. }
  31. void Person::mining()
  32. {
  33. cout << "I am mining."<<endl;
  34. }
  35. void Person::catchMouse()
  36. {
  37. cout << "I am the best, I caught a mouse" << endl;
  38. }
  39. int main() {
  40. Person person;
  41. person.setParameter("Tom", 20, "girl", 10000);
  42. person.selfIntroduction();
  43. person.mining();
  44. person.catchMouse();
  45. system("pause");
  46. return 0;
  47. }

调用结果为:

  1. My name is Tom, I am 20 years old, I am a girl , my monthlyIncome is 10000.
  2. I am mining.
  3. I am the best, I caught a mouse
  4. 请按任意键继续. . .

2.2、声明与定义在不同文件

C++中许多库都将类的声明放在头文件.h中,将类的定义放在源文件.cpp中,而调用则发生在另一个有main函数的源文件中,这样做相比于所有函数都放在一个文件里,更加清晰便于阅读,当然其优点不仅仅如此。
person.h文件:

  1. #pragma once
  2. #include<iostream>
  3. #include <string>
  4. using namespace std;
  5. class Person
  6. {
  7. private:
  8. string name;
  9. int age;
  10. string sex;
  11. int monthlyIncome;
  12. public:
  13. void setParameter(string nameIn, int ageIn,
  14. string sexIn, int monthlyIncomeIn);
  15. void selfIntroduction();
  16. void mining();
  17. void catchMouse();
  18. };

person.cpp文件,需要注意的是,该文件需要载入头函数#include “person.h”:

  1. #include<iostream>
  2. #include <string>
  3. #include "person.h"
  4. using namespace std;
  5. void Person::setParameter(string nameIn, int ageIn,
  6. string sexIn, int monthlyIncomeIn)
  7. {
  8. //ToDo: finish this function
  9. name = nameIn;
  10. age = ageIn;
  11. sex = sexIn;
  12. monthlyIncome = monthlyIncomeIn;
  13. }
  14. void Person::selfIntroduction()
  15. {
  16. cout << "My name is " << name << ", I am " << age << " years old, I am a " << sex << " , my monthlyIncome is " << monthlyIncome << "." << endl;
  17. }
  18. void Person::mining()
  19. {
  20. cout << "I am mining." << endl;
  21. }
  22. void Person::catchMouse()
  23. {
  24. cout << "I am the best, I caught a mouse" << endl;
  25. }

main.cpp调用上述库:

  1. #include<iostream>
  2. #include <string>
  3. #include "person.h"
  4. using namespace std;
  5. int main() {
  6. Person person;
  7. person.setParameter("Tom", 20, "girl", 10000);
  8. person.selfIntroduction();
  9. person.mining();
  10. person.catchMouse();
  11. system("pause");
  12. return 0;
  13. }

调用结果同上。

2.3、C++类的创建和使用中的约定

1、需要使用命名空间classname ::来表示函数是类定义的一部分。

  1. void Person::catchMouse()
  2. {
  3. cout << "I am the best, I caught a mouse" << endl;
  4. }

2、使用 ‘getVariableName’ 来访问数据,使用 ‘setVariableName’ 来修改数据。(这个我上述为了方便没有遵守)

  1. class Person
  2. {
  3. ......
  4. public:
  5. void setName(string nameIn);
  6. void setSex(int idIn);
  7. ......
  8. string getName();
  9. int getSex();
  10. ......
  11. };

3、大写类名的第一个字母。

  1. class Person

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读