10.关于C++

C++

1.C++概述

C++之父
	本贾尼·斯特劳斯特卢普
C++ 是基于 C语言的增强!!!引入的面向对象思想,更多的模型,函数,容器。。。
C 和 C++ 都是系统级别的语言,操作系统都是基于 C 和 C++ 实现的!!!

C++ 重点知识
	1.C++ 相较于 C 语言增强内容【琐碎知识点】
	2.C++ 面向对象【封装,继承,多态】
	3.C++ STL 标准库【各种函数,各种功能,各种容器】

2.C++相对于 C 语言的增强

2.1 C++ 第一行代码

代码实现

// 需要导入的 C++ 基础头文件,对应标准输入输出操作
#include <iostream>

/*
需要使用命名空间 namespace
使用 std 可以使用C++ 中的标准输入 cin 和标准输出 cout 相关内容
*/
using namespace std;

int main(int argc, char const *argv[])
{
    /*
    cout 对应标准输出操作,将数据内容展示到控制台中
    << 连接使用的运算符,中间是拼接展示内容
    endl 刷新输出缓冲区,可以将数据内容直接展示到控制台,
    同时提供换行操作
    */
    cout  << "第一行代码~" << endl;
    return 0;
}

编译流程

/*
编译使用的工具是
	g++
格式:
	和之前的 C 语言编译格式一致
	g++ 目标文件.cpp
	得到a.out 可执行文件
*/

2.2 C++补充类型

C语言基本数据类型

整型: short int long
浮点型: float double
字符型: char

C++补充bool 类型,针对于真假关系

bool
/*
另外补充两个关键字
*/
true (1)
false (0)
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    bool ret = false;

    /*
    true 对应 1
    false 对应 0
    */
    cout << "ret : " << ret << endl;
    cout << "sizeof(bool) : " << sizeof(bool) << endl;

    if (ret) 
    {
        cout << "到点了!!!" << endl;
    }
    else 
    {
        cout << "下课了!!!" << endl;
    }
    return 0;
}

2.3作用域运算符

:: 格式,提示后续内容的归属权
#include <iostream>

using namespace std;
/*
全局变量
*/
int a = 10;

int main(int argc, char const *argv[])
{
    /*
    函数内部的局部变量
    */
    int a = 200;

    /*
    就近原则,导致全局变量 a 被覆盖 无法获取到全局变量 a 对应的数据
    */
    cout << "a : " << a << endl;
    /*
    解决方案:
        可以使用作用域运算符 :: 解决问题
        告知计算机需要获取全局变量 a 
    可以认为当前 a 全局变量是在整个文件中有效 :: 对应的作用域
    是整个 C++ 文件
    */
    cout << "::a : " << ::a << endl;
    return 0;
}
#include <iostream>

int main(int argc,char * argv[])
{
    /*
    目前代码中没有使用命名空间 std
    直接使用 cout cin endl 报错,无法使用,因为以上内容都属于 std 中的组成部分,属于 std 作用域范围以内
    如果不需要通过引入 std 命名空间的方式,需要加入 std 命名空间前缀,同时使用 :: 作用域运算符,引入 cout 内容
    */
    std::cout << "你好!" << std::endl;
    return 0;
}

2.4命名空间 namespace

2.4.1 命名空间基本内容和开放性

自定义命名空间可以有变量定义,函数定义,类型声明

命名空间是开放性的,后续代码中可以通过命名空间的方式补充内容,和前置声明的同命名空间一致

注意使用作用域运算符来确定函数的归属权

#include <iostream>

using namespace std;

/*
自定义命名空间可以有变量定义,函数定义,类型声明..
*/
namespace A 
{
    // 命名空间 A 中定义的变量 a 
    int a = 10;
    void test()
    {
        cout << "命名空间 A 中的 test 函数" << endl;
    }
}

/*
命名空间是开放性的,后续代码中可以通过命名空间的方式
补充内容,和前置声明的同命名空间一致!
*/

namespace A 
{
    int b = 20;
}

void test();

int main(int argc, char const *argv[])
{
    cout << "a : " << A::a << endl;
    cout << "b : " << A::b << endl;

    // 没有使用作用域运算符,默认是当前文件中或者其导入的其他文件中搜索
    // 目标 test 函数
    test();

    /*
    可以利用作用域运算符,选择当前 test 函数是命名空间 A 中的 test 函数
    */
    A::test();

    return 0;
}

void test()
{
    cout << "当前 C++ 文件中的 test 函数" << endl;
}
2.4.2多个命名空间操作

当有多个自定义命名空间时,注意

​ 告知编译器,当前代码使用命名空间 B
using namespace B;

​ 想要使用以上命名空间中的 变量,需要不同的命名空间 +::
作用域运算符进行明确区分

#include <iostream>

using namespace std;

namespace A
{
    int a = 10;
}

namespace B
{
    int a = 200;
}

// 告知编译器,当前代码使用命名空间 B
using namespace B;

int main(int argc, char const *argv[])
{
    /*
    想要使用以上命名空间中的 a 变量,需要不同的命名空间 +::
    作用域运算符进行明确区分
    */
    cout << "A::a : " << A::a << endl;
    cout << "B::a : " << B::a << endl;

    /*
    在代码之前使用using 关键字使用了命名空间,相当于真个代码中
    的可以直接获取对应的命名空间的内容,省去命名空间名称

    当前变量 a 明确为 B 命名空间中的变量
    */
   cout << "a : " << a << endl;
    return 0;
}
2.4.3命名空间函数定义和实现分离
#include <iostream>

using namespace std;

namespace A
{
    // 函数声明
    void test1();
    void test2();
}

using namespace A;

int main(int argc, char const *argv[])
{
    test1();
    test2();
    return 0;
}

/*
函数实现
    在函数名和返回值之间,需要使用作用域运算符,明确当前
    函数归属于哪一个命名空间,后续代码中作用域运算符之有
    可能是结构体(struct),类(class)
*/
void A::test1()
{
    cout << "namespace A test1 function" << endl;
}

void A::test2()
{
    cout << "namespace B test function" << endl;
}
2.4.4 匿名命名空间

当前命名空间没有名称,可以认为

​ 1.当前命名空间为匿名命名空间

​ 2.当前命名空间有且只能在当前 C++ 文件中使用

匿名命名空间使用的时候 加 :: 就可以

#include <iostream>

using namespace std;

/*
当前命名空间没有名称,可以认为
    1. 当前命名空间为匿名命名空间
    2. 当前命名空间有且只能在当前 C++ 文件中使用
*/
namespace
{
    int a = 10;
}

int main(int argc, char const *argv[])
{
    /*
    当前代码中有且只有一个 a 变量,可以直接使用命名空间中的 a
    */
    cout << "a : " << a << endl;

    int a = 2000;
    // 定义了一个局部变量 a,按照基本的就近原则,直接使用是局部变量 a
    cout << "a : " << a << endl;
    
    // 如果想要使用匿名命名空间中的 a ,需要加入 :: 作用域运算符
    cout << "::a : " << ::a << endl;
    return 0;
}#include <iostream>

using namespace std;
2.4.5 命名空间别名
#include <iostream>

using namespace std;

namespace njvkdfnvifdvfij
{
    int a = 10;
}

int main(int argc, char const *argv[])
{
    // 命名空间名称过长,会导致使用非常的麻烦!
    cout << "a: " << njvkdfnvifdvfij::a << endl;

    // 给予当前命名空间起别名,简化命名空间形式
    namespace nj = njvkdfnvifdvfij;
    cout << "a: " << bt::a << endl;

    return 0;
}

2.5结构体增强

2.5.1 C++结构体语法特征

C 语言和 C++ 结构体的区别

​ 1.C++ 结构体的数据类型名称直接就是 struct 关键字之后的名称;C语言结构体的名称是 struct 加结构体名

例如:

​ C++ struct Student 数据类型名称是 Student

​ C语言 struct student 数据类型名称为 struct student

​ 2.C++结构体允许定义函数/声明函数,C语言不允许

#include <iostream>

using namespace std;

struct Student
{
    int id;
    char name[16];
    int age;

    // C++ 支持结构体声明函数,可以认为当前函数属于当前结构体
    void test()
    {
        cout << "测试" << endl;
    }
};

/*
C++ 中结构体的数据类型是 struct 关键字之后的名称
通常情况下按照大驼峰命名法规则
*/
int main(int argc, char const *argv[])
{
    // 直接可以利用结构体 struct 关键字之后的名称
    // 作为数据类型名称
    Student stu = {1, "James", 16};

    cout << "ID : " << stu.id << endl;
    cout << "Name : " << stu.name << endl;
    cout << "Age : " << stu.age << endl;

    // 结构体声明实现的函数,通过结构体变量或者结构体指针调用
    stu.test();

    return 0;
}
2.5.2 C++结构体案例
#include <iostream>
/*
相当于引入了 <string.h> C 语言头文件
推荐使用 <cstring>
*/
#include <cstring>
/*
cstdlib 相当于导入 stdlib.h
*/
#include <cstdlib>

using namespace std;

struct Student
{
    int id;
    char name[20];
    int age;

    void set_id(int id);
    int get_id();

    void set_name(char *name);
    char *get_name();

    void set_age(int age);
    int get_age();
};

/*
外部实现结构体声明函数,需要使用作用域运算符,明确当前函数的归属
是哪一个结构体
    void Student::set_id(int id) 实现结构体 Student 的函数
*/
void Student::set_id(int id)
{
    /*
    this->id 明确告知编译器,当前操作的 id
    是当前调用函数结构体变量/指针的成员变量 id 而不是
    参数变量 id
    */
    this->id = id;
}
int Student::get_id() 
{
    return id;
}

void Student::set_name(char *name) 
{
    strcpy(this->name, name);
}
char *Student::get_name() 
{
    return name;
}

void Student::set_age(int age) 
{
    this->age = age;
}

int Student::get_age() 
{
    return age;
}

int main(int argc, char const *argv[])
{
    Student * stu = (Student *)calloc(1, sizeof(Student));    

    /*
    通过结构体指针操作结构体中的函数
    利用 set 相关函数,给予当前结构体数据进行赋值操作

    stu->id = 10;
    */
    stu->set_id(10);
    stu->set_name("James");
    stu->set_age(16);

    cout << "ID : " << stu->get_id() << endl; 
    cout << "Name : " << stu->get_name() << endl; 
    cout << "Age : " << stu->get_age() << endl; 

    free(stu);

    stu = NULL;
    return 0;
}
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值