美文网首页
c++内存/引用/函数重载/权限控制

c++内存/引用/函数重载/权限控制

作者: Sheik | 来源:发表于2021-07-27 00:18 被阅读0次

环境:ide:Mac+clion

视频链接:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5

c++内存分区模型:

代码区:存放二进制代码,操作系统进行管理。代码共享只读。
全局区:全局变量/静态变量/常量 ————>字符串常量/全局常量/static
栈区:存放变量/参数值等,编译器负责释放 -->包含局部的const 常量
堆区:程序员进行申请和释放。忘记释放,程序退出的时候,系统进行回收。

注意:局部变量的地址不要返回。

int * func(){
    int value = 10;
    return &value;
}

利用new 来创建堆地址

int * func_new(){
    int * p = new int(10);
    return p;
}

int main(){
    int * p = func_new();
    cout << "地址是:"<<p<<",值是:"<<*p<<endl;
    if (p != NULL){
        delete p;//释放指针
        p = NULL;//一般都需要设置为NULL防止野指针的出现。
    }
    return 0;
}

指针数组:delete 数组

    int *arr = new int[10];
    for (int i = 0; i < 10; i++) {
        arr[i] = i + 100;
    }
    for (int i = 0; i < 10; i++) {
        cout << arr[i] << endl;
    }
    if (arr != NULL) {
        delete[] arr;//释放数组指针
        arr = NULL;//防止野指针出现。
    }

引用:给变量起别名。 模型是:数据类型 &别名 = 原名 这里其实指向的同一个内存。引用必须进行初始化,一旦初始化,就不能进行改变。引用的本质就是指针常量。

    int value = 10;
    int &intValue = value;
    intValue = 20;
    cout <<value <<endl;//20
    cout << intValue << endl;//20
    //一般来讲,我们是不能给常量起别名。例如:
    int &reference = 10;//这个编译错误。
    //但是下面的可以。实际上是 int temp = 10; const int &ref = temp; 的一个转化。
    const int &ref = 10;

函数默认值

//函数默认返回值,如果没有传就是默认值,如果传了就是传的值
int add(int a,int b=20,int c = 30){
    return a+b+c;
}
//在main函数中调用,可以省略后面两个参数,也可以全部加进来。
    cout << add(3)<<endl;//53
    cout << add(3,5)<<endl;//38

函数默认值注意事项:

//函数的声明有参数的默认值,实现就不能有默认值。这里不能有二异性
int func (int a = 10,int b=10);
int func (int a,int b){
    return a + b;
}
//实现有默认值,声明就不能有默认值。 这里不能有二异性
int func1 (int a,int b);
int func1 (int a = 20,int b = 20){
    return a + b;
}

函数的占位符

//函数的占位参数
int func2(int a,int);//最后面的是占位参数
int func2(int,int = 10){//实现的时候也可以有占位参数,这里还可以有默认参数
    cout << "func2"<<endl;
}
int main() {

    int value1 = add(10);
    int value2 = add(10,30);
    int value3 = add(10,30,40);
    cout << "value1:"<< value1  << " value2:"<< value2<< " value3:"<<value3<<endl;

    cout<<"func:"<< func(2)<<endl;
    cout<<"func1:"<< func1(4)<<endl;
    func2(1);
    return 0;
}

函数重载

1.同一个作用域下。
2.函数名相同,参数不同,参数位置不同。 提高复用性
3.返回值不同,不是重载。

void func() {
    cout << "func run" << endl;
}

void func(int a) {
    cout << "func int a run" << endl;
}

void func(double a) {
    cout << "func double a run" << endl;
}

void func(double a, int b) {
    cout << "func double a,int b  run" << endl;
}

void func(int a, double b) {
    cout << "func int a,double b  run" << endl;
}

函数重载坑:

//引用加不加const 是可以进行重载。
void func(int &a){
    cout << "int &a"<<endl;
}
void func(const int &a){
    cout << "const int &a"<<endl;
}

c++三大特性: 封装/继承/多态

类的学习

#define PI 3.14
class Circle{//class 关键字+ 类名
public:
    //属性
    double r;
    //行为
    double calculateZC() {
        return 2 * PI * r;
    }

};
//在main中的调用:
    Circle circle;
    circle->r = 2;
    double zc = circle->calculateZC();
    cout << zc<< endl;//12.56

类中的三种权限:

1.public 公共权限 类内可以访问,类外面也可以访问。
2.protected 保护权限 类内可以访问,类外不可以访问。子类可以访问。
3.private 私有权限 类内可以访问,类外不可以访问。子类不可以访问。

class Person {
public:
    string name;
protected:
    string car;
private:
    int passwd;

public:
    void show() {//类内都可以进行访问
        // name = "张三";
        car = "摩托";
        passwd = 123;
        cout << name << " " << car << " " << passwd << endl;
    }
};
//在main中进行访问:
    Person person;
    person.name = "赵老六";
  //person.car = "奔驰"; 类外无法访问
  //person.password = 567;类外无法访问
    person.show();

相关文章

网友评论

      本文标题:c++内存/引用/函数重载/权限控制

      本文链接:https://www.haomeiwen.com/subject/atbgmltx.html