Series Notes
-
[Basic Data Structure] () -
[Use of classes] () -
[Inheritance and derivation of class] () -
[One of the object-oriented features polymorphism] () -
[Operator overload] () -
[Use of const keyword] ()
1. Definition and use of classes
1.1 Definition of Class
The simplest class declaration:
class Student{ };
Use of Class 1.2
Student studentObject;
2. Member variables and member functions of classes
2.1 Member Variables
class Student { char name[20]; // full name Int id_num;//Student ID Int age;//Age Char sex;//Gender
2.2 Member Functions
1. Class internal declaration, class external definition (implementation):
class student { void set_age(int a); int get_age(); }; //Define set_age function outside class void student::set_age(int a) { age = a; } //Define the get_age function outside the class int student::get_age() { return age; }
Return type class name:: member function name (parameter list) { //Function body }
class student { inline void set_age(int a); inline int get_age(); }; //Define set_age function outside class void student::set_age(int a) { age = a; } //Define the get_age function outside the class int student::get_age() { return age; }
2. Declare and define the inner side of the class (implementation)
class Student{ void set_age(int a){age = a;} int get_age(){return age;} }
2.3 Access restrictions
public : This class and other classes can access private : Only this class can access protected : Only this class and derived classes can access.
class book { public: void setprice(double a); double getprice(); private: double price; };
2.4 Accessing Member Variables/Functions
#include <iostream> using namespace std; class book { public: void setprice(double a); double getprice(); private: double price; }; void book::setprice(double a) { price = a; } double book::getprice() { return price; } int main() { book Alice; Alice.setprice(29.9); cout<<"The price of Alice is $"<<Alice.getprice()<<endl; book *Harry = new book; Harry->setprice(49.9); cout<<"The price of Harry is $"<<Harry->getprice()<<endl; return 0; }
-
With the struct keyword, member variables or member functions defined in the structure are public by default. -
With the class keyword, the member variables or member functions defined in the class are private by default,
3. Constructor
3.1 Definition of constructor
-
The function name of the constructor must be the same as the class name; -
The constructor has no return value; -
When we create class objects, the constructor will be automatically called, without our initiative to call.
class book { public: book(){} book(char* a, double p); private: double price; char[] title; }; book::book(char[] a, double p) { title = a; price = p; } int main() { book Harry("Harry Potter", 49.9); return 0; }
3.2 Initialization parameter table
Constructor (parameter list): member variable 1 (initial value), member variable 2 (initial value)...... {}
class book { public: book(){} book(char *a, double p):title(a),price(p){} };
book(char *a, double p){ title = a; price = p; }
3.3 Default values of constructor parameters
function exampleFunction($parm1,$parm2 = "world"){ echo $parm1 ." ". $parm2; } exampleFunction("hello") //Output: hello world;
class book { public: book(){} book(char* a, double p = 5.0); private: double price; char * title; }; Book:: book (char * a, double p)//You can not specify default parameters when defining functions { title = a; price = p; }
Book Time("Time book")
book(char* a);
*3.4 Transformation and Copy Constructor
3.4.1 Transformation constructor
-
Constructor without parameters -
Constructors with parameters (including constructors with default parameters)
-
Transition constructor -
copy constructor
class student { public: Student (char * n) {name=n;}//transformation constructor private : char * name; } void fun(Student studentObj){}; char * name = “Harry Potter”; fun(name);
3.4.3 Copy constructor
4. Destructor
-
No return value -
No parameter, cannot be overloaded, so a class can only contain one destructor -
The function name must be in the form of "~class name", and there can be a space between the symbol "~" and the class name
5. Advanced usage of classes
5.1 Constant pointer this
void setprice(double price) { price = price; }
5.2 New and delete operators
-
The first kind of space is allocated in the stack and can be directly managed by the system. For example, outside the code block, the objects in the block will be cleaned up. -
The second type of space is allocated on the heap. The size can be large, but the destruction is handled by the program itself.
5.3 const keyword
5.3.1 Modifying (member) variables
const int a=5;
5.3.2 Modifying member functions
Fun (formal parameter) const {}
5.3.3 Decorative objects
class A { public: void funcA() {} void funcB() const {} }; int main { const A a; a.funcB(); // sure a.funcA(); // error const A* b = new A(); b->funcB(); // sure b->funcA(); // error }
5.3.4 Reference of decorated objects
void display(const book &b) { b.setprice(59.9); // compile error cout<<"The price of "<<b.gettitle()<<" is $"<<b.getprice()<<endl; //ok }
5.3.5 Overloading Member Functions
5.4 Static keyword
5.4.1 Static Member Variables
5.4.2 Static Member Functions
5.5 Friend Functions and Friend Classes
5.5.1 Friend function
friend Return type function name (formal parameter);
-
The declaration of friend function can be placed in the private part of the class or in the public part. There is no difference between them, which means that they are a friend function of the class. -
A function can be a friend function of multiple classes, and only needs to be declared separately in each class. -
The method and principle of calling friend functions are consistent with those of general functions.
5.5.2 Friends
Friend class name;
class A { … public: friend class B; … };
-
Friendship cannot be inherited. -
The friendship relationship is one-way and not exchangeable. If class B is a friend of class A, class A is not necessarily a friend of class B. It depends on whether there is a corresponding declaration in the class. -
Friendship is not transitive. If class B is a friend of class A, and class C is a friend of class B, class C is not necessarily a friend of class A. It also depends on whether there is a corresponding declaration in the class