目录
C++ == C + 面向对象 + 标准库
C语言标准库:
stdio string stdlib stdbool
多个函数组成一个库,宏 ,数据结构
面向对象:
用对象的思维去思考,去描述 事物
物 数据 数据结构
事 代码 函数 算法
面向过程:
用流程的思维去考虑问题,去描述事物
C++的工程:win32控制台应用程序
stdafx.h : 预编译头文件 ,所有的.h都应该放到这个文件里包含,所有的.cpp文件都应该包含此头文件
stdafx.cpp : 没有用处
targetver.h: 平台需要,写代码时候无视
0X01文字与字符集:
字母文字:英语
一个字节 8个bit 256种
char
char c = 'a';
象形文字:汉语
一个汉字就是一个字符串
char c[2] = "中";
多字节字符集:一个英文字符一个字节。一个汉字字符就是一个字符串,就是多个字节。
宽字节字符集(unicode字符集):不论是英文字符还是汉字字符,一个符号都是是一个宽字节(unsigned short 2byte)。一个英文字母是两个字节,一半是0/,另一半是该字母,一个汉字刚好就是一个宽字节。例如:wchar_t c = '中';
操作系统字符集:兼容字符集
_tmain:
_TCHAR:
设置项目字符集:
项目->属性->配置属性->常规->字符集
0x02函数:
1.函数的缺省参数:
- 缺省参数在函数原型中写,在函数定义的时候不用写,也不能写
- 函数原型中注明缺省参数时不能省略形参名。
2.函数重载
在同一作用域内,函数名相同,参数列表不同构成重载,包含两种情况:1.参数个数不同 2.参数类型不同。函数重载和缺省参数结合时需要注意二义性问题
C++对类型的检查非常严格:int n = 1+1.2; 这种写法是错误的
0x03 C++中使用结构体:
- 1 可以省略关键字
- 2 可以有成员函数,成员函数不占结构构造出来的结构体的内存。
- 3 结构体有默认的一个指针,指针名 this,指向结构构造出来的每一个结构体
0x04 C++中有bool类型:
C语言里面没有bool类型,需要导入stdbool.h
0x05 C++中输入输出不一样
iostream 头文件
std::cin >>
std::cout <<
std::endl 换行
0x06 C++中有字符串类:string类
0x07 C++中动态内存分配不一样
new 类型名 返回指针
delete 指针
new 类型名[个数]
delete[] 指针
new 不能 搭配 free
malloc 搭配 free
new 和 delete 是运算符
malloc和free 函数
0x08 C++中有引用类型
给内存段起别名。变量名是内存段的原名。引用必须初始化
// 引用类型.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//void swap(int a, int b){ int t = a;a = b;b = t;}
void swap(int* a, int* b){ int t = *a; *a = *b; *b = t; }
void swap(int& a, int& b){ int t = a; a = b; b = t; }
int _tmain(int argc, _TCHAR* argv[])
{
int n = 10, m = 20;
printf("交换前 n:%d,m:%d\n", n, m);
swap(n, m);
printf("交换后 n:%d,m:%d\n", n, m);
while (1);
return 0;
}
作业:
使用引用写个函数遍历数组。
提示:
用下标方式遍历数组
用指针移动方式遍历数组
用引用方式遍历数组
#include <stdio.h>
struct student{
char name[20];
int age;
double score;
void(*pInit)(struct student*, char*, int, double);
};
void init(struct student* pThis,char* name, int age, double score){
strcpy(pThis->name, name);
pThis->age = age;
pThis->score = score;
}
int main(){
struct student stu1;
stu1.pInit = init;
stu1.pInit(&stu1, "张三", 18, 11.11);
printf("%s,%d,%.2lf\n", stu1.name, stu1.age, stu1.score);
while (1);
return 0;
}
// CPP.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream> //C++风格的头文件* C风格头文件 *.h C++风格使用C的头文件 c*
#include <string> //C++中string类的头文件
#include <cstring> //#include <string.h> //C语言的strcpy strlen strcmp memcpy memset 。。。
int _tmain(int argc, _TCHAR* argv[])
{
/*
//1 输入输出
C 标准格式化输入输出
C++输入输出类 智能类型识别
int n, m;
char c;
//scanf("%d-%d", &n, &m);
std::cin >> n >> c >> m;
//printf("n:%d,m:%d\n", n, m);
std::cout << "n:" << n << ",m:" << m << std::endl;
*/
/*
int n = 0;
n = 666;
std::cout << n << std::endl;
char name[20] = "abcdefg";
printf("%s\n", name);
std::string str = "字符串";
std::cout << str << std::endl;
std::cin >> str;
std::cout << str << std::endl;
str = "你好";
std::cout << str << std::endl;
std::cout << "字符串大小" << strlen(str.c_str()) << std::endl;
*/
/*
代码区 值
堆区 手动内存分配 malloc
栈区 定义的变量 定义的时候就存在,一直要到函数结束才会释放
静态存储区 static
char* pStr = NULL;
pStr = (char*)malloc(5);
std::string* pString = NULL;
pString = new std::string;
*/
int* pInt = (int*)malloc(4*5);
for (int i = 0; i < 5; i++)
pInt[i] = i;
free(pInt);
int* pInterger = new int[5];
for (int i = 0; i < 5; i++)
pInterger[i] = i;
delete[] pInterger;
while (1);
return 0;
}
// CPPDay01.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#if 0
void f1(int a, double b){
printf("a:%d,b:%lf\n", a, b);
}
void f1(double a, int b){
printf("a:%lf,b:%d\n", a, b);
}
void f1(int a, double b, char c = '1'){
printf("a:%d,b:%lf,c:%c\n", a, b,c);
}
//int f1(int c, double d){ printf("c:%d,d:%lf\n", c, d);}
//void f1(int a, double b){ printf("a:%d,b:%lf\n", a, b);}
int _tmain(int argc, _TCHAR* argv[])
{
f1(666, 888.888, 'c');
f1(555, 111.111);//二义性问题
/*
printf("1+1=%d\n", add(1));
printf("1+3=%d\n", add(1, 3));
printf("1.1 + 2.2 = %.2lf\n", add_double(1.1, 2.2));
printf("1.1 - 2.2 = %.2lf\n", sub(1.1, 2.2));
printf("1-2=%d\n", sub(1, 2));
*/
while (1);
return 0;
}
#endif
struct student{
char name[20];
int age;
double score;
void init(char* name, int age, double score);
};
void student::init(char* name, int age, double score){
strcpy(this->name, name);
this->age = age;
this->score = score;
}
int main(){
student stu1;
printf("%d\n", sizeof stu1);
stu1.init("孙泽华", 17, 99.99);
while (1);
return 0;
}