C++语言学习

一、C和C++的区别

// #include  // C语言的标准支持
#include  // C++标准支持  C++的与众不同
using namespace std; // 命名空间 C++ 的特性 (Java语言的内部类)

int main() {
    // C++语言面向对象 + 标准特性
    // C语言面向过程,函数+结构体
    // C++里面可以运行C语言,可以调用C语言,反之 就不行C语言无法运行C++
    printf("降龙十八掌(C版)\n");

    // std::cout << "C++语言的学习" << std::endl;
    cout << "C++语言的学习" << endl; // 因为你前面引入了命名空间,省略std::

    // endl == \n  都是换行的含义一样
    // << 操作符重载,后面会讲
    cout << "擒龙功" << endl;
    cout << "铁头功\n"
         << "金刚腿\n"
         << "铁布衫\n";
    return 0;
}
#include 
// C语言的常量,其实是个 假常量,可以使用指针修改
int main() {
    const int number = 100;
    // number = 200;
    int * numP = &number;
    *numP = 10000;
    printf("%d\n", number);
    return 0;
}

======================
#include 

// C++语言的常量,其实是真常量,不能修改
int main() {
    const int number = 100;
    // number = 200;
    // 我的编译器,编译不通过, 有的编译器,编译通过,但是运行报错(结论:就不能改)
    // int * numP = &number;
    // *numP = 10000;
    printf("%d\n", number);
    return 0;
}

二、引用

#include 
using namespace std;
// 互换两个数
// 指针取地址 互换  C语言第一节课的内容
// 接收number1/number2的地址,取改地址的值,来完成的互换
void numberChange(int * number1, int * number2) {
    int temp = 0;
    temp = *number1;
    *number1 = *number2;
    *number2 = temp;
}

// C++提倡的引用
void numberChange2(int & number1, int & number2) {

    // 如果不采用引用,main numberChange2 内存地址是不一样的
    // 如果采用引用,main numberChange2 内存地址是一样的,为什么呢?
    cout << "numberChange2 " << "n1地址:" << &number1 << " , n2地址:" << &number2 <
引用01.png

引用02.png

三、常量引用

#include 
#include 
using namespace std;
// 代码的统一性
typedef struct {
    char name[20];
    int age;
}Student;

// 常量引用:Student不准你改 == const Student &
// 插入数据库,Student的信息给插入数据库
void insertStudent(const Student & student) {
    // strcpy(student.name, "李元霸"); 不能这样修改
    Student student2 = {"刘奋", 43};
    // student = student2; 不能这样修改
    // 只读的了,可以安心插入数据库了
    cout << student.name << "," << student.age << endl;
}
int main() {
    // 用户提交的Student数据
    Student student = {"张无忌", 30};
    insertStudent(student);
    return 0;
}

四、面向对象

//Student.h
#include 
using namespace std;
// Student.h 头文件  只写声明,不写实现
class Student {
private:
    char *name;
    int age;
public:
    void setName(char *name);
    void setAge(int age);
    char *getName();
    int getAge();
};
//Student.cpp
#include "Student.h"
// 根据头文件 ,写实现
// 和实现头文件那个函数,没有任何关系,相当于另外一个函数
/*void setAge(int age) {
}*/
//头文件的实现函数
void Student::setName(char *name) {
    // C++对象指向的是一个指针
    // -> 调用一级指针的成员
    this->name = name;
}
void Student::setAge(int age) {
    this->age = age;
}
char *Student::getName() {
    return this->name;
}
int Student::getAge() {
    return this->age;
}
//main.cpp
#include "Student.h"
int main() {
    //栈空间
    Student student;
    student.setName("张无忌");
    student.setAge(22);
    cout << "name:" << student.getName() << ",age:" << student.getAge() << endl;

    //堆空间
    Student * student1 = new Student();
    student1->setName("杨过");
    student1->setAge(22);
    cout << "name:" << student1->getName() << ",age:" << student1->getAge() << endl;

    //堆空间手动释放
    if (student1){
        delete student1;
        student1 = NULL;
    }
    return 0;
}

五、命名空间

#include 

// 声明std,我们的main函数就可以直接使用里面的成员,不需要使用 std::
using namespace std; // C++自己的命名空间 (C# .net 命名空间)

// 自定义命名空间
namespace derry1 {
    int age = 33;
    char * name = "Derry猛男1";

    void show() {
        cout << "name:" << name << ", age:" << age << endl;
    }

    void action() {
        cout << "derry1 action" << endl;
    }
}

// TODO ------ 命名空间里面重复的函数
// 自定义命名空间
namespace derry2 {
    void action() {
        cout << "derry2 action" << endl;
    }
}

// TODO ------ 小概率会遇到的情况,命名空间的嵌套
// 自定义命名空间
namespace derry3 {
    namespace derry3Inner {
        namespace derry3Inner1 {
            namespace derry3Inner2 {
                namespace derry3Inner3 {
                    void out() {
                        cout << "爱恨情仇人消瘦,悲欢起落人寂寞" << endl;
                    }
                }
            }
        }
    }
}

// 声明 命名空间 全局使用
// using namespace derry1;

int main() {
    cout << "命名空间" << endl;

    // 声明写的 命名空间  局部使用
    using namespace derry1;

    int ageValue = derry1::age; // 方式1 使用 刚刚声明的命名空间
    derry1::show(); // 使用 刚刚声明的命名空间

    ageValue = age; // 方式2 直接去引出来 ::
    show(); // 直接去引出来 ::


    // TODO ------ 命名空间里面重复的函数
    using namespace derry2;
    // action(); 重复加::
    derry1::action();
    derry2::action();

    // TODO ------ 小概率会遇到的情况,命名空间的嵌套

    // 第一种方式 先声明命名空间  再使用
    using namespace derry3::derry3Inner::derry3Inner1::derry3Inner2::derry3Inner3;
    // 再使用
    out();

    // 第二种方式 直接使用
    derry3::derry3Inner::derry3Inner1::derry3Inner2::derry3Inner3::out();
    return 0;
}

你可能感兴趣的:(C++语言学习)