当前位置:网站首页>基于对象(Object Based)-两个经典类
基于对象(Object Based)-两个经典类
2022-07-23 05:44:00 【龙城ne货92923】
- 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 保存分配内存大小,例如:
0000004140表示64的十六进制,1标记申请内存 - 灰色:dubug模式信息
- 绿色:数据
- 深绿色:为了满足16倍数填补

- 白色:数组的元素个数

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

边栏推荐
- 2021 TOP10 development trend of information science. Deep learning? Convolutional neural network?
- 【Autosar CP通用 1.如何阅读Autosar官方文档】
- 单片机学习笔记9--常见的通信方式(基于百问网STM32F103系列教程)
- Analyze the pre integration of vio with less rigorous but logical mathematical theory
- 高等代数知识结构
- 时间序列的数据分析(三):经典时间序列分解
- 单片机学习笔记3--单片机结构和最小系统(基于百问网STM32F103系列教程)
- 使用pycaret来进行数据挖掘:关联规则挖掘
- Interpretation of the paper: "i4mc deep: intelligent prediction of N4 methylcytosine sites using deep learning methods with chemical properties"
- 把LVGL所有控件整合到一个工程中展示(LVGL6.0版本)
猜你喜欢

Installation and use of APP automated testing tool appium

Interpretation of the paper: develop a prediction model based on multi-layer deep learning to identify DNA N4 methylcytosine modification

【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】

Using pycaret: low code, automated machine learning framework to solve regression problems

Green data center: comprehensive analysis of air-cooled GPU server and water-cooled GPU server

【AUTOSAR COM 3.信号的收发流程TX/RX】
![[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points](/img/e4/0b709a6ed5e639a75e0506f6eac9fd.png)
[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points

匿名上位机v7波形显示

高等代数知识结构

ARM架构与编程1--LED闪烁(基于百问网ARM架构与编程教程视频)
随机推荐
Data analysis of time series (III): decomposition of classical time series
使用PyOD来进行异常值检测
利用google or-tools 求解逻辑难题:斑马问题
CPC client installation tutorial
How to develop the liquid cooled GPU server in the data center under the "east to West calculation"?
ARM架构与编程5--gcc与Makefile(基于百问网ARM架构与编程教程视频)
ARM架构与编程2--ARM架构(基于百问网ARM架构与编程教程视频)
预处理指令#define,你真的懂了吗?
#under指令
Deep learning neural network
What technologies are used in pharmaceutical research and development in the field of life sciences? Cryoelectron microscope? Molecular simulation? IND?
Importance of data analysis
A hundred schools of thought contend at the 2021 trusted privacy computing Summit Forum and data security industry summit
【AUTOSAR COM 1.通信协议栈介绍】
C语言中,对柔性数组的理解
钢结构复习题
对字符串函数的使用和理解(1)
Opencv library installation path (don't open this)
高分子物理名词解释
【存储器了解 RAM flash和eeprom存储器的区别和作用】