Structures

  • A struct in C is a type consisting of a sequence of data members.
  • Some functions/statements are needed to operate the data members of an object of a struct type. | ```c struct Student { char name[4]; int born; bool male; };

struct Student stu; strcpy(stu.name, “Yu”); stu.born = 2000; stu.male = true;

  1. | ![image.png](https://cdn.nlark.com/yuque/0/2021/png/353587/1638276495873-ba1aef3a-b5b9-4505-86d1-bcf74257a056.png#clientId=u35a2e579-8ac4-4&from=paste&height=391&id=u7c01f24a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=391&originWidth=152&originalType=binary&ratio=1&size=7003&status=done&style=none&taskId=u7b18d101-62de-4e50-902a-84e2a8bb918&width=152) | 结构体<br />- 数据操作要非常小心<br />- 不经意间会发生越界<br /> |
  2. | --- | --- | --- |
  3. Classes
  4. - You should be very careful to manipulated the data members in a `struct` object.
  5. - Can we improve `struct` to a better one?
  6. - Yes, it is **class**! We can put some member functions in it.
  7. ```cpp
  8. class Student
  9. {
  10. public:
  11. char name[4];
  12. int born;
  13. bool male;
  14. void setName(const char * s)
  15. {
  16. // 成员函数,专门操作类中的数据
  17. strncpy(name, s, sizeof(name));
  18. }
  19. void setBorn(int b)
  20. {
  21. // pass
  22. }
Student yu;
yu.setName("Yu");
// 数据操作更加安全

Access Specifiers访问限定符

  • You can protect data members by access specifier private.
  • Then data member can only be accessed by well designed member functions.
  • 私有成员变量只能被类内的成员函数访问 ```cpp class Student { private: char name[4]; int born; bool male; public: void setName(const char * s) {
      strncpy(name, s, sizeof(name));
    
    } void setBorn(int b) {} }

Student yu; yu.born = 2001; // 无法修改

Member Functions<br />A member function can be defined inside or outside class.
<br />成员函数实现可以放到类内,也可只在类内进行声明,
```cpp
class Student
{
  private:
    char name[4];
    int born;
    bool male; 
  public:
    // 类内中定义了这个函数的具体实现,这个函数就是inline函数
    void setName(const char * s)
    {
        strncpy(name, s, sizeof(name));
    }
    void setBorn(int b)
    {
        born = b;
    }
    void setGender(bool isMale);
    void printInfo();
};
inline void Student::setGender(bool isMale)
{
    male = isMale;
}

void Student::printInfo()
{
    cout << "Name: " << name << endl;
    cout << "Born in " << born << endl;
    cout << "Gender: " << (male ? "Male" : "Female") << endl;
}

File Structures
那函数实现到底应该放在什么地方:

  • 如果是特别简单的函数实现,可以放在类内实现
  • 如果是复杂函数,建议放在类的外部定义。推荐类的声明放在头文件,函数的定义放在cpp文件,cpp文件中红应该include对应的头文件。

The source code can be placed into multiple files

// student.hpp

class Student
{
  private:
    char name[4];
    int born;
    bool male; 
  public:
    void setName(const char * s)
    {
        strncpy(name, s, sizeof(name));
    }
    void setBorn(int b)
    {
        born = b;
    }
    void setGender(bool isMale);
    void printInfo();
};
// student.cpp
#include "student.hpp"

void Student::setGender(bool isMale)
{
    male = isMale;
}
void Student::printInfo()
{
    cout << "Name: " << name << endl;
    cout << "Born in " << born << endl;
    cout << "Gender: " << (male ? "Male" : "Female") << endl;
}

:::info

include 后面有时候是<>,有时候是"",有什么说法:

  • <>:在编译器指定的include路径,寻找头文件
  • "":不光从编译器指定的include路径中寻找,还从当前目录中寻找 :::