1, Functions
(1) Function default parameters
- In C + +, the formal parameters in the formal parameter list of a function can have default values.
The exercise code is as follows:
#include <iostream> using namespace std; int func01(int a, int b, int c) { return a + b + c; } int func02(int a, int b = 20, int c = 30) { return a + b + c; } int main() { cout << func01(10, 20, 30) << endl; cout << func02(10) << endl; cout << func02(10, 30) << endl; cout << func02(10, 30, 40) << endl; //Conclusion: if we pass in parameters, we will use our own data. If not, we will use the default value /* * matters needing attention: * 1,If a location already has default parameters, it must have default values from left to right from this location * int func(int a,int b=10,int c){}It's wrong * Must be int func(int a,int b=10,int c=10) {} to be correct * * 2,If the declaration of a function has default parameters, the implementation of the function cannot have default parameters * That is, declaration and implementation can only have one default parameter * int func(int a =10,int b=10);//statement * int func(int a=10,int b=10)//Wrong will make the compiler ambiguous * int func(int a,int b)//correct * { * return a+b; * } */ return 0; }
(2) Function placeholder parameter
- There can be placeholder parameters in the formal parameter list of functions in C + +, which are used for placeholder. This position must be filled when calling functions
- Placeholder parameters can have default parameters
- The space occupying parameters are not available at this stage, which will be described in detail later
#include <iostream> using namespace std; //At the present stage, the space occupying parameters are not used, and they will be introduced in detail later void func(int a, int)//int is a placeholder { cout << "this is func" << endl; } void func01(int a, int = 10) { cout << "this is func01" << endl; } int main() { //func(10);//error func(10, 20);//Must have parameters func01(10);//error return 0; }
(3) Function overloading
Function: function names can be the same to improve reusability
Function overload meets the following conditions:
- Under the same scope
- Same function name
- Function parameters have different types, numbers or orders
Note: the return value of a function cannot be used as a condition for function overloading
The exercise code is as follows:
#include <iostream> using namespace std; //Function overloading: improve function reusability void func() { cout << "func Call of" << endl; } void func(int a) { cout << "func(int a)Call of" << endl; } void func(double a) { cout << "func(double a)Call of" << endl; } void func(int a, char b) { cout << "func(int a,char b)Call of" << endl; } void func(char b, int a) { cout << "func(char b, int a)Call of" << endl; } int main() { func(); func(10); func(3.14); func(10, 'a'); func('a', 10); return 0; }
Precautions for function overloading:
- Reference as overload condition
- Function overload encountered function default parameter
Case code 1:
#include <iostream> using namespace std; //Reference as overload condition void func(int& a)//int& a = 10;// wrongful { cout << "func Call of" << endl; } void func(const int& a)//const int& a=10; Legal code { cout << "const func Call of" << endl; } int main() { //int a = 10; //func(a);// Call the first function func(10);//Call the second function return 0; }
Conclusion:
Case code 2:
#include <iostream> using namespace std; //Function overload encountered function default parameter void func(int a, int b = 10) { cout << "func(int a)" << endl; } void func(int a) { cout << "func(int a)" << endl; } int main() { func(10);//error both functions can be called, which is ambiguous and needs to be avoided as much as possible func(10, 20);//Correct, no ambiguity //Suggestion: function overloading and default parameters should not appear at the same time, which can easily lead to ambiguity return 0; }
2, Classes and objects -- encapsulation
- The three characteristics of C + + object-oriented are encapsulation, inheritance and polymorphism
- C + + believes that everything is an object, and the object has its properties and behavior
For example:
- People can be objects. Their attributes include name, age, height, etc. their behaviors include walking, eating, sleeping, etc
- Cars can be used as objects, with properties such as tires and steering wheel, and behaviors such as carrying people and playing music
- Wait, wait
(1) Meaning of encapsulation
Meaning of encapsulation:
- Take attributes and behaviors as a whole to express things in life
- Control attributes and behaviors with permissions
Case code 1: calculate the circumference of a circle
#include <iostream> using namespace std; const double PI = 3.14;//PI //Design a circle class to find the circumference of the circle: 2*PI*r //The meaning of encapsulation: encapsulate attributes and behaviors into a whole class Circle { //attribute public: int m_r;//radius //Behavior: get the circumference of a circle double calculateZC() { return 2 * PI * m_r; } }; int main() { //Instantiate an object through a class Circle c1; //Assign values to the attributes of the circle object c1.m_r = 10; cout << "The circumference of the circle is:" << c1.calculateZC() << endl; return 0; }
Case code 2: design a student class with the attributes of name and student number. You can assign values to the name and student number, and display the student's name and student number
#include <iostream> #include <string> using namespace std; class Student { public: string name;//full name string id;//Student number void ShowInfo() { cout << "full name:" << name << endl; cout << "Student No.:" << id << endl; } }; int main() { Student s1; s1.name = "Zhang San"; s1.id = "1831050072"; s1.ShowInfo(); return 0; }
Case 2 of improving program robustness:
#include <iostream> #include <string> using namespace std; class Student { public: string m_name;//full name int m_id;//Student number //Assign a name void SetName(string name) { m_name = name; } //Assign value to student number void SetId(int id) { m_id = id; } void ShowInfo() { cout << "full name:" << m_name << endl; cout << "Student No.:" << m_id << endl; } }; int main() { Student s1; s1.SetName("Zhang San"); s1.SetId(1000); s1.ShowInfo(); return 0; }
- The properties and behaviors in a class are collectively called members
- Attribute is also called member attribute and member variable
- Behavior is also called member function and member method
(2) Access rights
When designing classes, attributes and behaviors can be controlled under different permissions.
There are three types of access rights
- Public public permissions
- Protected protected permissions
- Private private permissions
public
- Accessible within class
- It can also be accessed outside the class
protected
- Accessible within class
- Not accessible outside class
- The son can access the father's protected content
private
- Accessible within class
- Not accessible outside class
- The son cannot access his father's private content
The exercise code is as follows:
#include <iostream> #include <string> using namespace std; class Person { public: string m_name; protected: string m_car; private: int m_password; public: void func() { //All three member properties are accessible within the class m_name = "Zhang San"; m_car = "bmw"; m_password = 123456; } }; int main() { Person p1; p1.m_name = "Li Si"; //p1.m_car = "Benz"// error protection permissions are not accessible outside the class //p1.m_password = 123;//error private permissions are not accessible outside the class return 0; }
The difference between struct and class
In C + +, both struct and class can be used to design a class. The only difference is that the default access permissions are different
difference
- The default permission of struct is public permission
- class the default permission is private
(3) Set member properties to private
In C + +, setting member properties to private has the following two advantages
- Set all member properties to private, and you can control the read and write permissions yourself
- For write permission, we can check the validity of the data
#include <iostream> #include <string> using namespace std; //Set member properties to private class Person { public: //Set name void SetName(string name) { m_name = name; } //Get name string GetName() { return m_name; } //Get the age readable and writable. If you want to modify it (the age range must be 0-150) int Getage() { //m_age = 0; return m_age; } void SetAge(int age) { if (age < 0 || age>150) { m_age = 0; cout << "You are an old goblin!" << endl; return; } m_age = age; } //Set lover void SetLover(string lover) { m_lover = lover; } private: //The name is readable and writable string m_name; //Age read only int m_age; //Lovers only write string m_lover; }; int main() { Person p; p.SetName("Zhang San"); cout << "full name:" << p.GetName() << endl; cout << "Age:" << p.Getage() << endl; p.SetLover("Li Si"); p.SetAge(18); cout << "Age:" << p.Getage() << endl; return 0; }
(4) Case design
Case 1
Exercise case 1:
- Design Cube class Cube
- Find the area and volume of the cube
- The global function and member function are used to judge whether the two cubes are equal
The case code is as follows:
#include <iostream> using namespace std; //Cube design case //Create cube class class Cube { public: //Set length void SetL(int length) { m_l = length; } //Get long int GetL() { return m_l; } //Set width void SetW(int width) { m_w = width; } //Get width int GetW() { return m_w; } //Set high void SetH(int height) { m_h = height; } //Get high int GetH() { return m_h; } //Get cube area int calculateS() { return 2 * (m_l * m_w + m_l * m_h + m_w * m_h); } //Get cube volume int calculateV() { return m_l * m_w * m_h; } //Using member function to judge whether two cubes are equal bool isSameClass(Cube& c) { if (m_h == c.GetH() && m_l == c.GetL() && m_w == c.GetW()) { return true; } return false; } private: int m_l; int m_w; int m_h; }; //The global function and member function are used to compare whether the cubes are equal //Using global function to judge whether two cubes are equal bool isSame(Cube& c1, Cube& c2) { if (c1.GetH() == c2.GetH() && c1.GetL() == c2.GetL() && c1.GetW() == c2.GetW()) { return true; } return false; } int main() { Cube c1;//Instantiate cube object c1.SetL(10); c1.SetW(10); c1.SetH(10); cout << "c1 The area of the is:" << c1.calculateS() << endl;//600 cout << "c1 The volume of is:" << c1.calculateV() << endl;//1000 //Create a second cube Cube c2; c2.SetL(10); c2.SetW(10); c2.SetH(11); bool ret = isSame(c1, c2);//Using global function judgment if (ret) { cout << "c1 c2 Is equal" << endl; } else { cout << "c1 c2 Are not equal" << endl; } ret = c1.isSameClass(c2);//Using member function to judge if (ret) { cout << "Member function judgment: c1 c2 Is equal" << endl; } else { cout << "Member function judgment: c1 c2 Are not equal" << endl; } return 0; }
Case 2
Relationship between point and circle
- Design a circular class and a point class to calculate the relationship between points and circles
- There are three relationships between a circle and a dot
- The point is in the circle (distance from the point to the center < radius of the circle)
- The point is on the circle (distance from the point to the center = = radius of the circle)
- The point is outside the circle (distance from the point to the center > radius of the circle)
- The formula for the distance between two points is as follows:
The case code is as follows:
#include <iostream> using namespace std; //Design a point class class Point { private: int m_X; int m_Y; public: //Set x coordinate void SetX(int x) { m_X = x; } //Get x coordinate int GetX() { return m_X; } //Set y coordinate void SetY(int y) { m_Y = y; } //Get y coordinate int GetY() { return m_Y; } }; //Design a circle class class Circle { private: int m_R;//radius Point m_Center;//center of a circle public: //Set radius void SetR(int r) { m_R = r; } //Get radius int GetR() { return m_R; } //Set center point void SetCenter(Point center) { m_Center = center; } //Get center point Point GetCenter() { return m_Center; } }; //Judge the relationship between point and circle void isInCircle(Circle& c, Point& p) { //Calculate the square of the distance between two points int distance = (c.GetCenter().GetX() - p.GetX()) * (c.GetCenter().GetX() - p.GetX()) + (c.GetCenter().GetY() - p.GetY()) * (c.GetCenter().GetY() - p.GetY()); //Calculate the square of the radius int rdistance = c.GetR() * c.GetR(); //Judgment relationship if (distance == rdistance) { cout << "Point on circle" << endl; } else if (distance > rdistance) { cout << "The point is outside the circle" << endl; } else { cout << "Point in circle" << endl; } } int main() { //Create an instance of a circle Circle c; c.SetR(10); Point center; center.SetX(10); center.SetY(0); c.SetCenter(center); //Create an instance of a point Point p; p.SetX(10); p.SetY(111); //Judgment relationship isInCircle(c, p); return 0; }
The projects we write in our daily work are generally large, so we need to write them in separate files. In this way, it is convenient to read the code, and it can also solve the problem of too much source code in a file.
The structure of the whole project can be divided into the following figure:
The code in main.cpp is:
#include <iostream> #include "point.h" #include "circle.h" using namespace std; //Judge the relationship between point and circle void isInCircle(Circle& c, Point& p) { //Calculate the square of the distance between two points int distance = (c.GetCenter().GetX() - p.GetX()) * (c.GetCenter().GetX() - p.GetX()) + (c.GetCenter().GetY() - p.GetY()) * (c.GetCenter().GetY() - p.GetY()); //Calculate the square of the radius int rdistance = c.GetR() * c.GetR(); //Judgment relationship if (distance == rdistance) { cout << "Point on circle" << endl; } else if (distance > rdistance) { cout << "The point is outside the circle" << endl; } else { cout << "Point in circle" << endl; } } int main() { //Create an instance of a circle Circle c; c.SetR(10); Point center; center.SetX(10); center.SetY(0); c.SetCenter(center); //Create an instance of a point Point p; p.SetX(10); p.SetY(111); //Judgment relationship isInCircle(c, p); return 0; }
The code in point.h is:
#pragma once / / prevent duplicate header files #include <iostream> using namespace std; //Design a point class class Point { private: int m_X; int m_Y; public: //Set x coordinate void SetX(int x); //Get x coordinate int GetX(); //Set y coordinate void SetY(int y); //Get y coordinate int GetY(); };
The code in the point.cpp file is:
#include "point.h" //Set x coordinate void Point::SetX(int x) { m_X = x; } //Get x coordinate int Point::GetX() { return m_X; } //Set y coordinate void Point::SetY(int y) { m_Y = y; } //Get y coordinate int Point::GetY() { return m_Y; }
The code in circle.h is:
#pragma once #include <iostream> #include "point.h" using namespace std; //Design a circle class class Circle { private: int m_R;//radius Point m_Center;//center of a circle public: //Set radius void SetR(int r); //Get radius int GetR(); //Set center point void SetCenter(Point center); //Get center point Point GetCenter(); };
The code in circle.cpp is:
#include "circle.h" #include "point.h" void Circle::SetR(int r) { m_R = r; } //Get radius int Circle::GetR() { return m_R; } //Set center point void Circle::SetCenter(Point center) { m_Center = center; } //Get center point Point Circle::GetCenter() { return m_Center; }
✨✨✨
come on.