当前位置:网站首页>基于对象(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时的正确写法

边栏推荐
- NLP自然语言处理-机器学习和自然语言处理介绍(一)
- [physical layer of CAN bus] 1. Content sharing of can/canfd sampling points
- Using or tools to solve path planning problem (VRP)
- 单片机学习笔记8--按键和外部中断(基于百问网STM32F103系列教程)
- NLP natural language processing - Introduction to machine learning and natural language processing (I)
- 高分子物理名词解释归纳
- Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in a month
- 博客搭建六:绑定自己域名的方法
- Data analysis (I)
- Smart pointer shared_ PTR and unique_ ptr
猜你喜欢

NLP natural language processing - Introduction to machine learning and natural language processing (2)

把LVGL所有控件整合到一个工程中展示(LVGL6.0版本)

Using Google or tools to solve logical problems: Zebra problem

使用PyOD来进行异常值检测

钢结构基本原理全面详细总结

【AUTOSAR CanTP 1.学习UDS诊断的网络层协议】

Interpretation of the paper: develop and verify the deep learning system to classify the etiology of macular hole and predict the anatomical results

单片机学习笔记8--按键和外部中断(基于百问网STM32F103系列教程)

深度学习-神经网络

Interpretation of the paper: recognition of enhancer promoter interactions with neural networks based on pre trained DNA vectors and attention mechanisms
随机推荐
【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】
利用google or-tools 求解逻辑难题:斑马问题
【AUTOSAR CanTP 1.学习UDS诊断的网络层协议】
博客搭建五:图床选择
匿名上位机v7波形显示
单片机学习笔记9--串口通信(基于百问网STM32F103系列教程)
单片机学习笔记1--资料下载、环境搭建(基于百问网STM32F103系列教程)
With statement
Gartner research: how is China's digital development compared with the world level? Can high-performance computing dominate?
Build "green computing" and interpret "Intelligent Computing Center"
Interpretation of the paper: recognition of enhancer promoter interactions with neural networks based on pre trained DNA vectors and attention mechanisms
In depth interpretation of Google or tools' complex scheduling program
Under the "double carbon" goal of the digital economy, why does the "digital East and digital West" data center rely on liquid cooling technology to save energy and reduce emissions?
[AUTOSAR candrive 1. learn the function and structure of candrive]
高等代数知识结构
利用or-tools来求解路径规划问题(TSP)
钢结构复习题
数据分析的重要性
[introduction to AUTOSAR com 4.com service layer module]
Using or tools to solve the path planning problem (TSP)