C++默认函数和类的组合07

C++默认函数和类的组合

                        完成优先于完美

直接上代码:

  • 什么类的组合?
  • <通过把别人的对象当做自己成员,然后访问“可访问”成员>
    以另一个类的对象为数据成员
  • 类的组合:
    • 构造函数的写法,必须采用初始化参数列表的写法
#include"iostream"
#include"string"
using namespace std;
class Boy{
public:
	Boy(string name, int age) :name(name), age(age) { "construction funtion\n"; }
	void print()
	{
		cout << name << " " << age << endl;
	}
protected:
	void pringdata()
	{
		cout << age << '\t' << name<<endl;
	}
	int age;
	string name;
};
class girl {
public:
	// 必须用初始化参数列表
	girl(string boyname, int boyage, string gname) :boy(boyname, boyage) //init combine object
	{
		this->gname = gname;// init own object
	}
	// 可以合并一起写,或下面分开重载;设置默认数据
	girl(string gname) :gname(gname),boy("Bigboy",22) {}
	void print()
	{
		boy.print();
		//boy.printdata();  不可访问,因为boy对于Boy来说类外
		cout << gname << endl;

	}
protected:
	string gname;
	Boy boy;// only can create a none argument object
};
int main()
{
	Boy b("YY",21);
	b.print();
	girl G("boy",19,"G");
	G.print();
	girl g = { "ff" };
	g.print();

	
	return 0;
}

在这里插入图片描述

类的组合构造顺序问题:

  • 构造顺序只和定义对象顺序的有关,和初始化参数列表无参
#include"iostream"
#include"string"
using namespace std;
class A {
public:
	A(string aa) :a(aa) { cout << a << endl; }
protected:
	string a;
};
class B{
public:
	B(string bb) :b(bb) { cout << b << endl; }
protected:
	string b;
};
class C {
public:
	C(string cc) :c(cc) { cout << c << endl; }
protected:
	string c;
};
class D {
public:
	D(string a,string b,string c) :a(a),b(b),c(c) { cout << 'D' << endl; }
protected:
	A a;
	B b;
	C c;
};
int main() {

	D d("A", "B", "C");
	return 0;
}

在这里插入图片描述

类中类

  • 依旧受权限限定
  • 访问方式,需要类名限定
#include<iostream>
#include<string>
using namespace std;
struct Node 
{
	int data;
	Node* next;
};
class List {
public:
protected:
	Node* headnode;//看做数据
private:
	class iterator {
	public:

	protected:
    // 类中类
	};

};
int main()
{
	List::iterator iter;// 不可访问
	return 0;
}

在这里插入图片描述
private ----> public is OK;
迭代器:类模仿指针行为
可以用类中类来 遍历链表

#include <iostream>
using namespace std;
struct Node 
{
	int data;
	Node* next;
	Node()
	{
		this->next = nullptr;
	}
	Node(int data) 
	{
		this->next = nullptr;
		this->data = data;
	}
};
class List 
{
public:
	List() 
	{
		headNode = new Node;
	}
	void push_front(int data) 
	{
		Node* newNode = new Node(data);
		newNode->next = headNode->next;
		headNode->next = newNode;
	}
protected:
	Node* headNode;
public:
	//迭代器-->类模仿指针行为
	class iterator 
	{
	public:
		iterator(Node* pMove=nullptr) :pMove(pMove) {}	
		void operator=(Node* pMove) 
		{
			this->pMove = pMove;
		}
		bool operator!=(Node* pMove) 
		{
			return this->pMove != pMove;
		}
		iterator operator++()
		{
			pMove = pMove->next;
			return iterator(pMove);
		}
		Node*& operator*() 
		{
			return pMove;
		}
	protected:
		Node* pMove;
	};
	Node* begin()
	{
		return headNode->next;
	}
	Node* end() 
	{
		return nullptr;
	}
};
//类中枚举类型
class  A 
{
public:
	enum time {first,second};
protected:
	enum date {mon,sur,tus};
	//类中的枚举类型受权限限定
};

int main() 
{
	List list;
	for (int i = 0; i < 3; i++) 
	{
		list.push_front(i);
	}
	List::iterator iter;
	for (iter = list.begin(); iter != list.end(); ++iter)
	{
		cout << (*iter)->data;
	}
	//cout << A::date::mon << endl; 不可访问
	cout << A::time::first << endl;
	return 0;
}

在这里插入图片描述

C++类中默认的函数

  • 默认构造函数
  • 默认拷贝构造函数
  • 默认析构函数
  • 默认赋值运算
#include <iostream>
using namespace std;
class A
{
public:
	A() = default;  //调出默认函数
	A(A& object) = default;  //默认拷贝构造函数 对对object引用
	//void print() = default;  它 普通函数不存在默认函数
	A& operator=(A& object) = default;  // 等号赋值运算也是有默认函数
	~A() = default;
};
int main()
{
	A a;
	A b = a;
	A c;
	c = a;
	return 0;
}

在这里插入图片描述

总结

迭代器:类模仿指针行为
可以用类中类来 遍历链表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值