V信公众号: 程序员架构笔记
欢迎关注
C++中,结构体(struct
)和类(class
)是两种用户定义的数据类型,它们都可以包含数据成员和成员函数。结构体和类的主要区别在于默认的访问控制权限:结构体的成员默认是public
,而类的成员默认是private
定义与使用
结构体是一种将不同类型的数据组合在一起的数据结构。结构体的定义使用struct
关键字。
#include <iostream> #include <string> // 定义一个结构体 struct Person { std::string name; int age; double height; }; int main() { // 创建结构体对象 Person person1; person1.name = "Alice"; person1.age = 25; person1.height = 1.68; // 访问结构体成员 std::cout << "Name: " << person1.name << ", Age: " << person1.age << ", Height: " << person1.height << std::endl; return 0; }
2. 类的定义与对象创建
类是一种将数据和操作数据的方法封装在一起的数据类型。类的定义使用class
关键字。
#include <iostream> #include <string> // 定义一个类 class Person { public: std::string name; int age; double height; void display() { std::cout << "Name: " << name << ", Age: " << age << ", Height: " << height << std::endl; } }; int main() { // 创建类对象 Person person1; person1.name = "Bob"; person1.age = 30; person1.height = 1.75; // 调用成员函数 person1.display(); return 0; }
3. 访问控制:public, private, protected
-
public
: 成员可以在类的外部访问。 -
private
: 成员只能在类的内部访问。 -
protected
: 成员可以在类的内部和派生类中访问。
#include <iostream> #include <string> class Person { private: std::string name; int age; double height; public: void setName(const std::string& n) { name = n; } void setAge(int a) { age = a; } void setHeight(double h) { height = h; } void display() { std::cout << "Name: " << name << ", Age: " << age << ", Height: " << height << std::endl; } }; int main() { Person person1; person1.setName("Charlie"); person1.setAge(35); person1.setHeight(1.80); person1.display(); // person1.name = "David"; // 错误:name 是 private 成员,不能直接访问 return 0; }
4. 构造函数与析构函数
-
构造函数:在创建对象时自动调用,用于初始化对象。
-
析构函数:在对象销毁时自动调用,用于释放资源。
#include <iostream> #include <string> class Person { private: std::string name; int age; double height; public: // 构造函数 Person(const std::string& n, int a, double h) : name(n), age(a), height(h) { std::cout << "Constructor called!" << std::endl; } // 析构函数 ~Person() { std::cout << "Destructor called!" << std::endl; } void display() { std::cout << "Name: " << name << ", Age: " << age << ", Height: " << height << std::endl; } }; int main() { Person person1("Eve", 40, 1.65); person1.display(); // 当 main 函数结束时,person1 的析构函数会被自动调用 return 0; }
5. 成员函数与this指针
-
成员函数:类中定义的函数,用于操作类的数据成员。
-
this指针:指向当前对象的指针,用于在成员函数中访问当前对象的成员。
#include <iostream> #include <string> class Person { private: std::string name; int age; double height; public: Person(const std::string& n, int a, double h) : name(n), age(a), height(h) {} void setName(const std::string& n) { this->name = n; // 使用 this 指针访问当前对象的成员 } void setAge(int a) { this->age = a; } void setHeight(double h) { this->height = h; } void display() { std::cout << "Name: " << this->name << ", Age: " << this->age << ", Height: " << this->height << std::endl; } }; int main() { Person person1("Frank", 45, 1.70); person1.display(); person1.setName("Grace"); person1.setAge(50); person1.setHeight(1.60); person1.display(); return 0; }
总结
-
结构体和类都可以包含数据成员和成员函数。
-
结构体的成员默认是
public
,类的成员默认是private
。 -
访问控制关键字
public
、private
和protected
用于控制成员的访问权限。 -
构造函数用于初始化对象,析构函数用于释放资源。
-
this
指针用于在成员函数中访问当前对象的成员