面向对象编程OOP实现顺序栈 字符串 循环队列 及面向对象知识点

class SeqStack {
public:
	SeqStack(int size=10) {
		cout << this << " " << "SeqStack()" << endl;
		_pstack = new int[size];
		_top = -1;
		_size = size;
	}
	SeqStack(const SeqStack& src) {
		cout << this << "  " << "SeqStack(const SeqStack& src)" << endl;
		_pstack = new int[src._size];
		for (int i = 0; i <= src._top; i++) {
			_pstack[i] = src._pstack[i];
		}
		_top = src._top;
		_size = src._size;
	}
	void operator=(const SeqStack& src) {
		cout << this << " " << "void operator=(const SeqStack& src)" << endl;
		if (this == &src)return;//防止自赋值
		delete[]_pstack;//释放原来的内存空间
		_pstack = new int[src._size];
		for (int i = 0; i <= src._top; i++) {
			_pstack[i] = src._pstack[i];
		}
		_top = src._top;
		_size = src._size;
	}
	void push(int val) {
		if (full() ){
			resize();
		}
		_pstack[++_top] = val;
	}
	void pop() {
		if (empty())return;
		--_top;
	}
	int top() {
		return _pstack[_top];
	}
	bool empty() {
		return _top == - 1;
	}
	bool full() {
		return _top == _size - 1;
	}
	~SeqStack() {
		cout << this << " " <<"~SeqStack()" << endl;
		delete[]_pstack;
		_pstack = nullptr;
	}
private:
	int* _pstack;
	int _top;//指向栈顶元素的后继
	int _size;
	void resize() {
		int* ptmp = new int[_size * 2];
		for (int i = 0; i < _size; i++) {
			ptmp[i] = _pstack[i];
		}
		_pstack = ptmp;
		_size *= 2;
	}
};
SeqStack st2;
int main() {
	SeqStack st;
	for (int i = 0; i < 20; i++) {
		st.push(rand() % 100);
	}
	/*while (!st.empty()) {
		cout << st.top() << " ";
		st.pop();
	}*/
	cout << endl;
	SeqStack* ps = new SeqStack(40);//new=先malloc开辟内存+再Seqstack(40)构造函数
	ps->push(20);
	ps->push(52);
	ps->pop();
	delete ps;//delete=先~SeqStack()析构函数 释放外部的堆内存+再free()释放成员变量占用的栈空间
	SeqStack s1 = st;
	SeqStack s2;
	s2 = s1;
	while (!s2.empty()) {
		cout << s2.top() << " ";
		s2.pop();
	}
	cout << endl;
	return 0;
}
class String {
public:
	String(const char* str = nullptr) {
		if (str != nullptr) {
			m_data = new char[strlen(str) + 1];
			strcpy(m_data, str);
		}
		else {
			m_data = new char[1];
			*m_data = '\0';
		}
	}
	~String() {
		delete[] m_data;
		m_data = nullptr;
	}
	String(const String& src) {
		m_data = new char[strlen(src.m_data)+ 1];
		strcpy(m_data, src.m_data);
	}
	String& operator=(const String& src) {
		if (this == &src)return *this;
		delete []m_data;
		m_data = new char[strlen(src.m_data) + 1];
		strcpy(m_data, src.m_data);
		return *this;
	}
	void show() {
		cout << m_data << endl;
	}
private:
	char* m_data;
};
int main() {
	String s1("jhssdh");
	s1.show();
	String s2 = "gh";
	s2.show();
	s1 = s2;
	s1.show();
	return 0;
}
class Queue {
public:
	Queue(int size=10) {
		_pQue = new int[size];
		_front = _rear = 0;
		_size = size;
	}
	~Queue() {
		delete[]_pQue;
		_pQue = nullptr;
	}
	Queue(const Queue& src) {
		_front = src._front;
		_rear = src._rear;
		_size = src._size;
		_pQue = new int[src._size];
		for (int i = _front; i != _rear; i = (i + 1) % _size) {
			_pQue[i] = src._pQue[i];
		}
	}
	Queue& operator=(const Queue& src) {
		if (this == &src) {
			return *this;
		}
		delete []_pQue;
		_front = src._front;
		_rear = src._rear;
		_size = src._size;
		_pQue = new int[src._size];
		for (int i = _front; i != _rear; i = (i + 1) % _size) {
			_pQue[i] = src._pQue[i];
		}
	}
	void push(int val) {
		if (full()) {
			resize();
		}
		_pQue[_rear] = val;
		_rear = (_rear + 1) % _size;
	}
	void pop() {
		if (empty())return;
		_front = (_front + 1) % _size;
	}
	bool empty() {
		return _front == _rear;
	}
	bool full() {
		return _rear == (_front + 1) % _size;
	}
	int front() {
		return _pQue[_front];
	}
private:
	int* _pQue;
	int _front;
	int _rear;//指向最后一个元素的后继
	int _size;
	void resize() {
		int* ptmp = new int[_size * 2];
		int index = 0;
		for (int i = _front; i != _rear; i=(i+1)%_size) {
			ptmp[index++] = _pQue[i];
		}
		_pQue = ptmp;
		_front = 0;
		_rear = index;
		_size *= 2;
	}
};
int main() {
	Queue q1(5);
	for (int i = 0; i < 20; i++) {
		q1.push(rand() % 100);
	}
	Queue q2 = q1;
	Queue q3;
	q3 = q2;
	while (!q1.empty()) {
		cout << q1.front() << " ";
		q1.pop();
	}
	cout << endl;
	while (!q3.empty()) {
		cout << q3.front() << " ";
		q3.pop();
	}
	cout << endl;
	return 0;
}
class CDate {
public:
	CDate(int y,int m,int d)
		:_year(y)
		,_month(m)
		,_day(d)
	{}
	void show() {
		cout << "year:" << _year << " " << 
		"month:" << _month << " " << "day:" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
class CGoods {
public:
	CGoods(const char* n, double p, int a, int y, int m, int d)
		:_date(y,m,d)
		, _price(p)
		, _amount(a)
	{
		strcpy(_name, n);
	}
	void show() {
		cout << "name:" << _name << " " << " price:" << _price 
			<< " " << "amount:" << _amount <<" ";
		_date.show();
	}
private:
	char _name[20];
	double _price;
	int _amount;
	CDate _date;//成员对象
};
int main() {
	CGoods good1("大米", 20.5, 50, 2024, 1, 23);
	good1.show();
	return 0;
}
class Test {
public:
	void func() {
		cout << "void func()" << endl;
	}
	static void static_func() {
		cout << "static void static_func()" << endl;
	}
	int ma;
	static int mb;
};
int Test::mb;
int main() {
	Test t1;
	Test* t2 = new Test();
	void(Test:: * pfunc)() = &Test::func;
	(t1.*pfunc)();
	(t2->*pfunc)();
	void (*pstatic_func)() = &Test::static_func;
	(*pstatic_func)();
	int Test::* p = &Test::ma;
	t1.*p = 20;
	cout << t1.*p << endl;
	t2->*p = 80;
	cout << t2->*p << endl;
	int * p1 = &Test::mb;
	*p1 = 100;
	cout << *p1 << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值