C++学习5

1 函数提高

1.1 函数默认参数

在这里插入图片描述

//函数默认参数
//如果我们自己传入数据,就用自己的数据,如果没有,name用默认值
//语法:返回值类型 函数名(形参 =默认值){}
int func(int a, int b = 10, int c = 30)
{
	return a + b + c;
}
//注意事项
//1、如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
//2、如果函数声明有默认参数,函数实现就不能有默认参数
//声明和实现只能有一个有默认参数
int func2(int a = 10, int b = 10);
int func2(int a, int b)
{
	return a + b;
}

3.2 函数占位参数

在这里插入图片描述

1.3 函数重载

1.3.1 函数重载概述

在这里插入图片描述

#include<iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

//函数重载的满足条件
//1、同一个作用域下
//2、函数名称相同
//3、函数参数类型不同,或者个数不同,或者顺序不同
void func()
{
	cout << "func的调用" << endl;
}
void func(int a)
{
	cout << "func(int a)的调用" << endl;
}
void func(double a, int b)
{
	cout << "func (double a, int b)的调用" << endl;
}
void func(int a,double b)
{
	cout << "func (int a,double b)的调用" << endl;
}
//注意事项
//函数的返回值不可以作为函数重载的条件
int main()
{
	//func();
	func(10, 3.14);
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

1.3.2 函数重载注意事项

在这里插入图片描述

#include<iostream>
using namespace std;
//函数重载的注意事项
//1、引用作为重载的条件
void func(int& a)// int &a=10;不合法
{
	cout << "func(int &a)调用" << endl;
}
void func(const int& a)//const int &a=10;合法
{
	cout << "func(const int &a)调用" << endl;
}
//2、函数重载碰到默认参数
void func2(int a, int b = 10)
{
	cout << "func2(int a,int b)的调用" << endl;
 }
void func2(int a)
{
	cout << "func2(int a)的调用" << endl;
}
int main()
{
	/*int a = 10;
	func(a);*/
	//func(10);.
	//func2(10);//当函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

2 类和对象

在这里插入图片描述

2.1 封装

2.1.1 封装的意义

在这里插入图片描述

#include<iostream>
using namespace std;
//圆周率
const double PI = 3.14;
//设计一个圆类,求圆的周长
//圆求周长的公式:2*PI*半径

//class 代表设计一个类,类后面紧跟着的就是类名称
class Circle
{
	//访问权限
	//公共权限
public:
	//属性
	int m_r;
	//行为
	//获取圆的周长
	double calcuateZC()
	{
		return 2 * PI * m_r;
	}
};
int main()
{
	//通过圆类 创建具体的圆(对象)
	//实例化 (通过一个类 创建一个对象的过程)
	Circle c1;
	//给圆对象 的属性进行赋值
	c1.m_r = 10;
	cout << "圆的周长:" << c1.calcuateZC() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

//类中的属性和行为 我们统一称为 成员
	// 属性 成员属性 成员变量
	// 行为 成员函数 成员方法

在这里插入图片描述

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

//访问权限
//三种
//公共权限 public     成员 类内可以访问 类外可以访问
//保护权限 protected  成员 类内可以访问 类外不可以访问   儿子可以访问父亲中的保护内容
//私有权限 private    成员 类内可以访问 类外不可以访问   儿子不可以访问父亲的私有内容
class Person
{
public:
	//公共权限
	string m_Name;//姓名
protected:
	//保护权限
	string m_Car;//汽车
private:
	//私有权限
	int m_Password;//银行卡密码
public:
	void func()
	{
		m_Name = "张三";
		m_Car = "t拖拉机";
		m_Password = 12346;
	}
};
int main()
{
	//实例化具体对象
	Person p1;
	p1.m_Name = "李四";
	//p1.m_Car="奔驰";//保护权限内容,在类外访问不到
	//p1.m_Password=123;//私有权限内容,类外访问不到


	system("pause");
	return 0;
}

2.1.2 struct和class区别

在这里插入图片描述

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

class C1
{
	int m_A;//默认权限 是私有
};
struct C2
{
	int m_A;//默认权限 是公共
};
int main()
{
	//struct 和class区别
	//struct 默认权限是 公共public
	//class  默认权限是私有private
	C1 c1;
	//c1.m_A=100;//在class里默认权限  私有,因此类外不可以访问
	C2 c2;
	c2.m_A = 100;//在struct默认的权限为公共,因此可以访问

	system("pause");
	return 0;
}

2.1.3 成员属性设置为私有

在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
//成员属性设置为私有
//1、可以自己控制读写权限
//2、对于写可以检测数据的有效性


//设计人类
class Person
{
public:
	//设置姓名
	void setName(string name)
	{
		m_Name = name;
	}
	//获取姓名
	string getName()
	{
		return m_Name;
	}

	//获取年龄
	int getAge()
	{
		m_Age = 0;//初始化为0岁
		return m_Age;

	}
     //设置年龄
	void setAge(int age)
	{
		if (age < 0 || age>150)
		{
			m_Age = 0;
			cout << "你这个老妖精!" << endl;
			return;
		}
		m_Age = age;
	 }
	//设置情人 只写
	void setLover(string lover)
	{
		m_Lover = lover;

	}
private:
	//姓名  可读可写
	string m_Name;
	//年龄   只读
	int m_Age;
	//情人   只写
	string m_Lover;
};
int main()
{
	Person p;
	p.setName("张三");
	cout << "张三:" << p.getName() << endl;
	p.setLover("c仓井");//只写
	p.setAge(18);
	cout << "年龄为:" << p.getAge() << endl;
    system("pause");
	return 0;
}

在这里插入图片描述
案例
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
//立方体类设计
//1、创建立方体类
//2、设计属性
//3、设计行为 获取立方体面积和体积
//4、分别利用全局函数和成员函数,判断两个立方体是否相等

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 calculateS()
	{
		return 2 * (m_L * m_H + m_L * m_W + m_H * m_W);
	}
	//获取立方体的体积
	int calculateV()
	{
		return m_L * m_W * m_H;
	}
	//利用成员函数判断两个立方体是否相等
	bool isSameByClass(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(10);
	c1.setH(10);
	c1.setW(10);

	cout << "c1的面积为:" << c1.calculateS() << endl;
	cout << "ci的体积为:" << c1.calculateV()<< endl;
	//创建第二个立方体
	Cube c2;
	c2.setL(10);
	c2.setH(10);
	c2.setW(10);
	//利用全局函数进行判断
	bool ret = isSame(c1, c2);
	if (ret)
	{
		cout << "c1和c2是相等的" << endl;
	}
	else
	{
		cout << "c1和c2是不相等的" << endl;
	}
	//利用成员函数判断
	ret = c1.isSameByClass(c2);
	if (ret)
	{
		cout << "成员函数:c1和c2相等的" << endl;
	}
	else
	{
		cout << "成员函数:c1和c2不相等的" << endl;
	}
    system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;
#include<string>
//点和圆关系案例
// 点类
class Point 
{
public:
	//设置x
	void setX(int x)
	{
		m_X = x;
	}
	//获取x
	int getX()
	{
		return m_X;
	}
	//设置y坐标
	void setY(int y)
	{
		m_Y = y;
	}
	//获取y坐标
	int getY()
	{
		return m_Y;
	}
private:
	int m_X;
	int m_Y;

};
//圆类
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;//半径
	//在类中可以让另一个类 作为本类中的成员
	Point m_Center;//圆心
};
//判断点和圆关系
void isInCircle(Circle& c, Point p)
{
	//计算两点之间距离 平方
	int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +
		(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
	//计算半径的平方
	int rDistance = c.getR() * c.getR();
	//判断关系
	if (distance == rDistance)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance > rDistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}
int main()
{
	//创建圆
	Circle c;
	c.setR(10);
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);
	//创建点
	Point p;
	p.setX(10);
	p.setY(10);

	//判断关系
	isInCircle(c, p);
    system("pause");
	return 0;
}

在这里插入图片描述
头文件:
在这里插入图片描述
源文件:
在这里插入图片描述

在这里插入代码片
```#include<iostream>
using namespace std;
#include<string>
#include"circle.h"
#include"point.h"
//点和圆关系案例

//判断点和圆关系
void isInCircle(Circle& c, Point p)
{
	//计算两点之间距离 平方
	int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +
		(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
	//计算半径的平方
	int rDistance = c.getR() * c.getR();
	//判断关系
	if (distance == rDistance)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance > rDistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}
int main()
{
	//创建圆
	Circle c;
	c.setR(10);
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);
	//创建点
	Point p;
	p.setX(10);
	p.setY(10);

	//判断关系
	isInCircle(c, p);
    system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值