C++类和对象(二)

C++类函数指针

创建

函数返回值类型 (类名::*指针名)(函数参数) 

例:void (MM::*printFunc)()=nullptr;

赋值

指针名=&类名::函数名

例如:printFunc=&MM::printMM;

#include <iostream>
#include <string>
using namespace std;
class MM 
{
public:
	void printMM() 
	{
		cout << m_name << "\t" << m_age << endl;
	}
	void set_data(string name, int age) 
	{
		m_name = name;
		m_age = age;
	}
protected:
	string m_name;
	int m_age;
};

int main() 
{
	//产生一个类成员函数指针
	void (MM::*printFunc)() = nullptr;
    //普通函数指针
	void (*pc)() = nullptr;
	printFunc = &MM::printMM;
	//错误,pc不是类成员函数指针
	//pc = &MM::printMM;
	//通过成员函数指针调用函数
	MM mm;
	(mm.*printFunc)();
	void  (MM::*setFunc)(string, int) = nullptr;
	setFunc = &MM::set_data;
	(mm.*setFunc)("小爱", 28);
	mm.printMM();
	return 0;
}

C++内存问题

两点:1.string的内存 2.空类占1字节

#include <iostream>
#include <string>
using namespace std;
class Empty 
{

};
class MM 
{
public:
	void print() 
	{
		cout << "普通函数不影响类的内存" << endl;
	}
protected:
	int age;
	double num;
};
class Boy
{
public:

protected:
	string name;
	int age;
	double num;
};

class A 
{
public:
protected:
public:
	class B 
	{
	public:

	protected:
		double b;
	};
};

int main() 
{
	cout << "空的类:" << sizeof(Empty) << endl;
	cout << "内存对齐:" << sizeof(MM) << endl;

	string str;
	cout << "string1:" << sizeof(str) << endl;
	string str1("sdsdfsdsdfsdsdfsdsdfsdsdfsdsdfsdfsdsdsdfsdsdfsdsdfsdsdfsdsdfsdsdfsdfsd");
	cout << "size:" << str1.size() << endl;
	cout << "string2:" << sizeof(str1) << endl;
	cout << "Boy:" << sizeof(Boy) << endl;
	cout << "A:" << sizeof(A) << endl;
	cout << "B:" << sizeof(A::B) << endl;
	return 0;
}

用C++类封装链表

//此示例不考虑可变参数模板来封装链表,后续可能会在专门将模板编程那块来重新实现链表
#include <iostream>
#include <string>
using namespace std;
//节点类
struct Node
{
	string m_data;    //存放数据
	Node* m_next;       //下一个节点
	//C++中结构体也有构造和析构函数,同时在C++中也可以有成员函数
	Node() = default;
	Node(string data)
	{
		m_data = data;
		m_next = nullptr;
	}
	Node(string data, Node* next)
	{
		m_data = data;
		m_next = next;
	}
};
class List
{
public:
	List(){}
	//头插法
	void push_front(string data)
	{
		//如果没有节点
		if (count == 0)
		{
			front = new Node(data);
			back = front;
			count++;
		}
		else
		{
			front = new Node(data, front);
			count++;
		}
	}
	//尾插法
	void push_back(string data)
	{
		if (count == 0)
		{
			back = new Node(data);
			front= back;
			count++;
		}
		else
		{
			back->m_next = new Node(data);
			back = back->m_next;
		}
	}
	//继续拓展:指定位置插入 删除节点
	void printNode()
	{
		Node* pmove = front;
		while (pmove != nullptr)
		{
			cout << pmove->m_data << endl;
			pmove = pmove->m_next;
		}
	}

protected:
	Node* front;  //前一个节点
	Node* back;   //后一个节点
	int count;    //节点个数
};
int main()
{
	List list;
	//打印结果:大帅哥 超级 我是
	list.push_front("我是");
	list.push_front("超级");
	list.push_front("大帅哥");
	//打印结果:我是 超级 大帅哥  
	list.push_back("我是");
	list.push_back("超级");
	list.push_back("大帅哥");
	list.printNode();
	return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值