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

news/2024/10/16 7:22:55 标签: 学习, 链表, 数据结构, c++,

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.niftyadmin.cn/n/5707554.html

相关文章

AI大模型时代:这20种职业可能首当其冲面临替代风险

随着ChatGPT为代表的大语言模型人工智能技术发展&#xff0c;“机器替代人工”的话题在最近半年持续火热。 近日&#xff0c;北京大学国家发展研究院联合智联招聘&#xff0c;发布了《AI大模型对我国劳动力市场潜在影响研究》报告&#xff0c;并首次构建了各种职业的人工智能影…

第三方软件检测机构收费标准是怎样的?

随着信息时代的发展&#xff0c;软件产品为我们带来了极大便利&#xff0c;产品数量也逐渐增多&#xff0c;因此软件企业为了更好的在市场上竞争&#xff0c;在产品上线前的软件检测必不可少。软件检测一般有两种方式&#xff1a;一种是公司自有团队进行检测工作&#xff0c;另…

fork中的死锁问题

背景 当我们通过fork去创建子进程时&#xff0c;当父/子进程都涉及到锁的操作&#xff0c;可能会产生死锁。 代码样例 #include <iostream> #include <mutex> #include <unistd.h> std::mutex m; int main() {std::cout << "main process begi…

ROS2初级面试题汇总

大家好&#xff0c;我是小白小帅&#xff0c;在日常基于ros2开发移动机器人和学习过程中&#xff0c;个人总结和收集了一些关于ros2的问题&#xff08;共25道&#xff09;&#xff0c;这些问题也是面试中可能涉及到的一些知识点&#xff0c;对于我个人就是一个简单的记录&#…

VS如何修改生成的exe程序的名称

一、简述 在日常开发中&#xff0c;我们一般会将工程名称设置为生成的exe程序的名称&#xff0c;在VS编译器中&#xff0c;默认生成的exe名称取自工程名称&#xff0c;所以如果是已经建立好工程&#xff0c;想要修改生成的exe名称&#xff0c;可以通过工程属性修改&#xff1a…

学习vue20.17.0-列表渲染

列表渲染 官方中文网站:列表渲染 | Vue.js (vuejs.org) v-for 我们可以使用 v-for 指令基于一个数组来渲染一个列表。v-for 指令的值需要使用 item in items 形式的特殊语法,其中 items 是源数据的数组,而 item 是迭代项的别名:演示代码: js 代码: const items = ref([{…

Python案例 | 测试网络的下载速度上传速度和 ping 延迟

使用了 speedtest 库来测试网络的下载速度上传速度和 ping 延迟 注意&#xff0c;这里需要先卸载speedtest&#xff0c;再安装speedtest-cli pip uninstall speedtest pip install speedtest-cli其次运行代码&#xff1a; # 使用了 speedtest 库来测试网络的下载速度上传速度…

PetaLinux工程的常用命令——petalinux-create

petalinux-create&#xff1a;此命令创建新的PetaLinux项目或组件。 注&#xff1a;有些命令我没用过&#xff0c;瞎翻译有可能会翻译错了&#xff0c;像是和fpgamanager相关的部分。 用法: petalinux-create [options] <-t|--type <TYPE> <-n|--name <COMPONEN…