class knowledge
Overview of classes: Classes are the core features of C++. In C++, in addition to existing data types (integer, character), you can also customize data types, which are often referred to as user-defined types. A class is used to specify the form of an object, it contains data representation (data classes) and methods for manipulating the data (operation classes). The data and methods in a class are called members of the class. Functions in a class are called members of the class.
Class Definition: A class definition starts with the keyword class followed by the name of the class. The body of the class is enclosed in a pair of curly braces. A class definition must be followed by a semicolon or a list of declarations. For example, we define the Box data type using the keyword class as follows:
class Box { public://public members can be accessed outside the class double length; // the length of the box double breadth; // the width of the box double height; // height of the box };
Note:
① If the class definition and the main function are in the same source file, then you may encounter such a problem: before the class definition, the main function uses this class. This will cause an error to occur because the main function is not aware of the existence of this class. So the existence of this class must be declared before the main function.
② You can also define the class in the header file, and then include the header file in the .Cpp source file. Since the header file is included before the main function, you do not need to declare this class in the main function.
Student.h //Header file class Student //definition of the student class { …… }; main.cpp //file #include "Student.h" //Note that double quotes must be used here, not angle brackets intmain() { …… }
Create Objects: Objects are created from classes. Declare an object of a class just like declaring a variable of a primitive type.
Box B; // declare object B, the type is Box
Object reference:
The reference method to declare an object is: class name & object name a = object name b;
Accessing and manipulating object a is the same as accessing and manipulating object b, which is just a "nickname" for object b.
string A; //declare a string object string &B=A; //declare a reference B.append("ABC"); //The effect is the same as A.append("ABC")
Object pointer: The so-called object pointer is a pointer to an object
string A; //declare a string object string *B=&A; //declare an object pointer B->append("ABC"); //The effect is the same as A.append("ABC")
[Arrow operator -> member data or member functions of the object pointed to by the pointer can be accessed]
Access data members:
When accessing data members, you can use the access operator . to operate, and private [Private] members and protected [ Protected ] members cannot be accessed directly using the direct member access operator . .
#include <iostream> using namespace std; class Box //Define the Box class { public://public member double length; // length double breadth; // width double height; // height }; int main( ) { Box Box1; // Declare the Box1 object of type Box double volume = 0.0; // used to store the volume // box 1 details Box1.height = 10.0; Box1.length = 10.0; Box1.breadth = 10.0; // the volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout << "Box1 的体积:" << volume <<endl; return 0; }
Note: Member data (or member functions) of the same class can be used directly in its member functions. When using the public member data of an object outside this class, it is necessary to write "object name.member data", but it is not required and cannot be written like that in member functions.
Constant member function:
Adding const after the member function and using the constant member function ensures the safety of member data. Any statement in this function that modifies member data will be rejected by the compiler.
int readNo() const; //The function to read the student number can only read and cannot modify the data
Overloading of member functions:
Similar to ordinary functions, there can also be overloaded member functions in a class, but the number of parameters, the data type and order of each parameter in the parameter table of any two functions with the same name cannot be exactly the same.
void set(int No,string Name); //Set student number name void set(); // reset student number name
Constructor:
A constructor is a function that is automatically called when an object is created, and its main purpose is to initialize an object. In C++, a member function with the same name as a class is defined as a constructor. The constructor should be a public member function, and the constructor has no return type.
#include<bits/stdc++.h> using namespace std; class Student//Student data class { private: string name;//Name int no;//student number int score[3];//Non-numerical score float average;//average grade int order;//Rank public: Student(int id,string na,int x,int y,int z):name(na),no(id){//Constructor with parameters score[0]=x,score[1]=y,score[2]=z; order=1,average=(score[0]+score[1]+score[2])/3; } Student(){//No parameter constructor score[0]=score[1]=score[2]=0; order=1,average=0; } };