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

C++的构造函数和析构函数

时间:2023-09-20 17:57:08浏览次数:45  
标签:const String double C++ complex operator 和析构 data 构造函数

背景介绍

在B站上看完侯捷老师讲解的两个类:String类 and complex类,这两个类的实现体现了不带指针和带指针的区别,也可以作为设计类的参考学习。

这两个类的实现过程中有很多小细节的东西需要注意,否则很可能造成编译报错。

编写带指针的类String

在c++的ansi库中有有一个string类,用于处理字符串。那么便仿写一个String类,实现其基本功能。

mystring.h

#pragma once
#ifndef __MYSTRING__
#define __MYSTRING__

#include <iostream>
#include <string.h>
using namespace std;

class String
{
public:
	String(const char *cstr = 0);
	String(const String& str);
	String& operator = (const String& str);
	~String();
	char* get_c_str() const { return m_data; };
private:
	char* m_data;
};

/* global function */
ostream& operator<< (ostream& os, const String& str);

#endif // !__MYSTRING__

mystring.cpp

#include "mystring.h"

/* 普通构造函数 */
String::String(const char *cstr)
{
	if (cstr)
	{
		m_data = new char[strlen(cstr) + 1];
		strcpy(m_data, cstr);
	}
	else
	{
		m_data = new char[1];
		*m_data = '\0';
	}
}

/* 析构函数 */
String::~String()
{
	delete[] m_data;
}

/* 拷贝构造函数 */
String::String(const String& str)
{
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
}

/* 拷贝赋值函数 */
String& String::operator = (const String& str)
{
	if (this == &str) // 检测自我赋值 self assignment
	{
		return *this;
	}
	delete[] m_data;
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
	return *this;
}

ostream& operator<< (ostream& os, const String& str)
{
	os << str.get_c_str();
	return os;
}

main.cpp

#include "mystring.h"

int main(void)
{
	String s1 = "hello";
	String s2(s1);
	String s3 = s2;

	cout << "s1: " << s1 << endl;
	cout << "s2: " << s1 << endl;
	cout << "s3: " << s1 << endl;
	
	return 0;
}


标签:const,String,double,C++,complex,operator,和析构,data,构造函数
From: https://www.cnblogs.com/caojun97/p/17717637.html

相关文章

  • C++医学影像(PACS)管理系统源码
    PACS(PictureArchivingandCommunicationsSystem)——图像存储与传输系统,和医院信息化及数字化的目标紧密关联,它是专门为现代化医院的影像管理而设计的包括数字化医学图像信息的采集、显示、处理、存储、诊断、输出、管理、查询、信息处理的综合应用系统,是以数字化诊断(无纸化、无......
  • C++ STL 容器之map
    一、map简介可以将任何基本类型映射到任何基本类型。如intarray[100]事实上就是定义了一个int型到int型的映射。map提供一对一的数据处理,key-value键值对,其类型可以自己定义,第一个称为关键字,第二个为关键字的值map内部是自动排序的二、用法1.map定义:map<type1name,t......
  • c++中生成随机数
    #include<iostream>#include<string>#include<algorithm>#include<ctime>usingnamespacestd;constintINF=1e9;intmain(){//设置种子srand((unsigned)time(NULL));//可随机生成0-10以内的数 intt=rand()%10; cout<<......
  • 【结对编程互评-C++】中小学数学卷子自动生成程序
    【结对编程互评-C++】中小学数学卷子自动生成程序项目名称:中小学数学卷子自动生成程序编程语言:C++代码作者:李义评价人:张恒硕目录[1.项目要求][1.1目标用户][1.2实现功能][2.代码分析][3.功能测试][3.1登录功能测试][3.2出题功能测试][4.优缺点分析与总结]......
  • C++学习
    C++简单或复杂又如何,万般皆由人--风尘尘风一、C++简述1.1C++概念C++是一种由BjarneStroustrup于1979年在新泽西州贝尔实验室开始设计开发的高级语言C++扩充和完善了C语言,是面向对象的程序设计语言,C++可运行于多种平台上(Win、Mac、unix)1.2C++特点C++......
  • C++中的类指针
    Studenta;s.setName("A");//Studeng*b=newStudent();Student*b; //声名指针b=newStudent(); //动态分配内存b->setName("B"); //访问成员函数分析定义类对象基本格式是:Studenta;在定义时就已经为a对象分配好了内存空间,且为内存栈;定义类指针......
  • C++文件的读写
    文件读写函数库对于文件对象的操作,主要使用库:#include<fstream>类可以定义三种类对象:ifstream定义的对象只能读文件ofstream定义的对象只能写文件iofstream定义对象既能读文件,也能写文件类定义的对象中open()方法的第二个参数文件模式(filemode)有多种属性:in:......
  • C++ STL之向量vector
    /*vector_example.cpp*/#include<iostream>#include<vector>#include<string>usingnamespacestd;intmain(){vector<string>msg={"Hello","C++","World","from","VSCode"......
  • C++中的转换构造函数
    在C/C++中,不同的数据类型之间可以相互转换。无需用户指明如何转换的称为自动类型转换(隐式类型转换),需要用户显式地指明如何转换的称为强制类型转换。自动类型转换示例:inta=6;a=7.5+a;编译器对7.5是作为double类型处理的,在求解表达式时,先将a转换为double类型,然......
  • C++ 类成员函数全家桶
    RAIIResourceAcquisitionIsInitialization,资源获取即初始化这是一种解决资源管理问题的方法,将资源的有效期与持有资源的对象的生命期严格绑定,由对象的构造函数完成资源的分配,由析构函数完成资源的释放C++借助构造函数和析构函数,解决了传统的malloc&free和new&del......