C++ 基础入门
笔记视频资源来自公众号:黑马程序员…
发博是为了记录学习…无盗窃他人课程之意
一.数据类型:
数据类型存在的意义:合理的分配内存空间…
1.1 整型:
1.2 sizeof 关键字:
统计数据类型 所占内存大小 (数据类型 / 变量);
#include <iostream>
using namespace std;
int main()
{
cout << "short数据类型大小:"<< sizeof(short)<< endl;
cout << "int数据类型大小:"<< sizeof(int)<< endl;
cout << "long数据类型大小:"<< sizeof(long)<< endl;
cout << "long long数据类型大小:"<< sizeof(long long)<< endl;
system("pause");
return 0;
}
1.3 实型(浮点型):
float 、double
1.3.1 float 初始化的时候:
float f1 =3.14f;//初始化的时候加个“f”...不加的时候,系统默认的是double...
1.3.2 代码:
#include <iostream>
using namespace std;
//1.单精度float;
//2.双精度double;
//一般的小数 只输出6位有效数字...
int main()
{
float f1 =3.1415926f;//初始化的时候加个“f”...不加的时候,系统默认的是double...
double d1 =3.1415926;
cout <<"f1="<< f1<< endl;
cout <<"d1="<< d1<< endl;
cout <<"float占内存大小"<< sizeof(float)<< endl;
cout <<"double占内存大小"<< sizeof(double)<< endl;
system("pause");
return 0;
}
1.4 用户可操作的空间:
1.4.1 可操作区域:
栈(局部变量) + 堆 + data、bss段(全局变量)…
1.4.2 栈回收:
1.5 C++对C的函数的拓展(inline 内联函数):
inline内联函数:为了解决C++中 多次压栈出栈的问题…(编译阶段…可以语法检测)
1.5.1 普通的函数调用(编译阶段):
缺点:无法解决1000次大的开销…
1.5.2 #define函数(预处理阶段):
缺点:无法解决语法处理…
1.5.3 inline 内联函数:
1.5.3.1 inline 内联函数的好处:
inline内联函数:
1.和#define函数一样 的展开…(无压栈出栈的过程)
2.发生在 “ 编译阶段”…判断语法处理(形参是否合法)…
1.5.3.2 函数声明 / 函数体 都要加 inline:
1.5.3.3 inline内联函数的特点:
1.5.3.4 inline内联函数内部不易复杂:
1.6 函数的 默认参数和占位参数:
1.6.1 默认参数:
1.6.2 默认参数 “从右往左”默认:
1.6.3 占位参数:
占位参数:没有意义…起着“占位”的作用(预留空间)…
1.6.4 占位参数 + 默认参数:
1.7 函数重载:
1.定义:
函数由:函数的返回值、函数形参列表(参数的个数、参数的类型、参数的顺序) 决定的…
1.C语言中 只要函数名func 相同…就会报编译错误…
2.C++中 函数名相同 但是参数列表不同…可以编译…称为 “函数重载”…
3.函数返回值…并不是构成函数重载的条件…
2.函数重载的调用:
例如:
两个函数func…参数列表中(参数个数不同)…在调用的时候 编译器会根据参数的个数进行调用…
3.如果要函数重载的话,不要写默认参数,为了避免函数歧义:
4.函数重载的匹配规则:
5.函数重载的底层实现(name mangling 倾炸技术):
1.8 函数重载 和 函数指针:
1.回顾函数指针:
1.1 typedef 方式一 :
(1) 定义一个函数:
int func(int a,int b)
{
cout<< a << endl;
return a;
}
(2) 定义一种函数类型:
typedef int(MY_FUC)(int,int)//按照此类型 定义一种新的函数
(3) 定义函数指针:
MY_FUC *fp =NULL;//定义函数指针
fp =func;//指向func
fp(10,20)//赋值
1.2 typedef * 方式二 :
定义一种 指向函数类型 的 指针类型:
(1) 定义一个函数:
int func(int a,int b)
{
cout<< a << endl;
return a;
}
(2) 定义一种指向 函数类型 的 指针类型:
typedef int(*MY_FUC2)(int,int)//定义一种指向该函数类型的 指针类型...
(3) 定义函数指针:
MY_FUC2 fp2 = NULL;
fp2 = func;
fp2(10,20)
1.3 int (*fp) 方式三 :
在原有的函数类型基础上 定义指针:
//3.
int(*fp3)(int,int) =NULL;
fp3 = func;
fp3(10,20)
2.函数指针 在赋值时 已经发生 函数重载了:
2.0 类的概念:
1.类(数据类型) 和 对象(变量):
2.Class 类方法:
3.Class的 公用属性(public) / 私用属性(private) / 保护属性(protected):
(1) public / private :
1.get_year():
定义一个Class类方法(get_year)来调用 私用方法:
2.set_year():
(2) protected :
(3) Class默认的访问权限是private :
(4) C++中 Class 和 struct 唯一的不同(默认的访问权限):
2.1 类的封装:
1.struct ( ) 调用实现方法很麻烦…函数在结构体外部调用:
#include <iostream>
using namespace std;
struct Date
{
int year;
int month;
int day;
};
void init(Date &d)
{
cout<<"year,month,day:"<<endl;
cin>>d.year>>d.month>>d.day;
}
void print(Date& d)
{
cout<<"year month day"<<endl;
cout<<d.year<<":"<<d.month<<":"<<d.day<<endl;
}
bool isLeapYear(Date& d)
{
if((d.year%4==0&& d.year%100 != 0) || d.year%400 == 0)
return true;
else
return false;
}
int main()
{
Date d;
init(d);
print(d);
if(isLeapYear(d))
cout<<"leap year"<<endl;
else
cout<<"not leap year"<<endl;
return 0;
}
2.Class ( ) 将 属性和方法 封装在 内部:
#include <iostream>
using namespace std;
class Date
//public 公有 可访问 可访问
//protected 保护 可访问 不可访问
//private 私有 可访问 不可访问
{
public:
void init(Date& d);
void print(Date& d);
bool isLeapYear(Date& d);
private:
int year;
int month;
int day;
}
void Date::init(Date& d)
{
cout<<"year,month,day:"<<endl;
cin>>d.year>>d.month>>d.day;
}
void Date::print(Date& d)
{
cout<<"year month day"<<endl;
cout<<d.year<<":"<<d.month<<":"<<d.day<<endl;
}
bool Date::isLeapYear(Date& d)
{
if((d.year%4==0 && d.year%100 != 0) || d.year%400 == 0)
return true;
else
return false;
};
int main()
{
Date d;
d.init(d);
d.print(d);
if(d.isLeapYear(d))
cout<<"leap year"<<endl;
else
cout<<"not leap year"<<endl;
return 0;
}
2.2 面向过程 和 面向对象:
面向过程:主要驱动函数…
面向对象:主要Class实例化后的对象…
2.3 面向对象求圆的周长和面积:
1.面向过程:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
//计算周长
double getCircleZC(double r)
{
return r*3.14*2;
}
//计算面积
double getCircleArea(double r)
{
return r*r*3.14;
}
int main()
{
//初始化圆的半径
double r =10;
double z =0;
double a =0;
z = getCircleZC(r);
a = getCircleArea(r);
cout << "周长="<< z <<"面积="<< a <<endl;
system("pause");
}
2.面向对象:
#define _crt_secure_no_warnings 1
#include <iostream>
using namespace std;
//定义一个圆类:
class Circle
{
public:
void setR(double r)//设置r
{
m_r =r;
}
double getCirleGirth()//返回周长
{
return m_r*2*3.14;
}
double getCirelArea()//返回面积
{
return m_r*m_r*3.14;
}
double getR()//打印半径r
{
return m_r;
}
private:
double m_r;
};
int main()
{
Circle r;//Class 实例化对象"r"...
r.setR(10);//调用类方法...设置半径
cout<<"半径="<<r.getR()<<endl;
cout<<"周长="<<r.getCirleGirth()<<endl;
cout<<"面积="<<r.getCirelArea()<<endl;
system("pause");
return 0;
}
3.类属性开辟内存空间 / 类方法不开辟:
1.这样设置可以吗???:
2.报错:
3.原因:
实例化之后…只为类属性开辟空间…类方法不开辟…
C2.setR(10)…只改变了m_r…
2.4 多文件编写_面向对象求圆的周长和面积:
1.创建 Circle.h(函数声明) 和Circle.cpp(副本):
2.Circle.h(函数声明):
#pragma once
#ifndef __CIRCLE_H_
#define __CIRCLE_H_
class Circle
{
public:
//设置半径
void setR(double r);
//计算周长
double getGirth();
//计算面积
double getArea();
//得到半径
double getR();
private:
double m_r;//定义半径
double m_girth;//定义周长
double m_area;//定义面积
};
#endif
3.Circle.cpp(副本):
2.1 首先引入头文件 #include Circle.h
2.2 viod Circle::setR(double r) :
代表我这个方法属于这个类…
2.3 code :
#include "Circle.h"
void Circle::setR(double r)//设置r
{
m_r =r;
}
double Circle::getGirth()//返回周长
{
return m_r*2*3.14;
}
double Circle::getArea()//返回面积
{
return m_r*m_r*3.14;
}
double Circle::getR()//打印半径r
{
return m_r;
}
4.main ( ):
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include "Circle.h"
using namespace std;
int main()
{
Circle r;//Class 实例化对象"r"...
r.setR(10);//调用类方法...设置半径
cout<<"半径="<<r.getR()<<endl;
cout<<"周长="<<r.getGirth()<<endl;
cout<<"面积="<<r.getArea()<<endl;
system("pause");
return 0;
}
2.5 判断两个立方体是否相等:
1.全局函数:
2.类方法: