C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++入门基础

C++入门基础知识完整版

作者:琳琳大王不爱吃

C++ 基础入门主要包括‌环境搭建‌、‌第一个程序‌、‌核心语法‌(命名空间、输入输出、函数、引用等)以及‌学习资源‌,建议从 C++11 及之后的标准开始学习

前言

C++是在C语言的基础上发展而来的一种面向对象 编程语言,它在保留C语言强大功能的同时,引入了许多新的特性。比如面向对象编程(OOP):类, 封装, 继承, 多态四大件,引用(Reference),模板(Template),命名空间(Namespace),异常处理(Exception Handling),标准模板库(STL)

本文讲解C++中的命名空间、C++输⼊&输出、缺省参数、函数重载、引用、内联函数、nullptr 等入门核心知识点

一、第一个C++程序

C++兼容C语⾔绝⼤多数的语法,所以C语⾔实现的hello world依旧可以运⾏,C++中需要把定义⽂件代码后缀改为.cpp,vs编译器看到是.cpp就会调⽤C++编译器编译,linux下要⽤g++编译,不再是gcc

//test.c
#include <stdio.h>
int main()
{
	printf("hello world\n");
	return 0;
}

当然C++有⼀套⾃⼰的输⼊输出,严格说C++版本的hello world应该是这样写的。

//test.cpp
#include <iostream>
using namespace std;
int main()
{
	cout << "hello world" << endl;
	return 0;
}

二、命名空间 namespace

1. 命名空间的价值

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

2. 命名空间的定义

3. 命名空间的使用

  1. 指定访问(项目推荐):test::rand
  2. 单个成员展开:using test::rand;
  3. 全局展开(练习可用,项目不推荐):using namespace std;

使用举例:

指定命名空间

// 指定命名空间访问
int main()
{
    printf("%d\n", N::a);
    return 0;
}

单个成员展开

// using将命名空间中某个成员展开
using N::b;
int main()
{
    printf("%d\n", N::a);
    printf("%d\n", b);
    return 0;
}

全局展开

// 全局展开
using namespce N;
int main()
{
    printf("%d\n", a);
    printf("%d\n", b);
    return 0;
}

三、C++输⼊&输出

  1. 是 Input Output Stream 的缩写,是标准的输⼊、输出流库,定义了标准的输⼊、输出对象。
  2. std::cin 是 istream 类的对象,它主要⾯向窄字符(narrow characters (of type char))的标准输⼊流。
  3. std::cout 是 ostream 类的对象,它主要⾯向窄字符的标准输出流。
  4. std::endl 是⼀个函数,流插⼊输出时,相当于插⼊⼀个换⾏字符加刷新缓冲区。
  5. <<是流插⼊运算符,>>是流提取运算符。
  6. cout/cin/endl等都属于C++标准库,C++标准库都放在⼀个叫std(standard)的命名空间中,所以要通过命名空间的使⽤⽅式去⽤他们。

对比C语言的优势:使⽤C++输⼊输出更⽅便,不需要像printf/scanf输⼊输出时那样,需要⼿动指定格式,C++的输⼊输出可以⾃动识别变量类型

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
	int a = 0;
	double b = 0.1;
	char c = 'x';
	//printf("%d %lf %c\n", a, b, c);
	//scanf("%d %lf %c", &a, &b, &c);
	//printf("%d %lf %c\n", a, b, c);
	//可以自动识别变量的类型
	cout << a << " " << b << " " << c << endl;
	cin >> a;
	cin >> b >> c;
	cout << a << " " << b << " " << c << endl;
	return 0;
}

四、缺省参数

#include <iostream>
using std::cout;
using std::endl;
//全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
//半缺省
void Func2(int a, int b = 20, int c = 30) //必须从右往左连续设置,不能间隔
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
	Func1(); //没有传参时,使⽤参数的默认值
	Func1(1); //传参时,使⽤指定的实参
	Func1(1, 2);
	Func1(1, 2, 3);
	//Func2(); //报错
	Func2(100);
	Func2(100, 200);
	Func2(100, 200, 300);
	return 0;
}

五、函数重载

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

#include <iostream>
using std::cout;
using std::endl;
//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(int left, int right)" << endl;
	return left + right;
}
//2.参数个数不同
void f()
{
	cout << "f()" << endl;
}
void f(int a)
{
	cout << "f(int a)" << endl;
}
//3.参数类型顺序不同(类型不同)
void f(int a, char b)
{
	cout << "f(int a, char b)" << endl;
}
void f(char a, int b)
{
	cout << "f(char a, int b)" << endl;
}
//返回值不同不能作为重载条件,因为调用时也无法区分
//void fxx()
//{
//
//}
//
//int fxx()
//{
//	return 0;
//}
//下⾯两个函数构成重载
//f()但是调⽤时,会报错,存在歧义,编译器不知道调⽤谁
void f1()
{
	cout << "f1()" << endl;
}
void f1(int a)
{
	cout << "f1(int a)" << endl;
}
int main()
{
	Add(10, 20);
	Add(10.1, 20.2);
	f();
	f(10);
	f(10, 'a');
	f('a', 10);
	return 0;
}

六、引用

概念:引用是已存在变量的别名,不开辟新内存,和原变量共用同一块空间,符号 & 复用取地址符。

类型& 引用别名 = 引用对象;
#include <iostream>
using std::cout;
using std::endl;
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;
}

引用的特性

#include<iostream>
using std::cout;
using std::endl;
int main()
{
	int a = 10;
	// 编译报错:“ra”: 必须初始化引⽤
	//int& ra;
	int& b = a;
	int c = 20;
	// 这⾥并⾮让b引⽤c,因为C++引⽤不能改变指向,
	// 这⾥是⼀个赋值
	b = c;
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	return 0;
}

引用的使用

#include <iostream>
using std::cout;
using std::endl;
//引用
void Swap(int& rx, int& ry)
{
	int tmp = rx;
	rx = ry;
	ry = tmp;
}
//指针
void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
int main()
{
	int x = 0, y = 1;
	cout << x << " " << y << endl;
	Swap(x, y); //使用引用
	//Swap(&x, &y); //使用指针
	cout << x << " " << y << endl;
	return 0;
}

const引用

#include <iostream>
using std::cout;
using std::endl;
int main()
{
	const int a = 10;
	//这里的引用是对a访问权限的放大,不可以
	//int& ra = a; //报错
	//这样才可以
	const int& ra = a;
	//这里的引用是对b权限的缩小,可以
	int b = 20;
	const int& rb = b;
	return 0;
}

指针和引用的关系

特性引用指针
内存占用不开辟空间占用地址字节(32 位 4 字节 / 64 位 8 字节)
初始化必须初始化可不初始化
指向修改不可改绑定可随意改变指向
安全系数无空引用、野引用易出现空指针、野指针

七、内联函数 inline

#include <iostream>
using std::cout;
using std::endl;
//宏
//#define Add(a, b) ((a) + (b))
//内联函数
inline int Add(int x, int y)
{
	int ret = x + y;
	return ret;
}
int main()
{
	int ret = Add(1, 2);
	cout << Add(1, 2) * 5 << endl;
	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()
{
	// 链接错误:无法解析的外部符号 "void __cdecl f(int)" (?f@@YAXH@Z)
    f(10);
    return 0;
}
![内联函数_2](D:/CSDN图片/8_31/内联函数_2.png)// F.h
#include <iostream>
using namespace std;
inline void f(int i)
{
    cout << i << endl;
}
// F.cpp
//不写
// main.cpp
#include "F.h"
int main()
{
	// 链接错误:无法解析的外部符号 "void __cdecl f(int)" (?f@@YAXH@Z)
    f(10);
    return 0;
}

八、nullptr 空指针

NULL实际是一个宏,在传统的C头文件(stddef.h)中,可以看到如下代码:

#ifndef NULL
    #ifdef __cplusplus
        #define NULL 0
    #else
        #define NULL ((void *)0)
    #endif
#endif
![nullper空指针](D:/CSDN图片/8_31/nullper空指针.png)#include<iostream>
using std::cout;
using std::endl;
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*)函数,但是由于NULL被定义成0,调用了f(int x),因此与程序的初衷相悖。
    f(NULL);
    f((int*)NULL);
    // 编译报错:error C2665: “f”: 2 个重载中没有一个可以转换所有参数类型
    // f((void*)NULL);
    f(nullptr);
    return 0;
}

到此这篇关于C++入门基础知识完整版的文章就介绍到这了,更多相关C++入门基础内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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