初识c++

初识c++

1:c++关键字

asmdoifreturntrycontinue
autodoubleinlineshorttypedeffor
booldynamic_castintsignedtypeidpublic
breakelselongsizeofthrowtypename
caseenummutablestaticunionwchar_t
catcheplicitanmespacestatic_castunsigneddefault
charexportnewstructusingfriend
classexternswitchvirtualregisteroperator
constfalseprivatetemplatevoidtrue
const_castgo toprotectedthisvolatilewhile
deletefloatreinterpret_cast

2:命名空间

2.1 命名空间的定义

#include<iostream>
namespace N1 {
int a = 10;
int b = 20;
int Add(int left, int right) {
return left + right;
}
}
namespace N2 {
int a = 10;
int b = 20;
int Add(int left, int right) {
return left + right;
}
 namespace N3 {
int c = 30;
int d = 40;
int Sub(int left, int right) {
return left - right;
}
}
}
int main() {
 using namespace N2;
printf("%d ", N1::a);
printf("%d ", b);
printf("%d ", N2::N3::c);
printf("%d ", Add(10, 20));
printf("%d ", N2::N3::Sub(30, 20));
system("pause");
return 0;
}

3:c++输入输出

#include<iostream>
using namespace std;
int main() {
int a;
char b;
double c;
 cin >> a >> b >> c;
 cout << a << " " << b << " " << c << endl;
system("pause");
return 0;
}

4:缺省参数、
缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该
默认值,否则使用指定的实参

//全缺省参数
#include<iostream>
using namespace std;
void TestFunc(int a = 10) {
 cout << a << endl;
}
int main() {
TestFunc();
TestFunc(20);
system("pause");
return 0;
}
//半缺省参数
#include<iostream>
using namespace std;
void TestFunc(int a, int b = 10) {
 cout << a <<" "<< b << endl;
}
int main() {
TestFunc(20);
system("pause");
return 0;
}

5):函数重载

函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的
形参列表(参数个数 或 类型 或 顺序)必须不同,常用来处理实现功能类似数据类型不同的问题

#include<iostream>
using namespace std;
void print(int i) {
 cout << i << endl;
}
void print(string str) {
 cout << str << endl;
}
int main() {
print(1);
print("hello world");
system("pause");
return 0;
}

6)引用

6.1:引用特性

#include<iostream>
using namespace std;
void TestFunc() {
int a = 10;
int& ra = a;
 ra = 100;
 cout << a << " " << ra << endl;//100  100
}
int main() {
TestFunc();
system("pause");
return 0;
}
void testfunc() {
int a = 10;
int& ra = a;
int& rra = a;
 cout << a << " " << ra << " " << rra << endl;//10 10 10
 cout << &a << " " << &ra << " " << &rra << endl;//00AFFB04 00AFFB04 00AFFB04
}
int main() {
testfunc();
system("pause");
return 0;
}

6.2 常引用

void TestFunc() {
const int a = 10;
const int& ra = a;
const int& b = 10;
double d = 3.14;
const int& rd = d;
 cout << a << " " << ra << " " << b << " " << d <<" "<<rd<< endl;
//10 10 10 3.14 3
}
int main() {
TestFunc();
system("pause");
return 0;
}

6.3 使用场景

//做参数
void swap(int& left, int& right) {
int t = left;
 left = right;
 right = t;
}
void TestFunc() {
int a = 10;
int b = 20;
swap(a, b);
 cout << a << " " << b << endl;//20 10
}
int main() {
TestFunc();
system("pause");
return 0;
}
//做返回值
int& Add(int a, int b) {
int c = a + b;
return c;
}
int main() {
int& ret = Add(1, 2);
Add(3, 4);
 cout << ret << endl;
system("pause");
return 0;
}

6.4 传值,传引用效率比较

#include<time.h>
struct a {
 int a[10000];
};
void testfunc1(a a) {
}
void testfunc2(a& a) {
}
void testrefandvalue() {
 a a;
 //以值作为函数参数
 size_t begin1 = clock();
 for (size_t i = 0;i < 10000;i++) {
  testfunc1(a);
 }
 size_t end1 = clock();
 //以引用作为函数参数
 size_t begin2 = clock();
 for (size_t i = 0;i < 10000;i++) {
  testfunc2(a);
 }
 size_t end2 = clock();
 //分别计算两个函数运行结束后的时间
 cout << "testfunc1(int*)-time" << end1 - begin1 << endl;
 cout << "testfunc2(int&)-time" << end2 - begin2 << endl;
}
int main() {
 for (int i = 0;i < 10;i++) {
  testrefandvalue();
 }
 system("pause");
 return 0;
}

值和引用作为返回值类型的比较

#include<time.h>
struct A {
 int a[10000];
};
A a;
A TestFunc1() {
 return a;
}
A& TestFunc2() {
 return a;
}
void TestReturnByRefOrvalue() {
 //以值作为函数的返回类型
 size_t begin1 = clock();
 for (size_t i = 0;i < 10000;i++) {
  TestFunc1();
 }
 size_t end1 = clock();
 //以引用作为函数的返回类型
 size_t begin2 = clock();
 for (size_t i = 0;i < 10000;i++) {
  TestFunc2();
 }
 size_t end2 = clock();
 cout << "TestFunc1 time" << end1 - begin1 << endl;
 cout << "TestFunc2 time" << end2 - begin2 << endl;
}
int main() {
 for (int i = 0;i < 10;i++) {
  TestReturnByRefOrvalue();
 }
 system("pause");
 return 0;
}

6.5 内联函数
test.h

#pragma once
class Test {
public:
 Test(int a,int b):a(a),b(b){}
 int max();
private:
 int a;
 int b;
};

test.c

#include"Test.h"
inline int Test:: max() {
 return a > b ? a : b;
}

Main.c

#include"Test.h"
#include<iostream>
using namespace std;
inline int Test::max()
{
 return a > b ? a : b;
}
int main() {
 Test a(3, 5);
 cout << a.max() << endl;
 system("pause");
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值