C + + learning notes - class

preface

The three characteristics of C + + object-oriented are encapsulation, inheritance and polymorphism
The meaning of encapsulation is to take the attributes and behavior as a whole, express or even the food in, and control the attributes
For example, human attributes include not only inherent attributes such as age, height and weight, but also wife, son and parents. Other social attributes

1, Class call

The code is as follows (example):

 class Class name
 {
     //Access rights
     public;
     //attribute
     //behavior
 }

There are three kinds of access rights: public, protect and private;

**Public permission: * * it can be accessed within the class or outside the class;
Protection permission: it can be accessed within the class, but not outside the class;
Private permission: it can be accessed within the class, but not outside the class;
Among them, protection authority and private authority are different in inheritance;
Attributes and behaviors belong to members, and member functions are behaviors. For example, defining a cube, its side length is its attribute, and setting functions in the class to define the calculated area and volume belongs to behaviors.

2, The difference between class and structure

Difference between class and structure:

  • The default permission of struct is public
  • class default permission is private
    The advantages of setting member properties to private are:
  • Set all members as private, and you can control reading and writing by yourself (by setting set and get functions);
  • For write permission, we can check the validity of data (set conditions inside the set function);

3, Example exercise

Exercise example 1: designing a cube class

Function:
  1. Design cube class
  2. Find the area and volume of the cube
  3. The global function and member function are used to judge whether the two cubes are equal
#include<stdio.h>
#include<string>
#include <iostream>
#define True 1
using namespace std;
class cube
{
private://Property is best set to private
	int m_L, m_W,m_H;
public:
	//set/get length
	void setL(int l)
	{
		m_L = l;
	}
	int getL()
	{
		return m_L;
	}
	//set/get wide
	void setW(int w)
	{
		m_W = w;
	}
	int getW()
	{
		return m_W;
	}
	//set/get high
	void setH(int h)
	{
		m_H = h;
	}
	int getH()
	{
		return m_H;
	}
	//suface area of cube
	int CubeS()
	{
		return  2 * m_L * m_W +2 * m_L * m_H +2 * m_W * m_H;
	}
	//volume of cube
	int  CubeV()
	{
		return m_L * m_W * m_H;
	}
	void isEquality2(cube& A1)
	{
		if ((A1.getL(), A1.getW(), A1.getH())== (m_L, m_W, m_H))
		{
			cout << "two cube is equal!!!" << endl;
		}
		else
		{
			cout << "two cube is not equal!!!" << endl;
		}
	}
};

//Check whether two cuboids are equal
void isEquality1(cube &A1, cube &A2)
{
	if ((A1.getL(),A1.getW(),A1.getH())==(A2.getL(),A2.getW(),A2.getH()))
	{
		cout <<"two cube is equal!"<<endl;
	}
	else
	{
		cout <<"two cube is not equal!"<<endl;
	}
}

int main(void)
{
	int a = 0,b=0,c=0;//define the length of the cube
	cube cube1;
	cube cube2;
	while (True)
	{
		cout << "please input the length of the cube(length,wide,high>0): " << endl;
		cin >> a;
		cin >> b;
		cin >> c;

		if ((a > 0)&&(b>0)&&(c>0))
		{

			cube1.setL(a);
			cube1.setW(b);
			cube1.setH(c);
			cout<<"Surface area of the cube:"<<cube1.CubeS()<<endl;
			cout << "Volume  of the cube:" <<cube1.CubeV()<< endl;
			break;
		}
		else
		{
			cout << "Invalid input please re-enter!" << endl;
		}
	}

	while (True)
	{
		cout << "please input the length of the cube(length,wide,high>0): " << endl;
		cin >> a;
		cin >> b;
		cin >> c;

		if ((a > 0) && (b > 0) && (c > 0))
		{

			cube2.setL(a);
			cube2.setW(b);
			cube2.setH(c);
			cout << "Surface area of the cube:" << cube1.CubeS() << endl;
			cout << "Volume  of the cube:" << cube1.CubeV() << endl;
			break;
		}
		else
		{
			cout << "Invalid input please re-enter!" << endl;
		}
	}
	//check two cube
	isEquality1(cube1, cube2);
	cube1.isEquality2(cube2);
	return 0;
}

Result diagram:

Note: the difference between the global function and the member function is that the global function needs to input two formal parameters, and the member function only needs one.

Exercise example 2: relationship between point and circle

Function: design a circular class and a point class to calculate the relationship between points and circles
Analysis: the point class has the abscissa and ordinate attributes of points
A circle has center coordinates and radius attributes

//Gets the relationship between points and circles
#include<iostream>

using namespace std;

class Point
{
public:
	void set_x(int x)
	{
		m_x = x;
	}
	int get_x()
	{
		return m_x;
	}
	void set_y(int y)
	{
		m_y = y;
	}
	int get_y()
	{
		return m_y;
	}
private:
	int m_x;
	int m_y;
};

class Circle
{
public:
	void set_r(int r)
	{
		m_r = r;
	}
	int get_r()
	{
		return m_r;
	}
	void set_center(Point c)
	{
		center = c;
	}
	Point get_center()
	{
		return center;
	}


private:
	int m_r;
	Point center;
};

void circle_point(Point& P,Circle& C)
{
	int Distance = (P.get_x() - C.get_center().get_x()) * (P.get_x() - C.get_center().get_x()) +
		(P.get_y() - C.get_center().get_y()) * (P.get_y() - C.get_center().get_y());
	Distance = sqrt(Distance);
	if (Distance > C.get_r())
	{
		cout << "The point is outside the circle." << endl;
	}
	else if (Distance == C.get_r())
	{
		cout << "Point on the circle." << endl;
	}
	else
	{
		cout << "The point is in the circle." << endl;
	}
}
int main(void)
{
	Point  P,C_P;
	Circle  C;
	int C_x, C_y, C_r,P_x,P_y;
	cout << "Please enter the abscissa of the center of the circle X: ";
	cin >> C_x;
	cout << "Please enter the center ordinate Y: ";
	cin >> C_y;
	cout << "Please enter the circle radius:";
	cin >> C_r;
	cout << "Please enter abscissa X: ";
	cin >> P_x;
	cout << "Please enter the point ordinate Y: ";
	cin >> P_y;
	C_P.set_x(C_x);
	C_P.set_y(C_y);
	C.set_center(C_P);
	C.set_r(C_r);
	P.set_x(P_x);
	P.set_y(P_y);
	circle_point(P,C);
	return 0;
}

Result diagram:

Note: classes can be nested. However, it is easy to cause confusion in the process of use.

summary

Classes are very similar to structs. Many times they can be used in general, but it seems that they have write differences:

  1. Structure can create a linked list, but I don't see class class doing so now
  2. Class class can define behavior functions, but there seems to be no practice of building functions inside the structure
  3. class can build read-only, but struct can't

Keywords: C++

Added by rajatr on Mon, 28 Feb 2022 14:40:53 +0200