Core programming of C + + Notes

Keywords: C++ html

Classes and objects

  • The three characteristics of C + + object-oriented are encapsulation, inheritance and polymorphism
  • C + + believes that everything is an object, and there are attributes and behaviors on the object

For example:

  • People can be objects. Their attributes include name, age, height and weight... Their behaviors include walking, running, jumping, eating, singing
  • Cars can also be used as objects. Their properties include tires, steering wheel, lights... Behaviors include carrying people, playing music, playing air conditioning
  • Concrete objects of the same nature can be abstracted into classes. People belong to humans and cars belong to cars.

1, Encapsulation

significance:

  • Take attributes and behaviors as a whole to express things in life
  • Control attributes and behaviors with permissions

1. Significance I:

  • When designing classes, attributes and behaviors are written together to represent things
  • Syntax: class name (access rights: attribute / behavior);
  • explain

  • Properties: member properties     Member variable
  • Behaviors: member functions     Member method

    Example: design a circle class to calculate the circumference of a circle

#include<iostream>
using namespace std;


//Formula: C = π * radius * 2
const double PI = 3.14;

//Class stands for designing a class, followed by the name of the class
class yuan
{
	//Access rights; Public authority
public:

	//Attribute -- radius
	int m_r;
	//Behavior -- get the circumference of a circle (behavior is usually a function)
	double calculateZC()
	{
		return 2 * PI * m_r;
	}
};

int main()
{
	//Create a concrete circle (object) through the circle class
	yuan c;
	//Assign values to the attributes of the circle object
	c.m_r = 10;
	//C=2*PI*10=62,8;
	cout << "The circumference of the circle is:" << c.calculateZC() << endl;
	system("pause");
	return 0;
}

    Example 2:

Design a student class, attribute: 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>
using namespace std;


//Formula: C = π * radius * 2
const double PI = 3.14;

//Class stands for designing a class, followed by the name of the class
class student
{
public://Public authority
	//attribute
	string name;//full name
	int num;//Student number
	//behavior
	//Display student information
	void showstudent()
	{
		cout << "full name:" << name <<endl<< "Student No.:" << num << endl;
	}
	//Enter student name
	void setname(string n)
	{
		name = n;
	}
	//Enter student ID
	void setnum(int m)
	{
		num = m;
	}

};

int main()
{
	student A;//Create a student
	A.name = "Zhang San";//Assign a name
	A.num = 1;//Assign value to student number
	A.showstudent();//Display student information

	student B;
	B.setname("Li Si");//Assign a name
	B.setnum(2);//Assign value to student number
	B.showstudent();//Display student information
	system("pause");
	return 0;
}

  2. Significance II

  • When designing classes, attributes and behaviors can be controlled under different permissions
  • There are three types of access rights
  1. public           Public authority       member     Accessible within class   It can also be accessed outside the class
  2. protected       Protection Authority      member     Accessible within class   Not accessible outside class       Child can access protected content in parent
  3. private           Private rights     member     Accessible within class   Not accessible outside class       Children cannot access private content in the parent    
#include<iostream>
using namespace std;

class person
{

public://Public authority
	string p_name;//name
protected://Protection Authority
	string p_car;//automobile
private://Private rights
	int p_password;//Bank card password
public:
	void fun()
	{
		p_name="Zhang San";
		p_car = "Tractor";
		p_password = 123456;

	}
protected:
	void fun1()
	{
		p_name = "Chen Wu";
		p_car = "truck";
		p_password = 234567;

	}
};

int main()
{
	person A;
	A.p_name = "Li Si";
	//A.p_car = "Benz"// The protection permission cannot be accessed outside the class
	//A.p_password = 234567;// Private permissions cannot be accessed outside the class
	A.fun();
	//A.fun1();// Functions that protect permissions are not accessible
	system("pause");
	return 0;
}

2, The difference between struct and class

In C + +, the only difference between struct and class is that the default access permissions are different

difference:

  • The default permission of struct is public
  • class the default permission is private
  • #include<iostream>
    using namespace std;
    
    class person
    {
    	string name;
    };
    
    struct student
    {
    	int num;
    };
    
    int main()
    {
    	person A;
    	student B;
    	//A.name = "Zhang San"// An error is reported. Class defaults to private permission and cannot be accessed outside the class
    	B.num = 1;
    	system("pause");
    	return 0;
    }

3, Set the member property to private  

The essence is to create an excuse to connect private and protected permissions outside and inside the class

  • Advantage 1: set all member properties to private, and you can control the read and write permissions yourself
  • Advantage 2: for write permission, we can detect the validity of data
#include<iostream>
using namespace std;
#include<string>

class person
{
public:    //Interface: connect protection and privacy permissions outside the class and inside the class
	//Write name
	void setname(string name)
	{
		m_name = name;
	}
	//Read name
	string getname()
	{
		return m_name;
	}
	//Reading age
	int getnum()
	{
		m_num = 0;
		return m_num;
	}
	//Read lover
	string setlover(string lover)
	{
		m_lover = lover;
	}
private:
	string m_name;//The name is readable and writable
	int m_num;//Age read only
	string m_lover;//Lovers only write
};

int main()
{
	person A;
	A.setname("Zhang San");//The name of A was successfully written
	cout << "A`s name:" << A.getname() << endl;//Read A's name
	cout << "A`s  num:" << A.getnum() << endl;//Read the age of A
	A.setlover("Allen");//Write A's lover
	system("pause");
	return 0;
}

Case: Design cube class

  • Design cube class
  • Find the area and volume of the cube
  • The global function and member function are used to judge whether the two cubes are equal

Posted by laffin on Wed, 06 Oct 2021 08:47:09 -0700