首页 > 编程语言 >C++中的几种构造函数和析构函数

C++中的几种构造函数和析构函数

时间:2022-11-28 16:13:12浏览次数:38  
标签:sound const string cout C++ Animal 和析构 构造函数

本篇文章,我们来了解一下C++中的几种构造函数,以及析构函数

#include <format>
#include <iostream>
#include <string>

using std::format;
using std::cout;
using std::string;

const string unk {"unknown"};
const string clone_prefix {"clone-"};

// -- interface --
class Animal {
    string a_type {};
    string a_name {};
    string a_sound {};
public:
    Animal();   // default constructor  这个是默认构造函数
    Animal(const string& type, const string& name, const string& sound);  //带参数的构造函数
    Animal(const Animal&);  // copy constructor  copy构造函数
    ~Animal();  // destructor
    Animal& operator = (const Animal&);   // copy/assignment operator   copy/assignment操作符
    void print() const;
};

// -- implementation --  默认构造函数的实现
Animal::Animal() : a_type(unk), a_name(unk), a_sound(unk) {
    cout << "default constructor\n";
}

// 带参数构造函数的实现 Animal::Animal(const string& type, const string& name, const string& sound) : a_type(type), a_name(name), a_sound(sound) { cout << "constructor with arguments\n"; } // copy构造函数的实现 Animal::Animal(const Animal& a) { cout << "copy constructor\n"; a_name = clone_prefix + a.a_name; a_type = a.a_type; a_sound = a.a_sound; }
// 析构函数的实现 Animal::~Animal() { cout << format("destructor: {} the {}\n", a_name, a_type); }
// copy/assignment 操作符的实现 Animal& Animal::operator = (const Animal& o) { cout << "assignment operator\n"; if(this != &o) { // this在这里代表当前对象current object, 判断传入的Animal对象的地址和this当前对象不是同一个地址,也就是说不是同一个对象,否则 避免同一个对象进行copy a_name = clone_prefix + o.a_name; a_type = o.a_type; a_sound = o.a_sound; } return *this; //D-reference当前对象,进行返回 } void Animal::print () const { cout << format("{} the {} says {}\n", a_name, a_type, a_sound); } int main() { Animal a {}; a.print(); const Animal b("goat", "bob", "baah"); b.print(); const Animal c = b; //这里用的就是copy 的构造函数,注意是这种写法,而不是 const Animal c(&b); =>不确定是否可以这样写 c.print(); a = c; //这里用的就是copy/assiganment 操作符 a.print(); cout << "end of main\n"; return 0; }

 

标签:sound,const,string,cout,C++,Animal,和析构,构造函数
From: https://www.cnblogs.com/wphl-27/p/16932477.html

相关文章

  • 【c++】map用法详解
    【c++】map用法详解LeeMooq已于2022-05-0122:01:43修改21398收藏46分类专栏:c++学习文章标签:c++版权c++学习专栏收录该内容5篇文章3订阅订阅专栏map是c++标准......
  • python grammar、C/C++ Python Parsing Engine
    pythongrammar、C/C++PythonParsingEnginecatalog1.Python语言简介2.Python模块3.嵌入式Python解析引擎:C++调用Python4.Python调用C(b......
  • C++ 进程间通信
    https://blog.csdn.net/weixin_38416696/article/details/90719388一,C++常用进程间通信管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖......
  • C++中class中对私有变量的访问
    C++中写class时,对私有变量通常使用set和get方法来进行访问,比较标准的例子classA{intia{};intib{};intic{};public:A(inta,intb,intc......
  • c++命名规范
    变量、函数名、名字空间用snake_case(全小写,单词之间用下划线连接),全局变量加“g_”前缀_自定义类名用CamelCase(单词首字母大写),成员函数用snake_case,成员变量加“m......
  • C++机票购买系统
    C++机票购买系统该系统有两类用户,会员(多名)和管理员(1名)其中,会员功能包括:1、首先注册并录入个人信息,包括:用户名,密码,生日,邮箱。注册后,自动设置会员编号。2、登录不......
  • 罗剑锋的C++实战笔记-学习笔记(3)
    书接上文,三句名言镇楼。三句名言镇楼任何人都能写出机器能看懂的代码,只有优秀的程序员才能写出人看懂的代码两种写程序的方式:把代码写的非常复杂,以至于"看不出明显......
  • 罗剑锋的C++实战笔记-学习笔记(2)
    书接上文,三句名言镇楼。三句名言镇楼任何人都能写出机器能看懂的代码,只有优秀的程序员才能写出人看懂的代码两种写程序的方式:把代码写的非常复杂,以至于"看不出明显......
  • 罗剑锋的C++实战笔记(学习笔记1)
    本系列文章记载学习一门在线课程罗剑锋的C++实战笔记过程中的心得体会,只会记录新增加的知识点,那些心中已熟透的知识点,不会重复记录。c++的主战场在Linux上,现在开发Wi......
  • C++中的Pointer member dereference(D-reference) operator
    在C++中,比如我们可以把一个结构体struct的地址赋给一个指针pointer然后使用这个指针去访问这个结构体中的元素时,可以使用pointermemberD-referenceoperator: ->用来......