5.黑马二十四期 day05

1.强化训练-数组类封装

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
using namespace std;
class MyArray {
private:
	int m_size;//数组大小
	int m_capacity;//数组容量
	int* p;//指向存储数据的空间
public:
	MyArray() {//无参情况
		this->m_capacity = 100;
		this->m_size = 0;
		this->p = new int[this->m_capacity];//p指向一个大小为int[m_capacity]的指针,是一个数组指针
	}
	MyArray(int num) {
		this->m_capacity = num;
		this->m_size = 0;
		this->p = new int[this->m_capacity];
	}
	MyArray(MyArray& a) {
		this->m_capacity = a.m_capacity;
		this->m_size = a.m_size;
		this->p = new int[a.m_capacity];
		for (int i = 0; i < a.m_size; i++) {
			this->p[i] = a.p[i];
		}
	}
	~MyArray() {
		cout<<"析构调用"<<endl;
		delete[] this->p;//数组指针要加[]
		this->p = NULL;//防止野指针
	}
	void push_back(int val) {//尾插法
		this->p[this->m_size] = val;
		this->m_size++;
	}
	int find_index(int index) {//根据索引获取值
		return this->p[index];
	}
	void setdata(int index,int val) {//根据索引改变值
		this->p[index] = val;
	}
	int size() {
		return this->m_size;
	}

};
int main() {
	MyArray* n1 = new MyArray;
	MyArray n3;
	MyArray n2(n3);//检测拷贝函数
	delete n1;
	for (int i = 0; i < 10; i++) {
		n2.push_back(i);//检测尾插法
	}
	for (int i = 0; i < 10; i++) {
		cout<< n2.find_index(i)<<endl;//检测根索引获得值
	}
	n2.setdata(0, 520);//检测根据索引改变值
	for (int i = 0; i < 10; i++) {
		cout << n2.find_index(i) << endl;
	}
	cout<<n2.size();//调用size求当前数组长度
}

2.二元运算符加号减号的重载

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
using namespace std;
class Person {
public:
	int m_a;
	int m_b;
	Person() {//默认构造函数
		this->m_a = 0;
		this->m_b = 0;
	}
	Person(int a, int b) {
		this->m_a = a;
		this->m_b = b;
	}
	Person operator+(Person& a) {//在类中重载加号运算符
		Person p1;
		p1.m_a = a.m_a + this->m_a;
		p1.m_b = a.m_b + this->m_b;
		return p1;
	}
	Person operator-(Person& a) {//在类中重载减号运算符
		Person p1;
		p1.m_a = this->m_a- a.m_a;
		p1.m_b = this->m_b - a.m_a;
		return p1;
	}
	void input() {
		cout << "m_a:" << this->m_a << endl;
		cout << "m_b:" << this->m_b << endl;
	}
};
/*Person operator+(Person& a, Person& b) {//在全局函数中进行重载,需要类属性是公有
	Person p1;
	p1.m_a = a.m_a + b.m_a;
	p1.m_b = a.m_b + b.m_b;
	return p1;
}*/
int main(){
	Person p1(1, 1);
	Person p2(1, 1);
	p1 = p1 + p2;
	p1.input();
	p1 = p1 - p2;
	p1.input();
}

3.一元运算符负号的重载

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
using namespace std;
class Person {
public:
	int m_a;
	int m_b;
	Person() {//默认构造函数
		this->m_a = 0;
		this->m_b = 0;
	}
	Person(int a, int b) {
		this->m_a = a;
		this->m_b = b;
	}
	Person operator-() {//在类中重载-
		Person p1;
		p1.m_a = -this->m_a;
		p1.m_b = -this->m_b;
		return p1;
	}
	void input() {
		cout << "m_a:" << this->m_a << endl;
		cout << "m_b:" << this->m_b << endl;
	}
};
int main(){
	Person p1(1, 1);
	p1 = -p1;
	p1.input();
}

4.输入输出运算符,左移<<,右移>>的重载

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
using namespace std;
class Person {
public:
	int m_a;
	int m_b;
	Person() {//默认构造函数
		this->m_a = 0;
		this->m_b = 0;
	}
	Person(int a, int b) {
		this->m_a = a;
		this->m_b = b;
	}
	void input() {
		cout << "m_a:" << this->m_a << endl;
		cout << "m_b:" << this->m_b << endl;
	}
};
void operator<<(ostream &cout,Person &a) {//左移运算符的重载只能在类外进行 要使其变成形如 cout  <<  p1这种形式
	cout << "m_a:" <<a.m_a<< endl;//第一个参数是cout,第二个参数是<<,第三个参数是a
	cout << "m_b:" <<a.m_b<< endl;
}
void operator>>(istream& cin, Person& a) {//左移运算符的重载只能在类外进行 要使其变成形如 cin >>  p1.m_a这种形式
	cin >> a.m_a;//第一个参数是cin,第二个参数是>>,第三个参数是a
	cin >> a.m_b;
}
int main(){
	Person p1;
	cin >> p1;//已经对>>进行了重载
	cout << p1;//已经对<<进行了重载
}

5.自增自减运算符重载

#include <iostream>
#include <cstring>//字符串
#include<stdlib.h>
#include<cmath>//数学
#include<iomanip>//精度控制
#include<string>//字符串
#include<vector>//容器 
#include<algorithm>//算法
using namespace std;
class Person {
public:
	int m_age;
	Person() {//默认构造函数
		this->m_age = 0;
	}
	Person(int a) {
		this->m_age = a;
	}
	Person& operator++() {//前置运算符++
		this->m_age++;
		return *this;
	}
	Person& operator++(int) {//后置运算符++
		Person p2 ;
		p2.m_age=this->m_age;
		this->m_age++;
		return p2;
	}
};
void operator << (ostream & cout, Person & a) {
	cout << a.m_age << endl;
}
int main(){
	Person p1(10);
	p1++;
	cout << ++p1;
}
发布了70 篇原创文章 · 获赞 39 · 访问量 2250

猜你喜欢

转载自blog.csdn.net/weixin_45221477/article/details/105147743