C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > 学习C++的基础知识

新手学习C++的基础知识概况总结

作者:chh563

文章介绍了C++中命名空间的定义和使用,包括定义和冲突解决、使用方式和标准库中的应用,还详细介绍了C++输入输出流、缺省参数、函数重载、引用、内联函数和nullptr等概念及其用法和注意事项

1.C++的第一个程序

#include <iostream>
using namespace std;
int main()
{
	cout << "Hello World" << endl;
	return 0;
}

2.命名空间

2.1 namespace的价值

在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称都存在于全局作用域中,可能会导致很多冲突。命名空间的目的就是对标识符进行本地化,以避免命名冲突,namespace关键字的出现就是针对这种问题的。

2.2 namespace的定义

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
namespace abc
{
    int rand = 10;
    int Add(int left,int right)
    {
        return left + right;
    }
    struct Node
    {
        struct Node* next;
        int val;
    };
}
int main()
{
    //这里默认访问的是全局的rand函数指针
    printf("%p\n",rand);
    //这里访问的是abc命名空间中的rand变量
    printf("%d\n",abc::rand);
    return 0;
}
//关于命名空间的嵌套
namespace asd
{
    namespace aaa
    {
        int rand = 15;
    }
    namespace bbb
    {
        int rand = 20;
    }
}
int main()
{
    printf("%d\n",asd::aaa::rand);
    printf("%d\n",asd::bbb::rand);
    return 0;
}

2.3命名空间使用

编译查找一个变量的声明/定义,默认只会在局部或者全局查找,不会到命名空间里去查找。所以下面程序会编译报错。所以我们要使用命名空间中定义的变量/函数,有三种方式:

namespace N
{
    int a = 0;
    int b = 0;
}
//指定命名空间访问
int main()
{
    printf("%d\n",N::a);
    return 0;
}
//将命名空间中某个成员展开
using N::b;
int main()
{
    printf("%d\n",N::a);
    printf("%d\n",b);
    return 0;
}
//将命名空间全部展开
using namespace N;
int mian()
{
    printf("%d\n",a);
    printf("%d\n",b);
    return 0;
}

3.C++输入&输出

#include <stdio.h>
using namespace std

int main()
{
	int a = 0;
	double b = 0.1;
	char c = 'x';

	count << a << " " << b << " " << c << endl;
	std::cout << a << b << c << std::endl;
	scanf("%d%lf\n,&a,&b");
	printf("%d %lf\n,a,b");
	return 0;
}

//在io需求比较高的地方,
//如大部分大量输入的竞赛题中,
//加以下3行代码,可以提高效率
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

4.缺省参数

#include <iostream>
#include <assert.h>
using namespace std;
void Func(int a = 0)
{
	cout << a << endl;
}
int main()
{
	Func(); 
	Func(10); 
// 没有传参时, 使用 参数的默认值
//传参时,使用指定的实参
	return 0;
}
#include <iostream>
using namespace std;
// 全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
//半缺省
void Func2(int a, int b = 10, int c = 20)
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
int main()
{
	Func1();
	Func1(1);
	Func1(1,2);
	Func1(1,2,3);
	Func2(100);
	Func2(100, 200);
	Func2(100,200,300);
	return 0;
}

5.函数重载

C++支持在同一作用域中出现同名函数
,但是要求这些同名函数的形参不同,可以是参数个数不同或者类型不同。这样C++函数调用就表现出了多态行为,使用更灵活。C语言是不支持同一作用域中出现同名函数的。

#include<iostream>
using namespace std;
// 1、 参数类型不同
int Add(int left, int right)
{
	cout << "int Add(int left, int right)" << endl;
	return left + right;
}
double Add(double left, double right)
{
	cout << "double Add(double left, double right)" << endl;
	return left + right;
}
// 2、参数个数不同
void f()
{
	cout << "f()" << endl;
}
void f(int a)
{
}
---------此时调用f()会报错,编译器不知道调用谁
cout << "f(int a)" << endl;
//3、参数类型顺序不同
void f(int a, char b)
{
	cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
	cout << "f(char b, int a)" << endl;
}
// 返回值不同不能作为重载条件 ,
//void fxx()
//{}
//
//int fxx()
//{
// return 0;
//}


// 下面两个函数构成重载
// f()但是调用时,会报错,存在歧义,编译器不知 道调用谁
void f1()
{
	cout << "f()" << endl;
}
void f1(int a = 10)
{
	cout << "f(int a)" << endl;
}
int main()
{
	Add(10, 20);
	Add(10.1, 20.2);
	f();
	f(10);
	f(10,'a');
	f('a', 10);
	return 0;
}

6.引用

6.1引用的概念和定义

引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块
内存空间。
类型& 引用别名 = 引用对象

#include<iostream>
using namespace std;
int main()
{
	int a = 0;
// 引用 :b和c是a的别名
int& b = a;
int& c = a;
// 也可以给别名b取别名,d相当于还是a的别名
int& d = b;
++d;
// 这里取地址我们看到是一样的
cout << &a << endl;
cout << &b << endl;
cout << &c << endl;
cout << &d << endl;
return 0;
}

6.2引用的特性

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
// 编 译报 错 :“ra”: 必 须 初 始 9
//int& ra;
	int& b = a;
	int c = 20;
// 这里并非让b引用c
// 这里是一个赋值
	b = c;
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	return 0;

6.3引用的使用

void Swap(int& rx, int& ry)
{
	int tmp = rx;
	rx = ry;
	ry = tmp;
}
int main()
{
	int x = 0, y = 1;
	cout << x <<" " << y << endl;
	Swap(x, y);
	cout << x << " " << y << endl;
	return 0;
}
//指针变量也可以取别名,LTNode*& phead就是给 指针变量取别名
//这样就不需要用二级指针了,相对而言简化了程序
//void ListPushBack(LTNode** phead, int x)
//void ListPushBack(LTNode*& phead, int x)

6.4const引用

int mian()
{
    const int a = 10;
    //这里引用对a访问权限放大
    //int& ra = a;
    //这样可以
    const int& ra = a;
    //不能给常量赋值
    //ra++;
    //这里引用对b访问权限缩小
    int b = 20;
    const int& rb = b;
    //不能给常量赋值
    //rb++;
    return 0;
}

#include<iostream>
using namespace std;
int main()
{
    int a = 10;
    const int& ra = 30;
// 编译报错:“初始化”:无法从“int”转换为“int &”
// int& rb = a * 3;
    const int& rb = a*3;
    double d = 12.34;
// 编译报错:“初始化”:无法从“double”转换为“int &”
// int& rd = d;
    const int& rd = d;
    return 0;
}

6.5指针和引用的关系

C++中指针和引用就像两个性格迥异的亲兄弟,指针是哥哥,引用是弟弟,在实践中他们相辅相成,功能有重叠性,但是各自有各自特点,互相不可替代。
• 语法概念上引用是一个变量的取别名不开空间,指针是存储一个变量地址,要开空间。
• 引用在定义时必须初始化,指针建议初始化,但是语法上不是必须的。
• 引用在初始化时引用一个对象后,就不能再引用其他对象;而指针可以在不断地改变指向对象。
• 引用可以直接访问指向对象,指针需要解引用才是访问指向对象。
• sizeof中含义不同,引用结果为引用类型的大小,但指针始终是地址空间所占字节个数(32位平台下,64位下是8byte)
• 指针很容易出现空指针和野指针的问题,引用很少出现,引用使用起来相对更安全一些。

7.inline

• inline修饰的函数叫做内联函数,编译时C++编译器会在调用的地方展开内联函数,这样调用内联函数就需要建立栈帧了,就可以提高效率。
• inline对于编译器而言只是一个建议,也就是说,你加了inline编译器也可以选择在调用的地方不展开,不同编译器关于inline什么情况展开各不相同,因为C++标准没有规定这个。inline适用于频繁调用的短小函数,对于递归函数,代码相对多一些的函数,加上inline也会被编译器忽略。
• C语言实现宏函数也会在预处理时替换展开,但是宏函数实现很复杂很容易出错的,且不方便调
试,C++设计了inline目的就是替代C的宏函数。
• vs编译器 debug版本下面默认是不展开inline的,这样方便调试,debug版本想展开需要设置下以下两个地方。
• inline不建议声明和定义分离到两个文件,分离会导致链接错误。因为inline被展开,就没有函数地址,链接时会出现报错。

#include<iostream>
using namespace std;
inline int Add(int x, int y)
{
    int ret = x + y;
    ret += 1;
    ret += 1;
    ret += 1;
    return ret;
}
int main()
{
// 可 以 通 过 汇 编 观察程序 是 否 展 开
// 有call Add语 句就 是 没 有 展 开,没 有就 是 展 开 了
    int ret = Add(1, 2);
    cout << Add(1, 2) * 5 << endl;
    return 0;
}
#include<iostream>
using namespace std;
// 实 现一个 ADD宏函数的 常见 问 题
//#define ADD(int a, int b) return a + b;
//#define ADD(a, b) a + b;
//#define ADD(a, b) (a + b)
// 正确 的宏实 现
// // // #define ADD(a, b) ((a) + (b))
// 为什 么 不 能加 分 号?
// 为什 么 要 加 外面 的 括 号?
// 为什 么 要 加里面 的 括 号?
int main()
{
    int ret = ADD(1, 2);
    cout << ADD(1, 2) << endl;
    cout << ADD(1, 2)*5 << endl;
    int x = 1, y = 2;
    ADD(x & y, x | y); // -> (x&y+x|y)
    return 0;
}
// F.h
#include <iostream>
using namespace std;
inline void f(int i);
// F.cpp
#include "F.h"
void f(int i)
{
    cout << i << endl;
}
// main.cpp
#include "F.h"
int main()
{
// 链 接错 误:无 法 解析 的 外 部符 号 f(10);
    f(10);
    return 0;
//"void__cdecl f(int)" (?f@@YAXH@Z)
}

8.nullptr

C++中NULL可能被定义为字面常量0,C语言中为(void*)。
nullptr是特殊关键字,它可以隐式转换成其他类型的指针,不能被转换成整数类型。

#include<iostream>
using namespace std;
void f(int x)
{
    cout << "f(int x)" << endl;
}
void f(int* ptr)
{
    cout << "f(int* ptr)" << endl;
}
int main()
{
    f(0);

// 本 想 通 过 f(NULL)调用 指 针 版本 的 f(int* ptr)函数
//,但 是 由 于NULL被 定 义 成0,调用 了f(int x)
//因此与 程序 的 初 衷 相悖 。
    f(NULL);
    f((int*)NULL);
// 编 译报 错 : error C2665: “f”: 2 个 重 载 中 没 有一个 可 以 转 换所 有参数类型
// f((void*)NULL);
    f(nullptr);

    //C++,必须强转
    void* p1 = NULL;
    int* p2 = (int*)p1;

    //使用nullptr
    void* p1 = nullptr;
    int* p2 = p1;
    //C语言有隐式类型转换,可以通过
    void* p1 = NULL;
    int* p2 = p1;
return 0;
}

总结

文章介绍了C++中命名空间的定义和使用,包括定义和冲突解决、使用方式和标准库中的应用,还详细介绍了C++输入输出流、缺省参数、函数重载、引用、内联函数和nullptr等概念及其用法和注意事项。

到此这篇关于新手学习C++的基础知识概况总结的文章就介绍到这了,更多相关学习C++的基础知识内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文