IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++基于对象-两个基本的(单一)类(带指针和不带指针) -> 正文阅读

[C++知识库]C++基于对象-两个基本的(单一)类(带指针和不带指针)

  • Object Based:面对的是单一的class设计
  • Object Oriented:面对的是多重classes的设计(classes和classes)之间的关系

一、两个基本的类

(1)不带指针的类

知识点:
(1)构造函数成员变量初始化->尽量在初始化列表中进行初始化(而不是在赋值)
(2)成员函数后面加 const 表示这个函数不能修改成员变量的值(否则当定义一个 const 对象时出现错误)
(3)传递参数和返回值的时候尽量使用 reference 传递,不修改值时要使用 const ,(返回值如果不是local object 就可以传递引用)
(4)friend 友元函数可以直接使用类中成员变量,相同 class 的各个 object 互为友元
(5)操作符重载 两种形式:成员函数 和 普通非成员函数

#pragma once

#include <iostream>

class complex
{
public:
	//构造函数 使用初始化列表
	complex(double r, double i) : re(r), im(i)
	{

	}

	//成员函数
	double real() const 
	{
		return re;
	}
	double imag() const
	{
		return im;
	}

	//成员函数运算符重载
	complex& operator += (const complex& o);
	complex& operator -= (const complex& o);
	complex& operator *= (const complex& o);
	complex& operator /= (const complex& o);

	

private:
	double re;		//实部
	double im;		//虚部

	//加减乘除 add , subtract , multiply and divide
	//友元函数,方便直接操作private成员
	friend complex& __add(complex* ths,const complex& o);
	friend complex& __subtract(complex* ths,const complex& o);
	friend complex& __multiply(complex* ths,const complex& o);
	friend complex& __divide(complex* ths,const complex& o);
};

// complex.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//


#include "complex.h"

double real(const complex& o)
{
    return o.real();
}
double imag(const complex& o)
{
    return o.imag();
}

//必须为非类成员函数   << 要作用于 ostream 
std::ostream& operator << (std::ostream& os,const complex& o)
{
    return os << "(" << real(o) << "," << imag(o) << ")";
}

// + 非类成员函数 要考虑不是两个复数的情况
inline complex operator + (const complex& o1, const complex& o2)
{
    return complex(o1.real() + o2.real(), o1.imag() + o2.imag());
}

inline complex operator + (double o1, const complex& o2)
{
    return complex(o1+ o2.real(), o2.imag());
}

inline complex operator + (const complex& o1, double o2)
{
    return complex(o1.real(), o1.imag() + o2);
}

inline bool operator == (const complex& o1, const complex& o2)
{
    return o1.real() == o2.real() && o1.imag() == o2.imag();
}

//+ 正号
inline complex operator + (const complex& o)
{
    return o;
}

//- 负号
inline complex operator - (const complex& o)
{
    return complex(-o.real(), -o.imag());
}



//友元函数
complex& __add(complex* ths, const complex& o)
{
    ths->re += o.re;
    ths->im += o.im;

    return *ths;
}
complex& __subtract(complex* ths, const complex& o)
{
    ths->re -= o.re;
    ths->im -= o.im;

    return *ths;
}
complex& __multiply(complex* ths, const complex& o)
{
    ths->re *= o.re;
    ths->im *= o.im;

    return *ths;
}
complex& __divide(complex* ths, const complex& o)
{
    ths->re /= o.re;
    ths->im /= o.im;

    return *ths;
}

complex& complex::operator += (const complex& o)
{
    return __add(this,o);
}
complex& complex::operator -= (const complex& o)
{
    return __subtract(this, o);
}
complex& complex::operator *= (const complex& o)
{
    return __multiply(this, o);
}
complex& complex::operator /= (const complex& o)
{
    return __divide(this, o);
}

int main()
{
    complex a(10, 20);
    complex b(1,2);

    std::cout << a << std::endl;
    std::cout << b << std::endl;
    
    std::cout << +a << std::endl;
    std::cout << -a << std::endl;

    std::cout << a + b << std::endl;
    std::cout << (a == b) << std::endl;

    std::cout << (a += b) << std::endl;
    std::cout << (a -= b) << std::endl;
    std::cout << (a *= b) << std::endl;
    std::cout << (a /= b) << std::endl;
}

(2)带指针的类

知识点:
(1)带指针的类需要实现的三个函数(big three)拷贝构造、拷贝复制、析构,目的是要避免浅拷贝
(2)拷贝赋值需要注意处理是否是自我赋值
(3)拷贝赋值返回类类型,需要考虑 连续=赋值这种操作

#pragma once

class String
{
public:
	//构造函数
	String(const char* cstr = 0);

	//拷贝构造
	String(const String& str);
	//拷贝赋值
	String& operator= (const String& str);
	//析构函数
	~String();
	inline char* get_cstr() const{ return m_data; }
private:
	char* m_data = { nullptr };
};
// string.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "string.h"

using namespace std;

std::ostream& operator << (std::ostream& os, const String& o)
{
	return os << o.get_cstr();
}

String::String(const char* cstr)
{
	if (cstr)
	{
		//注意字符串后面的结束符
		int strLen = strlen(cstr) + 1;
		m_data = new char[strLen];
		strcpy_s(m_data, strLen ,cstr);
	}
	else
	{
		m_data = new char[1];
		*m_data = '\n';
	}
}

String::String(const String& str)
{
	//str.m_data  直接取另一个对象的私有成员,相同 class 的各个 object 互为友元
	int strLen = strlen(str.m_data) + 1;
	m_data = new char[strLen];
	strcpy_s(m_data, strLen, str.m_data);
}

String& String::operator= (const String& str)
{
	//检查是否是自我复制,非常重要,否则自己给自己赋值时会报错
	if (this == &str)
	{
		return *this;
	}
	delete[] m_data;  //删除原来的
	int strLen = strlen(str.m_data) + 1;
	m_data = new char[strLen];
	strcpy_s(m_data, strLen, str.m_data);

    //注意返回值 不是local 可以返回引用
	return *this;
}

String::~String()
{
	delete[] m_data;
    m_data = nullptr;
}


int main()
{
	String a("hello");
	String b(a);
	String c;
	c = a;
	a = a;   //自我赋值,拷贝赋值函数中不进行自我赋值检查,此处会报错

	cout << "a:" << a << endl;
	cout << "b:" << b << endl;
	cout << "c:" << c << endl;
}

(3)new/delete 对象

(4)动态分配内存实际大小

  • 红砖色:cookie 保存分配内存大小,例如:00000041 40表示64的十六进制,1标记申请内存
  • 灰色:dubug模式信息
  • 绿色:数据
  • 深绿色:为了满足16倍数填补

在这里插入图片描述

  • 白色:数组的元素个数

在这里插入图片描述

(5)动态分配数组delete时的正确写法

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-17 16:02:42  更:2022-07-17 16:06:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 10:39:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码