C++-类和对象-2

类的六个默认成员函数:

类的六个默认成员函数

1.构造函数

构造函数是一个特殊的成员函数,函数名与类名相同;

在创建对象的时候被调用,进行对象的初始化与资源申请;

对象的生命周期只调用一次,并且可以重载。

例如,上面的日期类,设置日期可以不使用自定义的成员函数SetDate,而是使用构造函数:

#include <iostream>
#include <ostream>

using namespace std;

class Date
{
public:
	Date();
    Date(int year, int month, int date);
    void PrintDate();
private:
    int _year;
    int _month;
    int _date;
};

Date::Date()
{
}

Date::Date(int year, int month, int date)
{
    _year = year;
    _month = month;
    _date = date;
}

void Date::PrintDate()
{
    cout<<_year<<"-"<<_month<<"-"<<_date<<endl;
}

int main(int argc, const char** argv) {
    Date a;
    Date b(0, 0, 1);

    a.PrintDate();
    b.PrintDate();
    return 0;
}

输出结果:

book@100ask:~/Desktop$ g++ -o test test.cpp 
book@100ask:~/Desktop$ ./test 
1267428112-22041-1267427440
0-0-1

需注意构造函数以下特点

  • 函数名与类名相同
  • 无返回值
  • 对象实例化时编译器自动调用对应的构造函数
  • 构造函数可以重载
  • 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成
  • 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。

全缺省构造函数:

  • 定义了全缺省构造函数,就不能有无参构造函数
  • 只需要在声明的时候给出缺省参数即可,定义时不能重复给出。
#include <iostream>
#include <ostream>

using namespace std;

class Date
{
   
public:
	// Date();
    Date(int year=2024, int month=6, int date=29);
    void PrintDate();
private:
    int _year;
    int _month;
    int _date;
};
// Date::Date()
// {
   
// }
Date::Date(int year, int month, int date)
{
   
    _year = year;
    _month = month;
    _date = date;
}

void Date::PrintDate()
{
   
    cout<<_year<<"-"<<_month<<"-"<<_date<<endl;
}

int main(int argc, const char** argv) {
   
    Date a;
    Date b(0, 0, 1);

    a.PrintDate();
    b.PrintDate();
    return 0;
}

输出结果:

book@100ask:~/Desktop$ g++ -o test test.cpp 
book@100ask:~/Desktop$ ./test 
2024-6-29
0-0-1

2.析构函数

析构函数与构造函数相反,在对象销毁时会自动调用析构函数,完成类资源的清理。

析构函数的特点:

  • 在类名前面加上~符号的成员函数
  • 没有返回值没有参数
  • 一个类只有一个析构函数,会自动生成默认的析构函数
  • 对象生命周期结束时自动调用
class Date
{
   
public:
	// Date();
    Date(int year=2024, int month=6, int date=29);
    ~Date() // 析构函数
    {
   }
    void PrintDate();
private:
    int _year;
    int _month;
    int _date;
};

3.拷贝构造函数

通过拷贝构造函数可以创建一个与传入对象一样的新对象。

只有单个形参,该形参就是对本类类型的对象引用(一般都以const修饰)。

在用已存在对象构建新对象时自动调用。

class Date
{
public:
	// Date();
    Date(int year=2024, int month=6, int date=29);
    Date(const Date& d);
    void PrintDate();
private:
    int _year;
    int _month;
    int _date;
};

...
Date::Date(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _date = d._date;
}
int main(int argc, const char** argv) {
    Date a;
    Date b(0, 0, 1);
    Date c(a);

    a.PrintDate();
    b.PrintDate();
    c.PrintDate();
    return 0;
}

运行结果:

book@100ask:~/Desktop$ ./test 
2024-6-29
0-0-1
2024-6-29

特点:

  • 拷贝构造函数是构造函数的一个重载形式

  • 拷贝构造函数的参数只有一个且必须传引用,因为传值调用会引起拷贝构造的无穷递归

  • 若未显示定义拷贝构造函数,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
    这里留了一个问题:

    // 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
    class String
    {
    public:
        String(const char* str = "jack")
        {
            _str = (char*)malloc(strlen(str) + 1);
            strcpy(_str, str);
        }
        ~String()
        {
            cout << "~String()" << endl;
            free(_str);
        }
    private:
        char* _str;
    };
    int main()
    {
        String s1("hello");
        String s2(s1);
    }
    

    gpt解答:这个程序会崩溃的原因是因为浅拷贝的问题。默认情况下,编译器生成的拷贝构造函数会对指针成员进行浅拷贝,即只是拷贝指针的值而不是指针指向的内容。这会导致多个对象共享同一块内存。当这些对象被析构时,会出现多次释放同一块内存的情况,从而导致程序崩溃

4.赋值操作符重载函数

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

因此,赋值操作符重载函数属于运算符重载

我们先介绍运算符重载,再说明默认成员函数的赋值操作符。

运算符重载:

  • 函数名为关键字operator后面加操作符符号

  • 函数原型是

    返回值类型 operator操作符(参数列表)
    
  • 操作符的参数中规定,第一个参数为左操作数,第二个参数为右操作数

  • 不能通过连接其他符号来创建新的操作符:比如operator@

  • 不能改变操作符的含义

  • <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值