C + + classes and objects -- encapsulation

https://www.bilibili.com/video/BV1et411b73Z?p=99https://www.bilibili.com/video/BV1et411b73Z?p=99

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 used as objects. The attributes include name, age, height, weight Behaviors include walking, running, jumping, eating, singing

The car can also be used as an object. The attributes include tires, steering wheel, lights The behaviors include carrying people, playing music and turning on the air conditioner

Objects with the same properties can be abstracted into classes. People belong to humans and cars belong to cars

encapsulation

1. Significance of packaging

Encapsulation is one of the three characteristics of C + + object-oriented

Meaning of encapsulation:

·Take attributes and behaviors as a whole to express things in life

·Control attributes and behaviors with permissions

Packaging significance I:

When designing classes, attributes and behaviors are written together to represent things

Syntax: class name {access rights: attribute / behavior};

Example 1: design a circle class to find the circumference of the circle

#include <iostream>

using namespace std;

//PI
const double PI = 3.14;

//Design a circle class to find the circumference of the circle
//Formula: 2 * PI * radius

//Class represents the design of a class, followed by the name of the class
class Circle
{
	//Access rights
	//Public authority
public:

	//attribute
	//radius
	int m_r;


	//behavior
	//Gets the circumference of the circle
	double calculateZC()
	{
		return 2 * PI * m_r;
	}

};

int main()
{
	//Create a specific circle (object) through the circle class
	//Instantiation (the process of creating 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;
}
#include <iostream>
#include <string>


using namespace std;

class Student
{
public://Public authority

	//The properties and behaviors in a class are collectively called members
	//Properties: member properties member variables
	//Behavior member function member method

	//attribute
	string m_Name;//full name

	int m_Id;//Student number

	//behavior
	//Display name and student number
	void showStudent()
	{
		cout << "full name: " << m_Name << "  Student No.: " << m_Id << endl;
	}
	//Assign a name
	void setName(string name)
	{
		m_Name = name;
	}

};

int main()
{

	Student s1;

	s1.m_Name = "Zhang San";
	s1.m_Id = 1;

	s1.showStudent();

	Student s2;

	s2.setName("Li Si");
		
	s2.m_Id = 2;

	s2.showStudent();




}

2. Packaging significance II:

  1. Public , public permission , can be accessed inside the class and outside the class
  2. Protected: protected permission can be accessed within the class, but not outside the class
  3. Private , private permission , can be accessed inside the class, but not outside the class

The difference between struct # and # class:

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

·The default permission of struct is public

·The default permission of class is private

#include <iostream>

using namespace std;

class C1
{
	int m_A;//The default is private permissions
};

struct C2
{
	int m_A;//The default is shared permissions
};

int main()
{
	C1 c1;
	//c1.m_A = 10; Error, access is private

	C2 c2;
	c2.m_A = 10;//Correct, access rights are shared

}

3. Privatization of member attributes

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>
#include <string>
using namespace std;

//Set member properties to private
//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

class Person
{
public:

	//Set name - write name
	void setName(string name)
	{
		m_Name = name;
	}

	//Get name - read name
	string getName()
	{
		return m_Name;
	}

	//Get age
	void setAge(int age)
	{
		
		if (age < 0 || age>150)
		{
			m_Age = 0;
			cout << "Are you kidding me?" << endl;
			return;
		}
		m_Age = age;
	}

	int getAge()
	{
		m_Age = 0;
		return m_Age;
	}

	//Set valentine to write only
	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 p1;
	p1.setName("zhangsan");

	cout << p1.getName() << endl;

	p1.setAge(180);
	cout << "age: " << p1.getAge() << endl;

	p1.setLover("cangjing");

	//cout << "Lover: " << p1. setLover << endl; Not accessible


	return 0;
}

Exercise case 1

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

#include <iostream>

using namespace std;

//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


class Cube
{
	//behavior
	// Set get length, width and height
	//Get cube area
	//Get cube volume

public:
	//Set length
	void setL(int l)
	{
		m_L = l;
	 }
	//Get long
	int getL()
	{
		return m_L;
	}
	// Set width
	void setW(int w)
	{
		m_W = w;
	}
	
	// Get width
	int getW()
	{
		return m_W;
	}
	
	
	// Set high
	void setH(int h)
	{
		m_H = h;
	}
	//Get high
	int getH()
	{
		return m_H;
	}
	
	//Get area
	int calculateS()
	{
		return 2 * m_L * m_H + 2 * m_H * m_W + 2 * m_L * m_W;
	}
	
	//Get volume
	int calculateV()
	{
		return m_H * m_L * m_W;
	}

	//Using member function to judge whether two cubes are equal
	bool isSameByClass(Cube &c)
	{
		if (m_H == c.getH() && m_L == c.getL() && m_W == c.getW())
		{
			return true;
		}
		else return false;
	}

	//attribute
private:
	int m_L;//chang
	int m_W;//kuan
	int m_H;//gao

};

//Using global function judgment
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()
{
	//Create cube object
	Cube c1;
	c1.setL(10);
	c1.setH(10);
	c1.setW(10);

	cout << "c1's = " << c1.calculateS() << endl;

	cout << "c1'v = " << c1.calculateV() << endl;

	Cube c2;
	c2.setL(10);
	c2.setH(10);
	c2.setW(10);

	bool ret = isSame(c1, c2);
	if (ret)
	{
		cout << "c1 and c2 are same" << endl;
	}
	else
	{
		cout << "c1 and c2 are NOT same" << endl;
	}

	ret = c1.isSameByClass(c2);
	if (ret)
	{
		cout << "c1 and c2 are same" << endl;
	}
	else
	{
		cout << "c1 and c2 are NOT same" << endl;
	}

	return 0;
}

Exercise case 2:

Positional relationship between point and circle

Design a circle class and a point class

Calculate the relationship between point and circle

We simplify the code by squaring two distances

Sub document preparation:

Add point H header file and circle H header file

point.h: #pragma once to avoid the same header file being included twice

#pragma once
#include <iostream>

using namespace std;

//Point class
class Point
{
public:
	//Set X
	void setX(int x);

	//Get X
	int getX();

	//Set Y
	void setY(int y);

	//Get Y
	int getY();
private:
	int m_X;
	int m_Y;
};

point.cpp:

#include "point.h"

//Point class 

void Point::setX(int x)
{
	m_X = x;
}
int Point::getX()
{
	return m_X;
}
void Point::setY(int y)
{
	m_Y = y;
}
int Point::getY()
{
	return m_Y;
}

circle.h:

#pragma once
#include <iostream>
#include "point.h"

using namespace std;

//Circular class
class Circle
{
public:
	void setR(int r);


	int getR();


	void setCenter(Point center);


	Point getCenter();

private:
	int m_R;//radius

	Point m_Center;//center of a circle
};

circle.cpp:

#include "circle.h"
#include "point.h"
//Circular class

void Circle::setR(int r)
{
	m_R = r;
}

int Circle::getR()
{
	return m_R;
}

void Circle::setCenter(Point center)
{
	m_Center = center;
}

Point Circle::getCenter()
{
	return m_Center;
}

main:

#include <iostream>
#include "circle.h"
#include "point.h"
using namespace std;


//Design a Circle class and a Point class to calculate the positional relationship between points and circles

//Comparison of calculated point to center and radius



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 circle
	Circle c;
	c.setR(10);
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);
	//Create point
	Point p;
	p.setX(10);
	p.setY(10);



	//Judgment relationship
	isInCircle(c, p);

	return 0;
}

The following contents are prepared in sub documents:

Point class
//class Point
//{
//public:
//	//
//	void setX(int x)
//	{
//		m_X = x;
//	}
//	int getX()
//	{
//		return m_X;
//	}
//	void setY(int y)
//	{
//		m_Y = y;
//	}
//	int getY()
//	{
//		return m_Y;
//	}
//private:
//	int m_X;
//	int m_Y;
//};

//Circular class
//class Circle
//{
//public:
//	void setR(int r)
//	{
//		m_R = r;
//	}
//
//	int getR()
//	{
//		return m_R;
//	}
//
//	void setCenter(Point center)
//	{
//		m_Center = center;
//	}
//
//	Point getCenter()
//	{
//		return m_Center;
//	}
//private:
//	int m_R;// radius
//
//	Point m_Center;// center of a circle
//};

    

Keywords: C++ Back-end

Added by potato on Fri, 21 Jan 2022 21:45:11 +0200