1.5 Structures and Classes
Return to directory 1 Object-Oriented Technology
Last Section 1.4 cpp programming foundation
Next section 1.6 Common features of cpp
structural morphology
source code
/* struct.cpp Examples of Structures */ #include <iostream> #include <string> struct Student { std::string name; // Full name std::string sex; // Gender int age; // Age }typedef Student; void STUDENT_dispalyInfos(Student student) { /* This function is used to display student information. */ std::cout << "Student information" << std::endl; std::cout << "Full name:" << student.name << std::endl; std::cout << "Gender:" << student.sex << std::endl; std::cout << "Age:" << student.age << std::endl; } int main() { Student XiaoMing; XiaoMing.name = "Xiao Ming"; XiaoMing.sex = "male"; XiaoMing.age = 18; STUDENT_dispalyInfos(XiaoMing); return 0; }
Compile and run
Student information Name: Xiao Ming Gender: Male Age: 18
class
source code
/* class.cpp Class instance */ #include <iostream> #include <string> class Student { private: std::string name; // Full name std::string sex; // Gender int age; // Age public: Student(); // This is the constructor. Student(std::string name, std::string sex, int age); // Overloading Constructors ~Student(); // Destructor void Init(std::string name, std::string sex, int age); // Initialization function void displayInfos(); // Functions that display member information }; Student::Student() { std::cout << "-++When a class is instantiated, the constructor is called first" << std::endl; this->name = ""; this->sex = ""; this->age = 0; std::cout << "+++" << name << "Initialization complete" << std::endl; } Student::Student(std::string name, std::string sex, int age) { std::cout << "-++When a class is instantiated, the constructor is called first" << std::endl; this->name = ""; this->sex = ""; this->age = 0; std::cout << "+++" << name << "Initialization complete" << std::endl; Init(name, sex, age); } Student::~Student() { std::cout << "-++When an object is destroyed, the destructor is called" << std::endl; std::cout << "+++" << this->name << "destroyed" << std::endl; } void Student::displayInfos() { std::cout << "--Student Information————" << std::endl; std::cout << "-Full name:" << this->name << std::endl; std::cout << "-Gender:" << this->sex << std::endl; std::cout << "-Age:" << this->age << std::endl; std::cout << "------" << std::endl; } void Student::Init(std::string name, std::string sex, int age) { std::cout << "--+" << name << "Complete initialization" << std::endl; this->name = name; this->sex = sex; this->age = age; } int main() { Student XiaoMing; // Calling an argument-free constructor when instantiating a class XiaoMing.Init("Xiao Ming", "male", 20); // Manual Initialization Function Call Student XiaoHong("Xiaohong", "female", 20); // When class is instantiated, the constructor with parameters is called and initialized directly. XiaoMing.displayInfos(); // display information XiaoHong.displayInfos(); return 0; }
Compile and run
- The constructor is called first when the + + class is instantiated + ++ Initialization Completion --+Xiaoming completes initialization - The constructor is called first when the + + class is instantiated + ++ Xiaohong Initialization Completed --+Xiaohong completes initialization ———— Student Information———— —— Name: Xiao Ming —— Gender: male —— Age: 20 ------ ———— Student Information———— —— Name: Xiaohong —— Gender: female —— Age: 20 ------ - Destructors are called when ++ objects are destroyed + + + Xiaohong has been destroyed - Destructors are called when ++ objects are destroyed + ++ Xiaoming has been destroyed
As you can see, structs are very similar to classes, but functions are allowed within classes, and default constructors (which run automatically when the class is instantiated and can be overloaded) and default destructors (which run automatically at the end of the object life cycle and recycle resources) are available.
The first constructed object is then destructed.
Class can be seen as a separate small program, as the basic machine of the whole project exists, we will generally write as little as possible in the main function a large number of code, but concentrated in the class or function, call them directly in the main function.
This points directly to the members of the class corresponding to the instantiated object, and in this case is used to distinguish the external parameters from the data members of the object.
Return to directory 1 Object oriented technology
Last Section 1.4 cpp programming foundation
Next section 1.6 Common features of cpp
Reference material:
- C++ Programming Podcast
- Baidu Encyclopedia