08 C++ 重要函数原理

Android Ndk 学习笔记(目录)

1 命名空间

/**
 * 可以通过 namespace  关键词 直接定义 命名空间
 */
namespace custom{
    char * name ;
    int age ;
    void action(){
        cout << "custom 的 action" << endl ;
    }
    void action2(){
        cout << "custom 的 action2" << endl ;
    }
}

namespace custom2{

    void action(){
        cout << "custom2 的 action" << endl ;
    }
    void action3(){
        cout << "custom2 的 action3" << endl ;
    }
}

// 可以定义全局的 命名空间
using namespace custom 
void function01(){
    // 可以定义局部的 命名空间
    using namespace  custom2;
    //当两个命名空间的方法名冲突时 即使已经定义过了命名空间了 ,也需要戴上命名空间的名字才可以使用
    custom::action();
    custom2::action();
    // 可以 直接使用
    action2() ;
}

在这里插入图片描述

/*
 * 嵌套命名空间
 */
namespace custom3{
    namespace custom4{
        namespace custom5{
            namespace custom6{
                void action4(){
                    cout << "custom2 的 action" << endl ;
                }
            }
        }
    }
}
/*
 * 嵌套命名空间的使用
 */
void function03(){

    //01 先定义 后使用
    using namespace custom3::custom4::custom5::custom6;
    action4();

    // 直接使用
    custom3::custom4::custom5::custom6::action4();

}

2 构造函数

Sutdent.h 文件

//
// Created by admin on 2021/7/27.
//

#ifndef C___STUDENT_H
#define C___STUDENT_H


class Student {

public:
    Student();
    Student(char * name);
    Student(char * name ,int age);
    ~Student();//析构函数

private: // 下面的代码(成员和函数),都是私有
    char * name;
    int age;

public: // 下面的代码(成员和函数),都是公开
    // set get
    void setAge(int age); // 声明函数
    void setName(char * age); // 声明函数
    int getAge(); // 声明函数
    char * getName(); // 声明函数

};


#endif //C___STUDENT_H

Student.cpp

//
// Created by admin on 2021/7/27.
//
#include <iostream>
#include "Student.h"
#include <string.h>

using namespace std;


Student::Student() {
    cout << "无参构造函数" <<endl;
}

Student::Student(char * name) {
    this->name = name;
    cout << "一个参数的构造函数" <<endl;
}

// C++的第二种写法
//Student::Student(char * name) :name(name){}

Student::Student(char * name ,int age) {
//    第一种写法 直接赋值
//    this->name = name;
//    第二种写法 开辟堆空间
//    this->name = (char *) malloc(sizeof(Student));
//    this->name = name;
//    第三种写法 开辟堆空间同第二种
    this->name = static_cast<char *>(malloc(sizeof(Student)));
    strcpy(this->name,name);

    this->age = age;
    cout << "两个参数的构造函数" <<endl;
}

// C++的第二种写法
//Student::Student(char * name ,int age) :name(name) ,age(age){}

Student::~Student(){

    //  Student::Student(char * name ,int age) 中的 name 需要回收
    if (this->name) {
        free(this->name);
        this->name = NULL ;
    }

    cout << "C++ 执行delete的时候 调用该函数,进行回收操作" <<endl;
};

void Student::setAge(int age) { // 实现函数
    // C++对象指向的是一个指针
    // -> 调用一级指针的成员
    this->age = age;
}

void  Student::setName(char * name) { // 实现函数
    this->name = name;
}
int Student::getAge() { // 实现函数
    return this->age;
}
char * Student:: getName() { // 实现函数
    return this->name;
}

3 拷贝函数

Student.h
    Student(const Student & student);
Student.cpp
// 拷贝构造函数,它默认有,我们看不到,一旦我们写拷贝构造函数,会覆盖她
Student::Student(const Student & student){
    // 我们自己赋值
    // 为什么要自己赋值,自己来控制,就可以 例如:-10
    this->name = student.name;
    this->age = student.age - 10;
    cout << "copy  " << &student << endl;
}

    Student  st1 = {"zasd ",23};
    Student  st2 = st1;   // 调用 自己的拷贝函数
    cout << " st1 " << st1.getName() << st1.getAge() << endl;
    cout << " st2 " << st2.getName() << st2.getAge() << endl;
   
E:\C\Project\C++\cmake-build-debug\C__.exe
两个参数的构造函数
copy  0x7bfdd0
 st1 zasd 23
 st2 zasd 13

//Process finished with exit code -1073740940 (0xC0000374)
    Student  st1 = {"zasd ",23};
    Student  st2;
    st2 = st1 ;// 不调用 自己的拷贝函数    只是赋值
    cout << " st1 " << st1.getName() << st1.getAge() << endl;
    cout << " st2 " << st2.getName() << st2.getAge() << endl;
   
E:\C\Project\C++\cmake-build-debug\C__.exe
两个参数的构造函数   // st1 的
无参构造函数        // st2 的
 st1 zasd 23
 st2 zasd 23

Process finished with exit code -1073740940 (0xC0000374)

4 指针常量 常量指针 常量指针常量

// 常量指针
    const int * numberP1 = &number;
    // *numberP1 = 100; // 报错,不允许去修改【常量指针】存放地址所对应的值
    numberP1 = &number2; // OK,允许重新指向【常量指针】存放的地址

    //  指针常量
    int* const numberP2 = &number;
    *numberP2 = 100; // OK,允许去修改【指针常量】存放地址所对应的值
    // numberP2 = &number2; // 报错,不允许重新指向【指针常量】存放的地址

    // 常量指针常量
    const int * const numberP3 = &number;
    // *numberP3 = 100; // 报错,不允许去修改【常量指针常量】存放地址所对应的值
    // numberP3 = &number2; // 报错,不允许重新指向【常量指针常量】存放的地址
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值