输入输出函数
hello world
#include <iostream>using namespace std;int main(){ cout << "Hello World! 你好" << endl; int num = 0; cin >> num; cout << "num = " << num << endl; return 0;}
作用域运算符 ::
#include <iostream>using namespace std;int a = 10;int main(){ cout << "Hello World! 你好" << endl; int num = 0; cin >> num; cout << "num = " << num << ::a << endl; return 0;}
命名空间
namespace A { int a = 100; void call(){ printf("ssss"); } void eat();}void A::eat(){ printf("ddssssdddss");}
命名空间别名
#include <iostream>using namespace std;namespace veryLongName { int a = 10;}int main(){ namespace b = veryLongName; cout << "veryLongName = " << b::a <<endl; return 0;}
使用命名空间
#include <iostream>using namespace std;namespace veryLongName { int a = 10;}int main(){ using namespace veryLongName; cout << "veryLongName = " << a <<endl; return 0;}
只使用命名空间中的某一成员
#include <iostream>using namespace std;namespace veryLongName { int a = 10;}int main(){ using veryLongName::a; cout << "veryLongName = " << a <<endl; return 0;}
C++里面的结构体
struct 结构体名 { 结构体成员列表 };using namespace std;//全局变量声明区//函数声明区//结构体声明struct student { //成员列表 string name; //姓名 int age; //年龄 int scoro; //分数};
#define 宏,作用于他所在位置的下面代码部分
#include <iostream>using namespace std;void my_func(void){ const int num = 0; #define MY_NUM 10}int main(){ cout << "veryLongName = " << MY_NUM << endl; return 0;}
命名空间里面无法使用宏
Ifndef 用于检测宏是否定义
#ifndef _HEAD_H_ //如果没有定义_HEAD_H_#define _HEAD_H_ //定义_HEAD_H_
C++ 中的引用
#include <iostream>using namespace std;int main(){ int num = 10; int &a = num; a = 100; cout<< "a = " << num << endl; return 0;}
数组里面的引用方式一
#include <iostream>using namespace std;void test02(){ int arr[5] = {10,20,30,40,50};// 给arr起一个别名- int (&my_arr)[5] = arr; int i = 0; for(i = 0; i<5;i++){ cout << my_arr[i] << " "; } cout<<endl;}int main(){ test02(); return 0;}
数组里面的引用方式二
#include <iostream>using namespace std;void test03(){ int arr[5] = {10,20,30,40,50}; // 使用typedef定义数组类型 typedef int TYPE_ARR[5]; // myArr是数组别名 TYPE_ARR &myArr = arr; int i = 0; for(i = 0; i<5;i++){ cout << myArr[i] << " "; } cout<<endl;}int main(){ test03(); return 0;}
C++ 中引用作为函数参数传递
#include <iostream>using namespace std;void sum(int *a ,int *b){ int temp = *a; *a = *b; *b = temp;}int main(){ int num1 = 10; int num2 = 20; cout << "num1 == " << num1 << endl; cout << "num2 == " << num2 << endl; sum(&num1,&num2); cout << "after num1 == " << num1 << endl; cout << "after num2 == " << num2 << endl; return 0; }
C++中函数的返回值是引用
#include <iostream>using namespace std;int temp;int fun1(){ temp = 10; return temp;}int& fun2(){ temp = 10; return temp;}int main(){ int a = 0, b = 0; // 1. 返回函数的普通类型 a = fun1(); // 2. 返回函数的引用 b = fun2(); // 3. 返回函数的引用去初始化一个新的引用 // 函数返回值为左值时,返回值必须是引用 int &c = fun2() = 2000; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; return 0;}// 编译结果:a = 10 b = 10 c = 10
C++中指针的引用
#include <iostream>using namespace std;int temp;int& fun(){ temp = 10; return temp;}int main(){ // 函数返回值为左值时,返回值必须是引用 int &c = fun() = 2000; // 二级指针 int &d = *&c; d = 120; cout << "c = " << c << endl; return 0;}// 编译结果: c = 120
C++中常引用
#include <iostream>using namespace std;typedef struct { int num; char name[32];}STU;// 直接传入,可以修改void myPrintSTU1(STU temp){ cout<< sizeof(temp) << endl; cout << "name = " << temp.name << "age = " << temp.num << endl;}// 直接传入, 防止值进行修改void myPrintSTU2(const STU &temp){ cout<< sizeof(temp) << endl; cout << "name = " << temp.name << "age = " << temp.num << endl;}void test(){ STU lucy = {20,"liuyun 中国"}; myPrintSTU2(lucy);}int main(){ test(); return 0;}
C++内联函数
#include <iostream>using namespace std;inline int Max(int x, int y){ return (x > y)? x : y;}// 程序的主函数int main( ){ cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0;}
C++中函数的默认参数
#include <iostream>using namespace std;inline int Max(int x = 100, int y = 23){ return (x > y)? x : y;}// 程序的主函数int main( ){ cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0;}
C++中的函数声明和函数定义
fun.cpp
extern int func(int a,int b =12, int c = 20);
// 这里函数声明,函数定义不能同时设置默认参数
main.cpp
#include <iostream>
#include "fun.cpp"
using namespace std;
int func(int a,int b, int c){
cout << "a: " << a << " b: " << b << " c: " << c << endl;
}
inline int Max(int x = 100, int y = 23)
{
return (x > y)? x : y;
}
// 程序的主函数
int main( )
{
func(10);
return 0;
}
C++的函数占位参数
#include <iostream>
#include "fun.cpp"
using namespace std;
void funs(int a ,int){
cout << "func = call" << endl;
}
// 程序的主函数
int main( )
{
// 只有传入此参数才会调用此函数
funs(10,11);
return 0;
}
C++的函数的重载
#include <iostream>
#include "fun.cpp"
using namespace std;
void funs(int a ,int){
cout << "func = call" << endl;
}
void funs(int a ,int b,int c){
cout << "func = call2" << endl;
}
// 程序的主函数
int main( )
{
funs(10,11,12);
return 0;
}
C++中的类,构造函数
#include <iostream>
using namespace std;
class Line{
public:
void setHeight(double len);
double getHeight();
Line(double len); // 这是构造函数声明
~Line(); // 这是析构函数声明
private:
double height;
};
void Line::setHeight(double len){
height = len;
}
double Line::getHeight(void){
return height;
}
Line::Line(double len){
cout << "Object is being created, length = " << len << endl;
height = len;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
// 程序的主函数
int main()
{
Line line(10);
// 获取默认设置的长度
cout << "Length of line : " << line.getHeight() <<endl;
// 再次设置长度
line.setHeight(6.0);
cout << "Length of line : " << line.getHeight() <<endl;
return 0;
}
C++中new对象
#include <iostream>
using namespace std;
class TestNew
{
private:
int ID;
public:
TestNew(int ID);
~TestNew();
};
TestNew::TestNew(int ID)
{
this->ID = ID;
}
TestNew::~TestNew()
{
std::cout<<"对象 "<<this->ID<<" 执行析构函数"<<std::endl;
}
void Test()
{
TestNew test(1);//创建对象1,不使用new
TestNew *pTest = new TestNew(1);//创建对象2,使用new
}
int main()
{
Test();//这个地方有点问题,pTest没有进行处理,会导致内存泄露,实际应用中要注意呀
}
new创建类对象需要指针接收,一处初始化,多处使用
new创建类对象使用完需delete销毁
new创建对象直接使用堆空间,而局部不用new定义类对象则使用栈空间
new对象指针用途广泛,比如作为函数返回值、函数参数等
频繁调用场合并不适合new,就像new申请和释放内存一样