C++ 视频课笔记1
来源:互联网 发布:彩票开奖源码 编辑:程序博客网 时间:2024/06/05 16:31
2017.2.21
1.C++起航篇
C语言是C++的子集
C++的变量可以随用随定义
C++定义了 bool 的数据类型,这个C语言是没有的 (真:ture 假:false)
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
cout << "请输入一个整数:" << endl;
int x = 0;
cin >> x;
cout << oct << x << endl;
cout << dec << x << endl;
cout << hex << x << endl;
cout << "请输入一个布尔值(0或1):" << endl;
bool y = false;
cin >> y;
cout << boolalpha << y << endl;
system("pause");
return 0;
}
命名空间:简言之就是划片取名字,小王和小张都加入了A公司,我们喊名字的时候就可以喊:A公司的小王,A公司的小张。 所以这个A公司就是命名空间了。 命名关键字:namespace
namespace A namespace B 想引用时为:
{ {
int x = 0 ; int x = 0; cout<< A::x<<endl;
void f1( ); void f1( ); B::f1( );
void f2( ); void f3( ) ;
} }
#include<iostream>
#include<stdlib.h>
using namespace std;
namespace A
{
int X = 1;
void fun()
{
cout << "A" << endl;
}
}
namespace B
{
int X = 2;
void fun()
{
cout << "B" << endl;
}
void fun2()
{
cout << "2B" << endl;
}
}
int main()
{
cout << A::X << endl;
B::fun();
using namespace B;
fun2(); //不加B:: 也可以在前面声明 using namespace ...
system("pause");
return 0;
}
作业:定义一个命名空间为mynum,在该明明空间中定义一个整型变量x,并给该变量赋值为105;使用C++的新特性 bool类型判断mynum命名
空间下的变量是奇数还是偶数?
#include<iostream>
#include<stdlib.h>
using namespace std;
namespace mynum
{
int X = 106;
}
int main()
{
bool isflag = false;
if (mynum::X % 2 == 0)
{
isflag = false;
}
else
{
isflag = true;
}
if (isflag)
{
cout << "变量X是奇数" << endl;
}
else
{
cout << "变量X是偶数" << endl;
}
system("pause");
return 0;
}
/*知识点:bool 类型 命名空间 输入输出*/
/*题目要求:使用一个函数找出一个整型数组中的最大值或最小值*/
#include<iostream>
#include<stdlib.h>
using namespace std;
namespace CompA
{
int getMaxorMin(int *arr, int count, bool isMax)
{
int temp = arr[0];
for (int i = 1; i < count; i++)
{
if (isMax)
if (temp < arr[i])
{
temp = arr[i];
}
else
{
if (temp > arr[i])
{
temp = arr[i];
}
}
}
return temp;
}
}
int main()
{
int arr1[4] = { 1, 3, 5, 7 };
bool isMax = false;
cin >> isMax;
cout<< CompA::getMaxorMin(arr1, 4, isMax)<<endl;
system("pause");
return 0;
}
2.C++离港篇
2.1引用类型
引用:是变量的别名。
基本数据类型的引用
#include<iostream>
using namespace std;
int main()
{
int a = 3;
int &b = a; // 引用必须初始化
b=10;
cout << a << endl;
return 0;
}
结构体类型的引用
typedef struct
{
int x;
int y;
} Coor;
#include<iostream>
using namespace std;
int main()
{
Coor c1;
Coor &c = c1; // 引用必须初始化
c.x=10;
c.y=20;
cout << c1.x << c1.y;
return 0;
}
指针类型的引用(类型 *&指针引用名=指针;)
#include<iostream>
using namespace std;
int main()
{
int a =10;
int *p = &a;
int *&q = p; // 引用必须初始化
*q=20;
cout << a << endl;
return 0;
}
引用作函数参数
#include<iostream>
#include<stdlib.h>
using namespace std;
void fun(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
}
int main()
{
int x = 10;
int y = 20;
cout << x <<"," << y << endl;
fun(x, y);
cout << x << "," << y << endl;
system("pause");
return 0;
}
单元巩固:定义一个引用y,y是x的引用,然后打印x和y的值。将y的值更改之后再次打印,x和y的值。
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int x = 3;
int &y = x;
cout << x <<"," << y << endl;
y = 5;
cout << x << "," << y << endl;
system("pause");
return 0;
}
2.2 控制变化的 const
const int *p = NULL; 和 int const *p = NULL; 是等价的(能通过p去改变变量的数值) int *const p =BULL(p只能指向一个变量,*p可以赋值去改变变量的值)
const 与引用 (从效果上和#define 效果一样,而且编译时可以检测语法错误,define 就不可以,所以推荐用const)
int x = 3; const int &y = x; //x = 10;正确 //y = 20;错误(y不可变)
单元巩固:使用const关键字定义整型变量count,并定义指针p引用变量count。利用for循环打印count次hello imooc
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
const int count = 3;
const int *p = &count;
for (int i = 0; i < count; i++)
{
cout << "hello imooc" << endl;
}
system("pause");
return 0;
}
2.3 C++函数新特性
2.3.1.函数参数默认值
void fun(int i,int j =5,int k =10);
1.有默认参数值的参数必须是参数表的最右端
2.在函数声明中加入默认值,而在函数定义时最好不要加入默认值(否则部分编译器通不过)
eg: void fun(int i , int j =5,int k =10); int main()
{
void fun (int i ,int j ,int k) fun(20);
{ fun(20,30); 无实参则用默认值,否则实参覆盖默认值
cout <<i<<j<<k; fun(20,30,40);
} }
代码演示:
#include<iostream>
#include<stdlib.h>
using namespace std;
void fun(int i=30, int j=20, int k=10);
int main()
{
fun();
fun(100);
fun(100, 200);
fun(100, 200, 300);
system("pause");
return 0;
}
void fun(int i, int j, int k)
{
cout << i << "," << j << "," << k << endl;
}
2.3.2 函数重载
在相同的作用域下,用同名函数的多个函数,参数个数和参数类型不同
int getMax(int x,int y,int z) doublegetMax(double x,double y) 编译好后分别生成 getMax_int_int_int 和 getMax_double_double,调用时计算机自动识别,可以减少代码编写,提高效率 。
{ {
// to do //to do
} }
代码演示:
#include<iostream>
#include<stdlib.h>
using namespace std;
void fun(int i=30, int j=20, int k=10);
void fun(double i, double z);
int main()
{
fun(1.1, 2.2);
fun(1, 2);
system("pause");
return 0;
}
void fun(int i, int j, int k)
{
cout << i << "," << j << "," << k << endl;
}
void fun(double i, double z)
{
cout << i << "," << z << endl;
}
2.3.3内联函数
内联函数:编译时将函数体代码和实参代替函数调用语句
内联函数是建议性的,由编译器决定。
逻辑简单,调用频繁的函数建议使用内联。
递归函数无法使用内联方式。
关键字:inline
inline int max(int a,int b,int c);
int main()
{
int i = 10, j =20 , k = 30 , m;
m = max(i , j , k);
cout << "max ="<<m<<endl;
return 0;
}
单元巩固:使用函数重载完成返回最大值的方法。现有一个数组,定义一个方法getMax(),利用函数的重载,分别实现:
1.随意取出数组中的两个元素,传到方法getMax()中,可以返回较大的一个元素。
2。将整个数组传到方法getMax()中,可以返回数组中最大的元素。
代码演示:
#include<iostream>
#include<stdlib.h>
using namespace std;
int getMax(int a, int b)
{
return a > b ? a : b;
}
int getMax(int *arr, int count)
{
int i; int c;
c = arr[0];
for (i = 1; i < count; i++)
{
if (c<arr[i])
{
c = arr[i];
}
}
return c;
}
int main()
{
int numarr[] = { 3, 6, 8 };
cout << getMax(5, 9) << endl;
cout << getMax(numarr, 3) << endl;
system("pause");
return 0;
}
2.3.4内存管理
内存本质是资源,操作系统掌管内存,我们能做的就是申请/归还 内存资源,也就是内存管理
申请内存 new int*p= new int int *arr = new int[10]
if(NULL == P) if(NULL == arr)
{ {
//内存分配失败 //内存分配失败
//异常处理 //异常处理
} }
释放内存 delete delete p delete [ ]arr
p = NULL; arr = NULL; // 最后还要将内存指向空,否则容易出现异常
new delete 要配套使用!
代码示例:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int *p = new int[1000];
if (NULL == p)
{
system("pause");
return 0;
}
p[0] = 1;
p[1] = 2;
cout << p[0] <<","<<p[1]<< endl;
delete []p;
p = NULL;
system("pause");
return 0;
}
单元巩固:在堆中申请100个char类型的内存,拷贝Hello imooc字符串到分配的堆中的内存中,打印字符串,最后释放内存。
(这里面出现了编译的问题,通过查阅网上资料得到解决:main 前面加上 #pragmawarning(disable:4996))
代码演示:
#include<string.h>
#include<iostream>
using namespace std;
#pragma warning(disable:4996) // error C4996 加上这句把4996错误报警设置为失效就成功编译完成了
int main()
{
char *str = new char[100];
//if (NULL == str)
//{
// system("pause");
// return 0;
//}
strcpy(str,"Hello imooc");
cout << str << endl;
delete []str;
str = NULL;
system("pause");
return 0;
}
阅读全文
0 0
- C++ 视频课笔记1
- C语言视频学习笔记
- 看朱老师视频笔记-------C语言
- 视频笔记1
- 视频笔记1
- OC视频笔记-1
- C++ 视频课笔记2
- C++ 视频课笔记3
- C++ 视频课笔记4
- C++ 视频课笔记5
- C++ 视频课笔记6
- C++ 视频课笔记7
- C++ 视频课笔记8
- 视频学习第一课笔记
- oracle 视频笔记1(select)
- oracle 视频笔记1(select)
- opencv笔记1:读写视频
- redis视频学习笔记-1
- DDoS
- *打印菱形
- Elasticsearch中fielddata_cache的实现
- 使用SSH_Key完成Git与GitHub绑定
- POJ: River Hopscotch(二分搜索)
- C++ 视频课笔记1
- 第二十二天笔记xml
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/Nestabl
- heartbeat
- Linux 服务器的链接数统计
- C++ 视频课笔记2
- How to build MTFMapper
- Visual Studio Code 使用心得
- NodeJS运行时抛出: Error: listen EADDRINUSE :::3000