美文网首页c/c++
c++基础—引用和函数

c++基础—引用和函数

作者: Peakmain | 来源:发表于2019-02-17 10:18 被阅读3次

c和c++的区别

  • 1.c++中可以混编c代码,也即是说可以在c++中写c代码,也可以调用
  • 2.c++面向对象(java) ,c面向过程的
  • 3.很多开源框架大部分都是用c++写的

c和c++打印

#include <iostream>
void main(){
    //printf("Hello world");//c的打印
    //c++的打印  #include <iostream>
    //  <<操作符重载
    std::cout << "Hello World" << std::endl;

    getchar();
}

常量

void main(){
    const int number = 10;
    /*int *numberP = &number;//不能通过地址去修改值,但是某些编译器上面可以通过,但是也不能修改值,c中可以修改,所以在c中是个伪命题
    numberP = 30;*/
    getchar();
}   

引用

using namespace std;
/*void swap(int* number1, int* number2){
    int temp = 0;
    temp = *number1;
    *number1 = *number2;
    *number2 = temp;
}*/
//引用 ,其实是地址赋值,可以看成是同一块内存上的另一个变量
void swap(int &number1, int & number2){
    int temp = 0;
    temp = number1;
    number1 = number2;
    number2 = temp;
}
void main(){
    //交换值
    int number1 = 10;
    int number2 = 20;
    swap(number1, number2);
    cout << "number1 =" << number1 << ",number2=" << number2 << endl;
    getchar();
}

常量引用

using namespace std;
typedef struct
{
    char name[20];
    int age;
}Student;

void insertStu(const Student &stu){// stu 不想改 常量引用
    //有const就不可以修改 stu 的值 ,没有的话就可以修改
    //strcpy(stu.name,"Jack");
    // 就变成了只读
    cout << stu.name << "," << stu.age << endl;
}

void main(){
    Student stu = { "Pekamain", 20 };
    insertStu(stu);
    getchar();
}

函数重载和默认参数

c不允许函数重载,对于Boolean类型c中0则为false,非0则为true

函数重载

int add(int number1, int number2){
    return number1 + number2;
}
int add(int number1, int number2,int number3){
    return number1 + number2+number3;
}

默认参数

int add(int number1, int number2=1000){
    return number1 + number2;
}

类的初探

class Student{
private://私有
    char *name;
    int age;
public://共有
    void setAge(int age){
        this->age = age;
    }
    void setName(char*name){
        this->name = name;
    }
    int getAge(){
        return age;
    }
    char* getName(){
        return this->name;
    }
};
void main(){
    Student stu;
    stu.setAge(24);
    stu.setName("Peakmain");
    cout << "name=" << stu.getName() << ",age=" << stu.getAge() << endl;
    getchar();
}

我们都知道真正的开发过程中我们的 cpp 或者 c 文件,最终 都是以dll 或者 so 库供调用者使用,所以为了确保类能够正常被调用,我们一般需要定义 .h 头文件,这时候我们可以对以上代码进行修改,首先定义一个Student.h文件

class Student{
private://私有
    char *name;
    int age;
public:
    void setAge(int age);
    void setName(char*name);
    int getAge();
    char* getName();
};

随后定义一个Student.cpp文件,思路很简单引入.h文件,然后重写.h文件里面的方法

#include "Student.h"

void Student::setName(char*name){
    this->name = name;
}
void Student::setAge(int age){
    this->age = age;
}
int Student::getAge(){
    return age;
}
char* Student::getName(){
    return this->name;
}

使用的时候就可以直接引入.h,调用方式不变

#include"Student.h"

构造函数

修改之前的Student类

class Student{
public:
    Student(){//空参数的构造函数
        cout << "空参数的构造函数" << endl;
    }
    Student(char*name){//一个参数的构造函数
        cout << "一个参数的构造函数" << endl;
        this->name = name;
    }
    Student(char*name):Student(name,0){//调用两个参数的构造函数,结果是先会调用两个参数的构造函数才会执行当前的构造函数
        cout << "一个参数调用两个参数的构造函数" << endl;
        
    }
    Student(char*name,int age){//两个参数的构造函数
        cout << "两个参数的构造函数" << endl;
        this->name = name;
        this->age = age;
    }
private://私有
    char *name;
    int age;
public:
    void setAge(int age){
        this->age = age;
    }
    void setName(char*name){
        this->name = name;
    }
    int getAge(){
        return age;
    }
    char* getName(){
        return this->name;
    }
};

直接调用有以下几个方式

void main(){
    //1.默认是空参构造函数
    /*Student stu;
    stu.setAge(24);
    stu.setName("Peakmain");*/

    //2.一个参数的构造函数
    /*Student stu("Peakmain");*/

    //3.两个参数的构造函数
    /*Student stu("Peakmain", 24)*/;

    /*cout << "name=" << stu.getName() << ",age=" << stu.getAge() << endl;*/

    //4.用new关键字
    /*Student *stu = new Student("Peakmain");
    cout << "name=" << stu->getName() << ",age=" << stu->getAge() << endl;*/

    //5.用malloc的方式,结果没有调用空参的构造函数
    Student *stu = (Student *)malloc(sizeof(Student));
    stu->setAge(24);
    stu->setName("Peakmain");
    cout << "name=" << stu->getName() << ",age=" << stu->getAge() << endl;
    getchar();
}

析构函数

析构函数,如果有在对象内部开辟堆内存,可以在析构函数中释放对象
修改Student构造函数和添加析构函数方法

Student(char*name, int age){//两个参数的构造函数
        cout << "两个参数的构造函数" << endl;
        this->name = (char*)malloc(sizeof(char)*100);
        strcpy(this->name, name);
        this->age = age;
    }
~Student(){
        //对象被回收的时候调用
        //释放内存
        cout << "析构造函数" << endl;
        free(this->name);
        this->name = NULL;
}

调用:必须调用delete才会调到析构函数中

void main(){
    Student *stu = new Student("Peakmain");
    cout << "name=" << stu->getName() << ",age=" << stu->getAge() << endl;
    delete(stu);
    //java对象被回收会调用finalize
    getchar();
}

malloc、free、new、delete区别

  • malloc、free、new、delete都是开辟内存和释放内存
  • malloc和free是一组,不会去调用构造函数和析构函数
  • new 和delete是一组,会去调用构造函数和析构函数
  • 如果用了new,那么一定要记得delete去释放内存

拷贝函数

首先在之前的Student类中添加拷贝函数

    //拷贝函数,对象会有一个默认的拷贝函数,用来对象之间的赋值
    Student(const Student&stu){//常量的引用
        cout << "拷贝构造函数" << endl;
        this->name = stu.name;
        this->age = stu.age - 5;
    }
  • 第一种场景:=会调用拷贝函数
    Student stu ("Peakmain", 25);
    Student stu2 = stu;//=代表赋值,两个地址是不同的。其实这里会调用对象的拷贝函数
    stu.setAge(30);
    /*Student stu2;//声明变量开辟内存
    stu2 = stu;//这个不会调用拷贝构造函数,但是会赋值*/
    cout << "name=" << stu2.getName() << ",age=" << stu2.getAge() << endl;
  • 第二种场景,作为参数传递的时候调用会调用拷贝构造函数
    新增一个方法
Student getStudent(char*name){
    Student stu(name);//栈中开辟的,方法执行完,这个对象会被回收,但是发现调用了拷贝构造函数和析构函数
    return stu;//返回一个新的student的对象,而栈内存开辟stu会被回收
}

调用

Student stu = getStudent("Treasure");
    cout << "name=" << stu.getName() << ",age=" << stu.getAge() << endl;
  • 第三种场景,作为参数传递的时候
    新建方法
void print(Student stu){//stu是该方法栈中一个新的对象,拷贝构造函数赋值,方法执行完调用析构函数
    cout << stu.getName() << "," << stu.getAge() << endl;
}

调用

    Student stu2("Peakmain", 22);
    print(stu2);
    getchar();

补充:浅拷贝和深拷贝
浅拷贝

    Student(const Student&stu){//常量的引用
        cout << "拷贝构造函数" << endl;
        this->name = stu.name;
        this->age = stu.age - 5;
    }

深拷贝

    Student(const Student&stu){//常量的引用
        cout << "拷贝构造函数" << endl;
        this->name = (char*)malloc(sizeof(char)* 100);
        strcpy(this->name, stu.name);
        this->age = stu.age - 5;
    }

相关文章

网友评论

    本文标题:c++基础—引用和函数

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