C语言Day3:类、this指针和特殊成员函数详解

一、Xmind整理:

二、上课笔记整理:

1.类的应用实例

#include <iostream>
using namespace std;

class Person
{
private:
    string name;
public:
    int age;
    int high;
    void set_name(string n);    //在类内声明函数
    void show()
    {
        cout << "name = "<< name << " age = "<<age << " high = "<<high << endl;
    }
};

int main()
{
    Person p1;       //实例化一个类对象p1
    p1.set_name("zhangsan");
    p1.age = 90;
    p1.high = 100;
    p1.show();

    return 0;
}

//类外实现
void Person::set_name(string n)
{
    name = n;
}

2.定义一个矩形类(Rectangle),包含私有成员长(length)、宽(width),定义成员函数:

设置长度: void set_len(int l);

设置宽度: void set_wid(int w);

获取长度: int get_len();

获取宽度: int get_wid();

显示周长和面积: void show();

#include <iostream>

using namespace std;

class Rectangle
{
private:
    int length;
    int width;
public:
    void set_len(int l);
    void set_wid(int w);
    int get_len();
    int get_wid();
    void show()
    {
        cout << "周长为:" << 2*(length+width) << endl;
        cout << "面积为:" << length*width << endl;
    }
};
void Rectangle:: set_len(int l)
{
    length = l;
}
void Rectangle:: set_wid(int w)
{
    width = w;
}
int Rectangle:: get_len()
{
    return length;
}
int Rectangle:: get_wid()
{
    return width;
}
int main()
{
    Rectangle c;
    c.set_len(3);
    c.set_wid(4);
    cout << "长为:" << c.get_len() << endl;
    cout << "宽为:" << c.get_wid() << endl;
    c.show();
    return 0;
}

3.必须使用this指针的场合

#include <iostream>
using namespace std;

class Rec
{
    int length;
    int width;
public:
    //所有函数的类内声明
    void set_len(int length);
    void set_wid(int width);
    int get_len();
    int get_wid();
    void show();
};

void Rec::set_len(int length)
{
    this->length = length;
}

void Rec::set_wid(int width)
{
    this->width = width;
}

int Rec::get_len()
{
    return length;
}

int Rec::get_wid()
{
    return width;
}

void Rec::show()
{
    cout << "周长" << 2*(length+width) << endl;
    cout << "面积" << length*width << endl;
}
int main()
{
    Rec r1;    //实例化了一个Rec类的类对象r1
    Rec r2;
    r2.set_len(20);

    r1.set_len(10);
    r1.set_wid(3);
    cout << "长:" << r1.get_len() << endl;
    cout << "宽:" << r1.get_wid() << endl;
    r1.show();
    return 0;
}

4.构造函数允许函数重载

#include <iostream>
using namespace std;

class Stu
{
    string name;
    int age;
public:
    //定义Stu类的无参构造
    Stu()
    {
        cout << "Stu的无参构造" << endl;
    }
    //函数重载
    Stu(string name,int age)
    {
        this->name = name;
        this->age = age;
        cout << "Stu的有参构造" << endl;
    }
    Stu(string name)
    {
        this->name = name;
        cout << "Stu的对name的有参构造" << endl;
    }
};

int main()
{     
    Stu s1("zhangsan",18);  //栈区的类对象
    Stu *p;  //在栈区申请一个Stu*类型的指针
    p = new Stu("lisi");    //在堆区申请一个Stu的空间,会调用构造函数
    return 0;
}

5.构造函数的初始化列表

//包含其他类的子对象
#include <iostream>
using namespace std;
class Per
{
    string name;
public:
    //Per提供的有参构造
    Per(string name)
    {
        this->name = name;
        cout << "Per的有参构造" << endl;
    }
    Per()
    {
        cout << "Per的无参构造" << endl;
    }
    //一个show函数,输出name
    void show()
    {
        cout << "Per中的name=" << name << endl;
    }
};

class Stu
{
    int score;
public:
    Per p1;    //Stu类中,包含另一个类的子对象,且另一个类只有有参构造函数
public:
    Stu(string name):p1(name)    //必须使用初始化列表,并且在初始化列表显性调用另一个类的构造函数
    {
        cout << "Stu的有参构造" << endl;
    }
    Stu()
    {
        cout << "Stu的无参构造" << endl;
    }
};

int main()
{
    Stu s1("zhangsan");
    //通过s1这个类对象,调用Per中的show函数
    s1.p1.show();

    Stu s2;
    return 0;
}

//同名或者是包含引用成员/const修饰的成员
class Stu
{
    string name;
    //int &age;     //类中有引用成员
    const int score;
public:
    //定义Stu类的无参构造
//    Stu()
//    {
//        cout << "Stu的无参构造" << endl;
//    }
    //函数重载,使用初始化列表的方式
    //初始化列表也可以解决形参和成员属性同名问题
    Stu(string name,int age):name(name),score(age)  //在函数体外给成员属性初始化
    {
        cout << "Stu的有参构造" << endl;
    }
    void show()
    {
        cout << name << " " << score << endl;
    }
};

int main()
{     
    Stu s1("zhangsan",18);  //栈区的类对象
    s1.show();
    Stu *p;  //在栈区申请一个Stu*类型的指针
    p = new Stu("lisi",20);    //在堆区申请一个Stu的空间,会调用构造函数
    return 0;
}

6.需要显性定义出析构函数的情况 

#include <iostream>
using namespace std;

class Stu
{
    int *p;
public:
    Stu():p(new int)    //在不传参的情况下,给指针成员p用堆区的空间初始化
    {
        cout << "Stu无参构造" << endl;
    }

    Stu(int a):p(new int(a))   //在参数为整形变量的情况下,让p指向堆区申请的空间
    //但是把这篇空间的内容,用传过来的整形变量初始化
    {
        cout << "Stu的有参构造" << endl;
    }

    Stu(int *p)   //要求传过来的指针的值,一定是指向堆区空间
    {
        this->p = p;
        cout << "Stu传指针的有参构造" << endl;
    }

    ~Stu()
    {
        //在析构之前释放堆区的空间
        cout << "准备释放空间:" << p << endl;
        delete p;
        p = nullptr;
        cout << "Stu的析构函数" << endl;
    }
};

int main()
{
    Stu s1(90);
    return 0;
}

7.拷贝构造函数

#include <iostream>
using namespace std;
class Stu
{
    string name;
public:
    Stu()
    {
        cout << "Stu的无参构造" << endl;
    }
    Stu(string name)
    {
        this->name = name;
        cout << "Stu的有参构造" << endl;
    }
    Stu(Stu &other)   //Stu的拷贝构造函数,和无参构造以及有参构造构成函数冲澡
    {
        this->name = other.name;
        cout << "Stu的拷贝构造函数" << endl;
    }

    void show()
    {
        cout << name << endl;
    }
};
Stu fun(Stu s1)  //定义了一个函数fun,形参是一个类对象
{
    cout << "调用成功" << endl;
    return s1;
}
int main()
{
    Stu s1("zhangsan");
    cout << "s1:";
    s1.show();
    Stu s2 = s1;   //拷贝构造函数
    cout << "s2:";
    s2.show();
    fun(s2);
    return 0;
}

 8.深浅拷贝问题

#include <iostream>
using namespace std;

class Stu
{
    int *p;
public:
    Stu():p(new int)    //在不传参的情况下,给指针成员p用堆区的空间初始化
    {
        cout << "Stu无参构造" << endl;
    }

    Stu(int a):p(new int(a))   //在参数为整形变量的情况下,让p指向堆区申请的空间
    //但是把这篇空间的内容,用传过来的整形变量初始化
    {
        cout << "Stu的有参构造" << endl;
    }
    Stu(int *p)   //要求传过来的指针的值,一定是指向堆区空间
    {
        this->p = p;
        cout << "Stu传指针的有参构造" << endl;
    }
    ~Stu()
    {
        //在析构之前释放堆区的空间
        cout << "准备释放空间:" << p << endl;
        delete p;
        p = nullptr;
        cout << "Stu的析构函数" << endl;
    }
    void show()
    {
        cout << "p=" << p << endl;
    }
    void set_p(int a)
    {
        *p = a;    //通过指针变量p,修改内存中的值
    }
    void show_p()
    {
        cout << *p << endl;  //通过指针变量,输出p指向的内存中的值
    }
    
    //拷贝构造函数
    Stu(Stu &other)
    {
        p = new int;  //手动让s2的指针p指向堆区的空间
        //实现深拷贝
        *p = *(other.p);
        cout << "Stu的拷贝构造函数" << endl;
    }
};
int main()
{
    Stu s1(90);    //会给s1的指针成员在堆区申请一片空间使用90初始化
    Stu s2 = s1;   //申请了一个Stu的类对象的空间,也实现了用s1的值初始化s2
    //左调右参
    //上面一行,调用了拷贝构造函数
    //使用了默认提供的拷贝构造,会造成指针成员,指向同一片空间的问题
//    cout << "s1:";
//    s1.show();
//    cout << "s2:";
//    s2.show();
    s1.show_p();
    s2.show_p();
    s1.set_p(101);
    s1.show_p();
    s2.show_p();

    return 0;
}
物联沃分享整理
物联沃-IOTWORD物联网 » C语言Day3:类、this指针和特殊成员函数详解

发表评论