2021-01-28 The eighth day of clocking in and learning C++


When judging true or false, you can use the bool type

One, class and object

The three main characteristics of C++ object-oriented are: Encapsulation, inheritance, polymorphism

C++ believes that everything is an object, and objects have their attributes and behaviors

Objects with the same properties, we can abstractly call them classes

1. Package

(1) Significance of encapsulation:

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

  • Permission control of attributes and behaviors

(1) When designing a class, write attributes and behaviors together to express things

Syntax: `class class name {access authority: attribute/behavior};

Instantiation: Create an object from a class

The attributes and behaviors in the class are collectively referred to as members

Attributes are also called member attributes or member variables

Behavior is also called member function or member method

Example

#include<iostream>
using namespace std;

const double pi = 3.14;
//创建一个圆类,求其周长
class circle
{
    
    
public:  //访问权限
	int R;  //属性:一般就是变量
	double zhouchang()  //行为:一般就是函数
	{
    
    
		return 2 * pi*R;
	}
};
int main()
{
    
    
//通过圆类,创建具体的圆
	circle c1;
	c1.R = 5;
	cout << "圆的周长为:" << c1.zhouchang() << endl;

	system("pause");
	return 0;

}

Example design a student class, the attributes have name and student ID, you can assign values ​​to the name and student ID, and you can also display the student’s name and student ID

#include<iostream>
using namespace std;
#include<string>

//设计一个学生类,属性有姓名和学号
//可以给姓名和学号赋值,也可以显示学生的姓名和学号
class student
{
    
    
public://访问权限

	string name;  //属性
	string ID;

	void showstudent()   //行为
	{
    
    
		cout << "学生姓名为:" << name << endl;
		cout << "学生学号为:" << ID << endl;
	}

};

int main()
{
    
    
	student s1;
	cout << "请输入学生姓名:";
	cin >> s1.name;
	cout << "请输入学生学号:";
	cin >> s1.ID;

	s1.showstudent();
	
	system("pause");
	return 0;

}

(2) When designing a class, attributes and behaviors can be placed under different permissions and controlled

There are three types of access rights:

  1. public Public authority: members can access inside the class, and can also access outside the class
  2. Protected protection authority: members can access inside the class, not outside the class, and the son can access the protected content in the father
  3. Private private permissions: members can be accessed within the class, can not be accessed outside the class, sons can not access the private content of the father

(2) The difference between struct and class

The only difference between the two in C++ isThe default access permissions are different

  • struct default permissions are public
  • class default permissions are private
#include<iostream>
using namespace std;
#include<string>

//设计一个学生类,属性有姓名和学号
//可以给姓名和学号赋值,也可以显示学生的姓名和学号
class student1
{
    
    
	string name1;  //默认权限为私有
};
struct student2
{
    
    
	string name2;  //默认权限为公共
};

int main()
{
    
    
	
	
	system("pause");
	return 0;

}

(3) Set the member attribute to private

advantage:

  1. Set all member attributes to private, you can control the read and write permissions by yourself
  2. For write permissions, we can check the validity of the data
#include<iostream>
using namespace std;
#include<string>

class student
{
    
    

public:
	void ReadName()
	{
    
    
		name = "张三";
		cout << "姓名:" << name << endl;
	}

	void WriteAge(int a)
	{
    
    
		age = a;
	}

	void WriteScore(int b)
	{
    
    
		score = b;
	}
	void ReadScore()
	{
    
    
		cout << "分数:";
		cout << score << endl;
	}

private :
	string name;  //姓名  只读
	int age;     //年龄   只写
	int score;    //分数  可读可写
};

int main()
{
    
    
	student s1;
	s1.ReadName();
	s1.WriteAge(18);
	s1.WriteScore(450);
	s1.ReadScore();

	system("pause");
	return 0;

}

Practice case 1
Design a cube, find the area and volume of the cube, use the global function and the member function to judge whether the two cubes are equal

//设计立方体,求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等
#include<iostream>
using namespace std;

class cube
{
    
    
public:
	void setL(int L)//设置长
	{
    
    
		m_L = L;
	}
	int getL()//获取长
	{
    
    
		return m_L;
	}
	void setW(int W)//设置宽
	{
    
    
		m_W = W;
	}
	int getW()//获取宽
	{
    
    
		return m_W;
	}
	void setH(int H)//设置高
	{
    
    
		m_H = H;
	}
	int getH()//获取高
	{
    
    
		return m_H;
	}

	int getS()  //获取面积
	{
    
    
		return m_L * m_W * 2 + m_L * m_H * 2 + m_W * m_H * 2;
	}

	int getV()
	{
    
    
		return m_L * m_W*m_H;
	}

	bool issameC(cube c)  //只需要传入一个就可以,因为本身就有一组数,所以传入的和原来的进行比较即可
	{
    
    
		if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH())
			return true;
		return false;
	}

private:
	//属性一般设为私有权限
	int m_L;   //长
	int m_W;   //宽
	int m_H;	//高
};

bool issame(cube &c1, cube &c2)  //利用引用函数
{
    
    
	if (c1.getL() == c2.getL()&&c1.getW() == c2.getW()&&c1.getH() == c2.getH())
		return true;
	return false;
}

int main()
{
    
    
	cube c1;
	c1.setL(2);
	c1.setW(3);
	c1.setH(5);
	cout << "c1立方体的长为:" << c1.getL() << endl;
	cout << "c1立方体的宽为:" << c1.getW() << endl;
	cout << "c1立方体的高为:" << c1.getH() << endl;
	cout << "c1立方体的面积为:" << c1.getS() << endl;
	cout << "c1立方体的体积为:" << c1.getV() << endl;

	cube c2;
	c2.setL(2);
	c2.setW(3);
	c2.setH(4);

	bool flag1 = c1.issameC(c2);   //利用成员函数判断
	if (flag1 == true) 
		cout << "成员函数:两个立方体相同" << endl;
	else
		cout << "成员函数:两个立方体不相同" << endl;

	bool flag2 = issame(c1,c2);  //利用全局函数判断
	if (flag2 == true)
		cout << "全局函数:两个立方体相同" << endl;
	else
		cout << "全局函数:两个立方体不相同" << endl;

	system("pause");
	return 0;

}

Practice case 2
Determine the positional relationship between a point and a circle

#include<iostream>
using namespace std;

//设定一个点类
class point
{
    
    
public:
	void setX(int x)   //设置横坐标
	{
    
    
		p_X = x;
	}
	int getX()   //获取横坐标
	{
    
    
		return p_X;
	}
	void setY(int y)   //设置纵坐标
	{
    
    
		p_Y = y;
	}
	int getY()   //获取纵坐标
	{
    
    
		return p_Y ;
	}

private:
	int p_X;
	int p_Y;
};

//设定一个圆类
class circle
{
    
    
public:
	void setR(int r)
	{
    
    
		c_R = r;
	}
	int getR()
	{
    
    
		return c_R;
	}
	void setcenter(point center)
	{
    
    
		c_center=center;
	}
	point getcenter()
	{
    
    
		return c_center;
	}


private:
	int c_R;  //半径
	point c_center;  //圆心
};

void isInCircle(circle &c, point &p)
{
    
    
	int distenceP =
		(c.getcenter().getX() - p.getX())*(c.getcenter().getX() - p.getX())+
		(c.getcenter().getY() - p.getY())*(c.getcenter().getY() - p.getY());
	int distenceR = c.getR()*c.getR();

	if (distenceP == distenceR)
		cout << "点在圆上" << endl;
	else if(distenceP > distenceR)
		cout << "点在圆外" << endl;
	else
		cout << "点在圆内 " << endl;
}

int main()
{
    
    
	circle c;
	point center;
	point  p;
	c.setR(2);//圆半径
	center.setX(0);
	center.setY(0);
	c.setcenter(center);
	p.setX(0);
	p.setY(1);
	isInCircle(c, p);
	


	system("pause");
	return 0;

}

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/113308286