01.数组类(了解)

1.目的:设计一个类,该类有数组的功能,可以存储数据,可以删除修改数据

2.设计核心数据

1.属性:指针(指向堆区空间),数组实际存储的元素个数,数组容量

2.方法:构造(开辟堆区空间),尾插,头插,指定位置插入,尾删,头删,获取指定位置的值,指定位置修改值,获取数组元素个数,获取数组容量,析构函数

3.代码实现(看代码)

02.运算符重载的概念(重点)

1.运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

2.运算符重载的目的是让语法更加简洁

3.运算符重载不能改变本来寓意,不能改变基础类型寓意

4.运算符重载的本质是另一种函数调用(是编译器去调用)

5.这个函数同一的名字叫operator

6.重载函数可以写成全局或成员函数

7.重载函数如果写成全局的,那么双目运算符左边的是第一个参数,右边是第二个参数

8.重载函数如果写成成员函数,那么双目运算符的左边是this,右边是第一个参数

9.不能改变运算符优先级,不能改变运算符的参数个数。

03.加号运算符重载(重点)

1.同类型的对象相加

class Maker
{
public:
Maker(int id, int age)
{
this->id = id;
this->age = age;
}
//写成成员函数,那么只需要一个参数,这个参数是加号的右边
Maker operator+(Maker &m2)
{
Maker temp(this->id + m2.id, this->age + m2.age); return temp;
}
public:
int id;
int age;
}; //全局方式 //3.编译器调用这个函数
//Maker operator+(Maker &p1,Maker &p2)//2.编译器检查参数是否对应,第一个参数是加的左边,第二参数是加号的右边
//{
// Maker temp(p1.id + p2.id, p1.age + p2.age);
//
// return temp;
//} void test01()
{
Maker m1(1, 20);
Maker m2(2, 22);
//+也叫双目运算符
Maker m3=m1 + m2;//1.编译器看到两个对象相加,那么编译器会去找有没有叫operator+的函数 cout << "id:" << m3.id << " age:" << m3.age << endl; //复数加
Maker m4 = m1 + m2 + m3;
cout << "id:" << m4.id << " age:" << m4.age << endl;
}

2.不同类型的对象相加

class Maker
{
public:
Maker(int id, int age)
{
this->id = id;
this->age = age;
} public:
int id;
int age;
};
class Student
{
public:
Student()
{
mid = 0;
}
Student(int id)
{
mid = id;
}
public:
int mid;
}; Student operator+(Maker &m, Student &s)
{
Student tmp(m.id + s.mid); return tmp; } Student operator+(Student &s, Maker &m)
{
Student tmp(m.id + s.mid); return tmp; } void test()
{
Maker m1(1, 18);
Student s1(2);
Student s2 = m1 + s1; s1 + m1;
}

04.减号运算符重载(重点)

class Maker
{
public:
Maker(int id)
{
this->id = id;
}
Maker operator-(Maker &m2)
{
Maker tmp(this->id - m2.id);
return tmp;
}
public:
int id;
}; int operator-(Maker &m,int b)
{
return m.id-b;
} void test()
{
Maker m1(10);
Maker m2(5);
Maker m3 = m1 - m2;
cout << m3.id << endl; int a = m3 - 5;
cout << a << endl;
}

05.左移和右移运算符重载(重点难点)

1.左移运算符重载

​ 1.cout是对象,<<是左移运算符

​ 2.重载左移运算符是为了直接打印对象

​ 3.形参和实参是一个对象

​ 4.不能改变库类中的代码

​ 5.ostream中把拷贝构造函数私有化了

​ 4.如果要和endl一起使用,那么必须返回ostream的对象.

class Maker
{
//如果要访问类的私有成员,那么把<<重载函数声明为友元
friend ostream& operator<<(ostream &out, Maker &m);
public:
Maker(int id,string name)
{
this->id = id;
this->name = name;
} private:
int id;
string name;
};
//1.形参和实参是一个对象
//2.不能改变库类中的代码
//3.ostream中把拷贝构造函数私有化了
//4.如果要和endl一起使用,那么必须返回ostream的对象
ostream& operator<<(ostream &out, Maker &m)
{
cout << m.id <<" "<<m.name<< endl; return out;
} void test01()
{
Maker m(10,"小花");
cout << m << endl;
cout << endl;
/*
endl是一个函数
operator<<(endl)
*/ cout << 10;//内部重载了基础数据类型
}

2.右移运算符重载

class Maker
{
friend istream & operator>>(istream &in, Maker &m);
public:
Maker(string name, int age)
{
this->name = name;
this->age = age;
}
int getAge()
{
return age;
}
private:
string name;
int age;
}; istream &operator>>(istream &in, Maker &m)
{
in >> m.age;
in >> m.name; return in;
} void test02()
{
Maker m("悟空", 15);
Maker m2("悟空2", 25); cin >> m>>m2; cout << m.getAge() << endl;
cout << m2.getAge() << endl; }

06.赋值运算符重载(重点)

1.编译器默认给类提供了一个默认的赋值运算符重载函数

2.默认的赋值运算符重载函数进行了简单的赋值操作

class Maker
{
public:
Maker()
{
id = 0;
age = 0;
}
Maker(int id, int age)
{
this->id = id;
this->age = age;
}
public:
int id;
int age;
}; void test()
{
Maker m1(10, 20);
Maker m2; m2 = m1;//赋值操作
//默认的赋值运算符重载函数进行了简单的赋值操作
cout << m2.id << " " << m2.age << endl;
}

3.当类有成员指针时,然后在构造函数中申请堆区空间,在析构函数中释放堆区空间,会出现同一块空间释放2次,然后内存泄漏,所以要重写赋值运算符重载函数

class Student
{
public:
Student(const char *name)
{
pName = new char[strlen(name) + 1];
strcpy(pName, name);
}
//防止浅拷贝
Student(const Student &stu)
{
pName = new char[strlen(stu.pName) + 1];
strcpy(pName, stu.pName);
}
//重写赋值运算符重载函数
Student &operator=(const Student &stu)
{
//1.不能确定this->pName指向的空间是否能装下stu中的数据,所以先释放this->pName指向的空间
if (this->pName != NULL)
{
delete[] this->pName;
this->pName = NULL;
} //2.申请堆区空间,大小由stu决定
this->pName = new char[strlen(stu.pName) + 1];
//3.拷贝数据
strcpy(this->pName, stu.pName); //4.返回对象本身
return *this;
} ~Student()
{
if (pName != NULL)
{
delete[] pName;
pName = NULL;
}
} void printStudent()
{
cout << "Name:" << pName << endl;
}
public:
char *pName;
}; void test02()
{
Student s1("悟空");
Student s2("小林"); s1.printStudent();
s2.printStudent(); s1 = s2;//赋值操作 s1.printStudent();
s2.printStudent(); //复数运算不会出错
//s1 = s2 = s3;
}

4.赋值运算符重载函数中,为什么要返回引用

void test03()
{
Student s1("a");
Student s2("b");
Student s3("c"); s1 = s2 = s3;//s3赋值s2,s2赋值给s1 cout << &(s2 = s3) << endl;
cout << &s2 << endl;
//如果返回的是值,s2=s3这个表达式会产生一个新的对象
//s1=s2=s3,赋值运算符本来的寓意,是s3赋值s2,s2赋值给s1
//也就是说s2=s3这个表达式要返回s2这个对象,所以要返回引用 }

07.关系运算符重载(了解)

class Maker
{
public:
Maker()
{
id = 0;
age = 0;
}
Maker(int id, int age)
{
this->id = id;
this->age = age;
} bool operator==(Maker &m)
{
if (this->id == m.id && this->age == m.age)
{
return true;
}
return false;
} bool operator!=(Maker &m)
{
if (this->id != m.id || this->age != m.age)
{
return true;
}
return false;
}
public:
int id;
int age;
}; void test()
{
Maker p1(1, 20);
Maker p2; if (p1 == p2)
{
cout << "真" << endl;
}
else
{
cout << "假" << endl;
} if (p1 != p2)
{
cout << "真" << endl;
}
else
{
cout << "假" << endl;
}
}

08.前置加加和后置加加运算符重载(重点难点)

class Maker
{
friend ostream &operator<<(ostream &out, Maker &m);
public:
Maker(int a)
{
this->a = a;
}
//重载前置加加
Maker &operator++()
{
++this->a;
return *this;
} //后置加加,
Maker operator++(int)//占位参数,必须是int
{
//后置加加,先返回,后加加
Maker tmp(*this);//1.*this里面的值a是等于2
++this->a;//这个对象的a等3
return tmp;
}
private:
int a;
}; ostream &operator<<(ostream &out, Maker &m)
{
out << m.a << endl;
return out;
} void test02()
{
Maker m1(1);
cout << m1 << endl;//1
cout << ++m1 << endl;//2
//++(++m1);
cout << m1++ << endl;//2 这里返回的拷贝的tmp对象
cout << m1 << endl;//3 这里返回的是++this->a的对象 //同等条件下,优先使用前置加加,不需要产生新的对象和调用拷贝构造 }

09.数组下标运算符重载(重点)

MyArray.h
class MyArray
{
public:
//重写赋值运算符重载函数
MyArray&operator=(const MyArray &m); //要能当左右值
int &operator[](int index); }; MyArray.cpp
//重写赋值运算符重载函数
MyArray&MyArray::operator=(const MyArray &m)
{
cout << "赋值函数" << endl;
//1.释放原来的空间
if (this->pArray != NULL)
{
delete[] this->pArray;
this->pArray = NULL;
}
this->mCapacity = m.mCapacity;
this->mSize = m.mSize;
//2.申请空间,大小由m决定
this->pArray = new int[m.mCapacity];
//3.拷贝数据
cout << "this->mSize:"<<this->mSize << endl;
for (int i = 0; i < this->mCapacity; i++)
{
this->pArray[i] = m.pArray[i];
} return *this;
} //要能当左右值
int &MyArray::operator[](int index)
{
/*
for (int i = 0; i < 20; i++)
{
arr[i] = i + 10;
} for (int i = 0; i < 20; i++)
{
cout << arr[i] << " ";
}
*/
//赋值时,加加 if (this->mSize <=index)
{
this->mSize++;
} return this->pArray[index];
} 数组下标重载.cpp
void test02()
{
MyArray arr;
for (int i = 0; i < 20; i++)
{
arr[i] = i + 10;
} for (int i = 0; i < 20; i++)
{
cout << arr[i] << " ";
}
cout << endl;
MyArray arr2;
arr2 = arr;
for (int i = 0; i < 20; i++)
{
cout << arr2[i] << " ";
}
cout << endl;
cout << arr2.Size() << endl;
}

Day05笔记的更多相关文章

  1. python day05笔记总结

    2019.4.2 S21 day05笔记总结 一.昨日内容回顾与补充 1.extend(列表独有功能) 循环添加到一个列表中 a.有列表users = ['张三',‘李四]   people = [' ...

  2. Shell:Day05.笔记

    交互输入与for语句 1.交互输入 read  Python中用input()函数,进行输入:  read命令同时可以定义多个变量值:而输入的内容默认以空格为分隔符,将值输入到对应的变量中: 如果默认 ...

  3. 04C++核心编程

    Day01 笔记 1 C++概述 1.1 C++两大编程思想 1.1.1 面向对象 1.1.2 泛型编程 1.2 移植性和标准 1.2.1 ANSI 在1998制定出C++第一套标准 2 c++初识 ...

  4. 超全面的JavaWeb笔记day05<xml&dtd&jaxp>

    0.表单提交方式(*****) button提交 超链接提交 事件 1.xml简介和应用(了解) 2.xml文档声明和乱码解决(*****) 文档声明 必须放在第一行第一列 设置xml编码和保存编码一 ...

  5. C++Primer笔记-----day05

    =======================================================================day05======================== ...

  6. Django学习day05随堂笔记

    每日测验 """ 今日考题 1.反向解析的本质是什么,无名和有名反向解析如何操作? 2..路由分发能够实现的前提是什么,需要注意什么,名称空间什么时候使用 3..什么是虚 ...

  7. day05 java JDBC案例—Android小白的学习笔记

    1.要从键盘录入用户名与密码我们需要使用Scanner类完成操作 2.接收到用户名与密码后,我们需要调用jdbc程序根据用户名与密码查询数据库 User.java package com.superg ...

  8. java学习笔记day05

    1.final关键字:防止被继承的类或覆写的方法修改,变量或方法被final定义后  会在内在中存在 特点:   1)可以修饰类.函数.变量.   2)被final修饰的类不可以被继承.   3)被f ...

  9. C++MFC编程笔记day05 文档类-单文档和多文档应用程序

    文档类 1 相关类    CDocument类-父类是CCmdTarget类,所以,文档类也能够处理菜单等               命令消息. 作用保存和管理数据.    注意事项:怎样解决断言错 ...

  10. 标C编程笔记day05 函数声明、文件读写、联合类型、枚举类型

    函数声明:     1.隐式声明:在没有声明的情况下,系统可依据參数类型推断去调用函数(有可能出错)     2.显式声明:声明在被调用之前.如:double add(double,double);  ...

随机推荐

  1. CSS3-3D导航(transform:rotate)

    借助transform:rotate实现上图的3D导航效果 具体代码如下 1 <div class="nav"> 2 <ul> 3 <li> 4 ...

  2. vue-webpack代理

    baseUrl 改为 '/api'

  3. 北斗GPS授时系统技术及ntp时钟服务器(PTP)在电力系统中的应用

    北斗GPS授时系统技术及ntp时钟服务器(PTP)在电力系统中的应用 北斗GPS授时系统技术及ntp时钟服务器(PTP)在电力系统中的应用 技术交流:岳峰 15901092122 bjhrkc@126 ...

  4. WebService接口实际场景应用(一)

    背景:要求写一套接口测试工具.过程中遇到了WebService接口的问题,遂写下本篇文章. 阶段问题1: 需要利用数据驱动,然后读取excel中的数据并直接调用.但是webService接口与http ...

  5. Mysql学习:1、mysql安装及配置及连接Navicat

    1.下载地址: https://dev.mysql.com/downloads/windows/installer/8.0.html 2.安装流程: a.选自定义安装:custom. b. 在下一步的 ...

  6. C# 自定义组元扩展类(Tuple)

    组元Tuple没有构造函数,自定义一个有构造函数的组元TupleEx. namespace TupleEx { public class TupleEx<T1> { /// <sum ...

  7. UnsupportedOperationException异常

    看看下面的例子,这样输出什么呢? public class test { public static void main(String[] args) { String arr = "ab, ...

  8. GPS数据处理

    GPS数据处理 题目内容: NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The Nationa ...

  9. 启动Springboot 的批处理

    记下启动Springboot的批处理文本步骤:新建文本文档 > 参考下面文本内容 > 保存 > 修改后缀,作为个人笔记,提供参考: Linux  start.sh: #!/bin/s ...

  10. mysql 设置相关

    告诉mysql客户端这边的文字编码 告诉mysql希望返回的结果集编码: set character_set_client=gbk; set character_set_results=gbk;   ...