练习代码如下:
#include <iostream>
using namespace std;
int func01(int a, int b, int c)
{
return a + b + c;
}
int func02(int a, int b = 20, int c = 30)
{
return a + b + c;
}
int main()
{
cout << func01(10, 20, 30) << endl;
cout << func02(10) << endl;
cout << func02(10, 30) << endl;
cout << func02(10, 30, 40) << endl;
//结论:如果我们子传入参数,就用自己的数据,如果没有,那么就用默认值
/*
* 注意事项:
* 1、如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须要有默认值
* int func(int a,int b=10,int c){}是错误的
* 必须是int func(int a,int b=10,int c=10){}才是正确的
*
* 2、如果函数的声明有了默认参数,那么这个函数的实现就不能有默认参数
* 即声明和实现只能有一个有默认参数
* int func(int a =10,int b=10);//声明
* int func(int a=10,int b=10)//错误的,会让编译器产生二义性
* int func(int a,int b)//正确
* {
* return a+b;
* }
*/
return 0;
}
#include <iostream>
using namespace std;
//目前阶段的占位参数用不到,后面碰到了做详细介绍
void func(int a, int)//int 是占位
{
cout << "this is func" << endl;
}
void func01(int a, int = 10)
{
cout << "this is func01" << endl;
}
int main()
{
//func(10);//error
func(10, 20);//必须有参数
func01(10);//error
return 0;
}
作用:函数名可以相同,提高复用性
函数重载满足条件:
注意:函数的返回值不可以作为函数重载的条件
练习代码如下:
#include <iostream>
using namespace std;
//函数重载:提高函数复用性
void func()
{
cout << "func的调用" << endl;
}
void func(int a)
{
cout << "func(int a)的调用" << endl;
}
void func(double a)
{
cout << "func(double a)的调用" << endl;
}
void func(int a, char b)
{
cout << "func(int a,char b)的调用" << endl;
}
void func(char b, int a)
{
cout << "func(char b, int a)的调用" << endl;
}
int main()
{
func();
func(10);
func(3.14);
func(10, 'a');
func('a', 10);
return 0;
}
函数重载的注意事项:
案例代码1:
#include <iostream>
using namespace std;
//引用作为重载条件
void func(int& a)//int& a = 10;//不合法
{
cout << "func的调用" << endl;
}
void func(const int& a)//const int& a=10;合法代码
{
cout << "const func的调用" << endl;
}
int main()
{
//int a = 10;
//func(a);//调用第一个函数
func(10);//调用第二个函数
return 0;
}
结论:
案例代码2:
#include <iostream>
using namespace std;
//函数重载碰到函数默认参数
void func(int a, int b = 10)
{
cout << "func(int a)" << endl;
}
void func(int a)
{
cout << "func(int a)" << endl;
}
int main()
{
func(10);//error 两个函数都可以调用,出现了二义性,需要尽量避免这种情况
func(10, 20);//正确,不会出现二义性
//建议:函数重载和默认参数最好不要同时出现,很容易导致二义性
return 0;
}
例如:
封装的意义:
案例代码一:计算圆的周长
#include <iostream>
using namespace std;
const double PI = 3.14;//圆周率
//设计一个圆类,求圆的周长:2*PI*r
//封装的意义:将属性和行为封装成一个整体
class Circle
{
//属性
public:
int m_r;//半径
//行为:获取圆的周长
double calculateZC()
{
return 2 * PI * m_r;
}
};
int main()
{
//通过类实例化出一个对象
Circle c1;
//给圆对象的属性进行赋值
c1.m_r = 10;
cout << "圆的周长为:" << c1.calculateZC() << endl;
return 0;
}
案例代码二:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
string name;//姓名
string id;//学号
void ShowInfo()
{
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
}
};
int main()
{
Student s1;
s1.name = "张三";
s1.id = "1831050072";
s1.ShowInfo();
return 0;
}
提高程序健壮性的案例二:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
string m_name;//姓名
int m_id;//学号
//给姓名赋值
void SetName(string name)
{
m_name = name;
}
//给学号赋值
void SetId(int id)
{
m_id = id;
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "学号:" << m_id << endl;
}
};
int main()
{
Student s1;
s1.SetName("张三");
s1.SetId(1000);
s1.ShowInfo();
return 0;
}
类在设计时,可以把属性和行为放在不同的权限下,加以控制。
访问权限有三种
public
protected
private
练习代码如下:
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string m_name;
protected:
string m_car;
private:
int m_password;
public:
void func()
{
//三个成员属性在类内都可以访问
m_name = "张三";
m_car = "宝马";
m_password = 123456;
}
};
int main()
{
Person p1;
p1.m_name = "李四";
//p1.m_car = "奔驰";//error 保护权限在类外不可以访问
//p1.m_password = 123;//error 私有权限在类外不可以访问
return 0;
}
struct和class的区别
在C++中,struct和class都可以用来设计一个类,唯一的区别就在于默认的访问权限不同
区别
在C++中成员属性设置为私有有以下两个优点
#include <iostream>
#include <string>
using namespace std;
//成员属性设置为私有
class Person
{
public:
//设置姓名
void SetName(string name)
{
m_name = name;
}
//获取姓名
string GetName()
{
return m_name;
}
//获取年龄 可读可写,如果想修改(年龄范围必须是0-150)
int Getage()
{
//m_age = 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;
cout << "年龄:" << p.Getage() << endl;
p.SetLover("李四");
p.SetAge(18);
cout << "年龄:" << p.Getage() << endl;
return 0;
}
练习案例1:
案例代码如下:
#include <iostream>
using namespace std;
//立方体设计案例
//创建立方体的类
class Cube
{
public:
//设置长
void SetL(int length)
{
m_l = length;
}
//获取长
int GetL()
{
return m_l;
}
//设置宽
void SetW(int width)
{
m_w = width;
}
//获取宽
int GetW()
{
return m_w;
}
//设置高
void SetH(int height)
{
m_h = height;
}
//获取高
int GetH()
{
return m_h;
}
//获取立方体面积
int calculateS()
{
return 2 * (m_l * m_w + m_l * m_h + m_w * m_h);
}
//获取立方体体积
int calculateV()
{
return m_l * m_w * m_h;
}
//利用成员函数判断两个立方体是否相等
bool isSameClass(Cube& c)
{
if (m_h == c.GetH() && m_l == c.GetL() && m_w == c.GetW())
{
return true;
}
return false;
}
private:
int m_l;
int m_w;
int m_h;
};
//分别利用全局函数和成员函数比较立方体是否相等
//利用全局函数判断两个立方体是否相等
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()
{
Cube c1;//实例化立方体对象
c1.SetL(10);
c1.SetW(10);
c1.SetH(10);
cout << "c1的面积为:" << c1.calculateS() << endl;//600
cout << "c1的体积为:" << c1.calculateV() << endl;//1000
//创建第二个立方体
Cube c2;
c2.SetL(10);
c2.SetW(10);
c2.SetH(11);
bool ret = isSame(c1, c2);//利用全局函数判断
if (ret)
{
cout << "c1 c2 是相等的" << endl;
}
else
{
cout << "c1 c2是不相等的" << endl;
}
ret = c1.isSameClass(c2);//利用成员函数判断
if (ret)
{
cout << "成员函数判断:c1 c2 是相等的" << endl;
}
else
{
cout << "成员函数判断:c1 c2是不相等的" << endl;
}
return 0;
}
点和圆的关系
案例代码如下:
#include <iostream>
using namespace std;
//设计一个点类
class Point
{
private:
int m_X;
int m_Y;
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;
}
};
//设计一个圆类
class Circle
{
private:
int m_R;//半径
Point m_Center;//圆心
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;
}
};
//判断点和圆的关系
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(111);
//判断关系
isInCircle(c, p);
return 0;
}
我们在平时工作中写的项目一般都比较大,所以需要分文件编写,这样的话阅读代码比较方便,也可以解决一个文件中源代码量过大的情况。
整个项目的结构可以分成下图所示:
main.cpp中的代码为:
#include <iostream>
#include "point.h"
#include "circle.h"
using namespace std;
//判断点和圆的关系
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(111);
//判断关系
isInCircle(c, p);
return 0;
}
point.h中的代码为:
#pragma once//防止头文件重复包含
#include <iostream>
using namespace std;
//设计一个点类
class Point
{
private:
int m_X;
int m_Y;
public:
//设置x坐标
void SetX(int x);
//获取x坐标
int GetX();
//设置y坐标
void SetY(int y);
//获取y坐标
int GetY();
};
point.cpp文件中的代码为:
#include "point.h"
//设置x坐标
void Point::SetX(int x)
{
m_X = x;
}
//获取x坐标
int Point::GetX()
{
return m_X;
}
//设置y坐标
void Point::SetY(int y)
{
m_Y = y;
}
//获取y坐标
int Point::GetY()
{
return m_Y;
}
circle.h中的代码为:
#pragma once
#include <iostream>
#include "point.h"
using namespace std;
//设计一个圆类
class Circle
{
private:
int m_R;//半径
Point m_Center;//圆心
public:
//设置半径
void SetR(int r);
//获取半径
int GetR();
//设置圆心
void SetCenter(Point center);
//获取圆心
Point GetCenter();
};
circle.cpp中的代码为:
#include "circle.h"
#include "point.h"
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;
}
加油!!!
文章浏览阅读2.3k次。背景写了一个调用C++ DLL库的C#测试程序,在别人的电脑出现如下错误:排查确认自定义的DLL和需要引用的DLL在当前运行exe的同一目录。确认是否按照VC运行库vc_redist.x64自己安装确定运行库使用的环境 MT/MD/MT是 "multithread, static version ” 意思是多线程静态的版本/MD是 "multithread- and DLL-specific version” ,意思是多线程DLL版本类似linux的lib/so,确保_vs2017 引用dll错误
文章浏览阅读1.8w次,点赞28次,收藏141次。移动web开发指的是: 需要适配移动设备的网页开发移动web开发与pc端web开发没有本质的区别,使用的还是HTML/CSS/JavaScript的技术移动端与pc端web开发的区别:1、浏览器不同移动端的浏览器与pc端不同谷歌浏览器 苹果浏览器、 UC浏览器 QQ浏览器 百度手机浏览器 360安全浏览器 搜狗浏览器 猎豹浏览器等国内的手机浏览器都是根据webkit内核修..._移动开发所需要的知识
文章浏览阅读3.4k次。以前都是用Chrome来进行抓包,但是response可能是空的。今天用火狐的试了一下发现竟然有数据的。(详细询问下开发人员也可以)以下是相同bug的对比:ChromeFirefox..._firefox开发者工具能跟谷歌的一样吗
文章浏览阅读9.1k次,点赞6次,收藏29次。最近我的VMware群里遇到好几个下载了Win10 Ghost镜像但是放在VMware中没法安装的小伙伴,我决定出个博客教程帮助一下各位。前置准备VMware Workstation软件一个,能够给VMware引导的Windows PE 镜像一个,没法直接引导安装的Ghost的iso镜像一个1、新建虚拟机一定要选择自定义(高级),因为Win10一般都是UEFI引导,但是Ghost的Win10一般是BIOS引导,选错第一步就直接凉凉兼容性默认选择无法引导的Ghost镜像(由于镜_ghost文件在vm不能引导
文章浏览阅读897次。 关闭窗口的时候不弹出提示直接关闭javascript:opener=null;window.close(); _c++ window关机不提示
文章浏览阅读1.5k次。提升开发效率,降低维护成本一直是开发团队永恒不变的宗旨。近一年来国内的技术圈子中越来越多的开始提及Rx,经过一段时间的学习和探索之后我也深深的感受到了RxJava的魅力。它能帮助我们简化代码逻辑,提升代码可读性。这对于开发效率的提升、后期维护成本的降低帮助都是巨大的。个人预测RxJava一定是2016年的一个大趋势,所以也有打算将它引入到公司现有的项目中来,写这一系列的文章主要也是为了团队内部做技术分享。_rxjava是哪个公司开发的
文章浏览阅读104次。有时候连得上数据库,有时候又连不上.可能是数据库上当前的连接数目已经超过了它能够处理的最大值.select count(*) from v$process --当前的连接数select value from v$parameter where name = 'processes' --数据库允许的最大连接数修改最大连接数:alter system set processes = 300 s..._sys.utl_http tns:no listener
文章浏览阅读77次。C++提供的编译预处理功能主要有以下三种: (一) 宏定义 (二) 文件包含 (三) 条件编译在C++中,我们一般用const定义符号常量。很显然,用const定义常量比用define定义常量更好。 在使用宏定义时应注意的是: (a) 在书写#define 命令时,注意<宏名>和<字符串>之间用空格分开,而不是用等号连接。 (b) 使用#define定..._将宏定义改为2
文章浏览阅读1.2k次,点赞2次,收藏5次。• 理解霍夫变换的概念• 学习如何在一张图片中检测直线• 学习函数:cv2.HoughLines(),cv2.HoughLinesP()霍夫变换在检测各种形状的的技术中非常流行,如果你要检测的形状可以用数学表达式写出,你就可以是使用霍夫变换检测它。即使要检测的形状存在一点破坏或者扭曲也可以使用。我们下面就看看如何使用霍夫变换检测直线。一条直线可以用数学表达式y = mx + c 或者 = ..._python opencv houghlinesp
文章浏览阅读312次。1. 新建工程文件取名如下:芯片型号选择如下:2. 新建半加器原理图文件分别调入元件,设计原理图如下:设计原理图如下:_fpga用元件例化语句实现1位全加器
文章浏览阅读363次。http://acm.hdu.edu.cn/showproblem.php?pid=1242题意: 天使被困在城堡里 她的朋友要去救她 路上可能会碰到守卫 碰到守卫杀掉守卫消耗1个时间 只能上下左右走 每走一步消耗时间1 求最短时间"." 表示道路;“x”表示守卫;“a”表示天使;“r”表示朋友;“#”表_hsoj1242 dfs
文章浏览阅读1k次,点赞4次,收藏4次。下面来说一下本次打awd遇到的坑以及收获开头来先吐槽一番作为一个逆向菜鸡,第一次打awd攻防竞赛也是懵的一批,本次awd是本校与省内几个高校共同办的一个小型的内部线下竞赛。毕竟这次没有高年级的大佬参加,所以虽然打的懵,但是还不至于被打的人都傻了。至少后面几轮解出漏洞来也是疯狂的对别的队进行疯狂输出,无奈脚本没打通,只能手动提交flag,最后交的手都抽筋了也不过打了近1000分最左右,真是太菜..._逆向 awd