当前位置: 首页 > article >正文

类的初步学习(关于类的私有与公有成员、类链表、析构函数、友元函数)

1.关于类的私有与公有成员

#include<iostream>
using namespace std;
class Node
{
	int data;//私有数据成员可以在该类的公有函数里面调用,但是在main函数下不可调用
public:
	void print(int n);
};
void Node::print(int n)
{
	data = n;
	cout << data;
}
int main()
{
	Node s;
	int n, a, b;
	cin >> n;
	s.print(n);
	return 0;
}
  1. 类链表
#include<iostream>
using namespace std;
class Node
{
public:
	int data;
	Node* next;
};
class List
{
	
public:
	Node* head;
	void createlist1(Node*&head,int a);//建立逆序链表
	void createlist2(Node*&head,int b);//建立正序链表
	void showlist(Node*head);//遍历链表
};
void List::createlist1(Node*&head,int a)
{
	Node* s = new Node;
	Node* p, * q;
	s->data = a;
	s->next = NULL;
	if (head == NULL)
	{
		head = s;
		return;
	}
	if (head->data < s->data)
	{
		s->next = head;
		head = s;
		return;
	}
	for (q = head, p = head->next; p; q = p, p = p->next)
	{
		if (p->data < s->data)
		{
			s->next = p;
			q->next = s;
			return;
		}
	}
	q->next = s;
	return;
}
void List::createlist2(Node*& head, int b)
{
	Node* s = new Node;
	Node* p, * q;
	s->data = b;
	s->next = NULL;
	if (head == NULL)
	{
		head = s;
		return;
	}
	if (head->data > s->data)
	{
		s->next = head;
		head = s;
		return;
	}
	for (q = head, p = head->next; p; q = p, p = p->next)
	{
		if (p->data > s->data)
		{
			s->next = p;
			q->next = s;
			return;
		}
	}
	q->next = s;
	return;
}
void List::showlist(Node*head)
{
	while (head)
	{
		cout << head->data << ' ';
		head = head->next;
	}
}

int main()
{
	int n, a, b;
	while (1)
	{
		cin >> n;
		if (n == 0) break;
		if (n == 1)
		{
			List l1;
			l1.head = NULL;
			cin >> a;
			while (a != 0)
			{
				l1.createlist1(l1.head, a);
				cin >> a;
			}
			l1.showlist(l1.head);
			cout << endl;
		}
		if (n == 2)
		{ 
			List l2;
			l2.head = NULL;
			cin >> b;
			while (b != 0)
			{
				l2.createlist2(l2.head,b);
				cin >> b;
			}
			l2.showlist(l2.head);
			cout << endl;
		}
	}
	return 0;
}
  1. 析构函数
    delete指针和不delete指针的区别

#include <iostream>
using namespace std;
class A
{
public:
	A()
	{
		cout << "As cons." << endl;
	}
	virtual ~A()
	{
		cout << "As des." << endl;
	}
	virtual void f()
	{
		cout << "As f()." << endl;
	}
	void g()
	{
		f();
	}
};
class B :public A
{
public:
	B()
	{
		f(); cout << "Bs cons." << endl;
	}
	~B()
	{
		cout << "Bs des." << endl;
	}
};
class C :public B
{
public:
	C()
	{
		cout << "Cs cons." << endl;
	}
	~C()
	{
		cout << "Cs des." << endl;
	}
	void f()
	{
		cout << "Cs f()." << endl;
	}
};
void main()
{
	A* a = new C;
	a->g();
	/*delete a;*/
}
  1. 友元函数

#include <iostream>
using namespace std;
class box
{
private:
	int color;
	int upx, upy;
	int lowx, lowy;
public:
	friend class line;
		void set_color(int c) { color = c; }
	void define_box(int x1, int y1, int x2, int y2)
	{
		upx = x1; upy = y1; lowx = x2; lowy = y2;
	}
};
class line
{
private:
	int color;
	int startx, starty;
	int endx, endy;
public:
	friend int same_color(line l, box b);
	void set_color(int c) { color = c; }
	void define_line(int x1,int y1,int x2,int y2)
	{
		startx = x1; starty = y1; endx = x2; endy = y2;
	}
};
int same_color(line l, box b)
{
	if (l.color == b.color) return 1;
	return 0;
}

不用友元函数就会报错(用的友元类)
在这里插入图片描述

正确代码:

#include <iostream>
using namespace std;
class line;
class box
{
private:
	int color;
	int upx, upy;
	int lowx, lowy;
public:
	friend int same_color(line l, box b);//补空缺
		void set_color(int c) { color = c; }
	void define_box(int x1, int y1, int x2, int y2)
	{
		upx = x1; upy = y1; lowx = x2; lowy = y2;
	}
};
class line
{
private:
	int color;
	int startx, starty;
	int endx, endy;
public:
	friend int same_color(line l, box b);
	void set_color(int c) { color = c; }
	void define_line(int x1,int y1,int x2,int y2)
	{
		startx = x1; starty = y1; endx = x2; endy = y2;
	}
};
int same_color(line l, box b)
{
	if (l.color == b.color) return 1;
	return 0;
}

http://www.kler.cn/a/349392.html

相关文章:

  • 【读书笔记·VLSI电路设计方法解密】问题17:什么是封装
  • pytorch学习笔记
  • Nginx:proxy_pass指令
  • QJniObject--Qt中的Java交互类
  • Codeforces Round 923 (Div. 3) F题 Microcycle(生成树,并查集,DFS)
  • PHP MySQL 简介
  • docker tar包安装 docker-26.1.4.tgz
  • [权威出版|稳定检索]2024年大数据经济与公共管理国际会议(BDEPM 2024)
  • 算法练习:查找总价格为目标值的两个商品
  • 超强的开源OCR工具Surya更新了表识别功能!GitHub收藏人数超过1万。
  • java项目之纺织品企业财务管理系统源码(springboot+vue+mysql)
  • RocketMq详解:五、SpringBoot+Aop实现RocketMq的幂等
  • vue-seamless-scroll插件实现无缝滚动
  • 【安装JDK和Android SDK】
  • 小猿口算辅助工具(nodejs版)
  • 基于Python flask的豆瓣电影可视化系统,豆瓣电影爬虫系统
  • 27.数据结构与算法-图的遍历(DFS,BFS)
  • Debug-028-el-carousel走马灯-当展示图片为2的问题处理
  • 大学新生入门编程的推荐路径
  • 输电线路语义分割图像数据集,图片总共1200张左右,包含分割标签,json标签