7.类组合

  1. 以另一个类的对象为数据成员

     
    1. 构造函数的写法,必须采用初始化参数列表的写法

    2. #include<iostream>
      #include <string>
      using namespace std;
      
      class Hero {
      public:
      	Hero(string name="", int level=0) :name(name), level(level) {}
      	void print() {
      		cout << name << " " << level << endl;
      	}
      
      protected:
      	string name;
      	int level;
      private:
      	void printData() {
      		cout << "类外无法调用" <<endl;
      	}
      };
      
      class Heroine {
      public:
      
      	Heroine(string name, int level,string mname, int mlevel) :hero(mname, mlevel) {
      		this->name = name;
      		this->level = level;
      	}
      	Heroine(string name, int level) :hero("盖伦",18) {
      		this->name = name;
      		this->level = level;
      	}
      	void print() {
      		hero.print();
      		//hero.printData();//类外无法调用
      		cout << name << " " << level << endl;
      	}
      protected:
      	string name;
      	int level;
      	Hero hero;
      private:
      
      };
      
      int main() {
      	Heroine heroine1("辛德拉", 17,"赵信",16);
      	heroine1.print();
      	Heroine heroine2("雷欧娜", 17);
      	heroine2.print();
      	return 0;
      }
  2. 类的组合构造顺序问题

    1.  构造顺序只和定义对象顺序的有关,和初始化参数列表无参

    2. #include<iostream>
      #include <string>
      using namespace std;
      class A {
      public:
      	A(string str="") :str(str) {
      		cout << str << " ";
      	}
      	void print() {
      		cout << str << " ";
      	}
      protected:
      	string str;
      private:
      };
      class B {
      public:
      	B(string str="") :str(str) {
      		cout << str << " ";
      	}
      	void print() {
      		cout << str << " ";
      	}
      protected:
      	string str;
      private:
      };
      class C {
      public:
      	C(string str="") :str(str) {
      		cout << str << " ";
      	}
      	void print() {
      		cout << str << " ";
      	}
      protected:
      	string str;
      private:
      };
      class D {
      public:
      	D(string Astr, string Bstr, string Cstr) :a(Astr),b(Bstr),c(Cstr){}
      	void print() {
      		cout << "D" << " ";
      	}
      protected:
      	C c;
      	A a;
      	B b;
      private:
      };
      int main() {
      	D d("A", "B", "C");
      	d.print();
      	return 0;
      }

  3. 类中类

    • 依旧受权限限定

    • 访问方式,需要类名限定

    • #include<iostream>
      #include <string>
      using namespace std;
      
      struct Node 
      {
      	Node(int data=0) :data(data) {
      		this->next = nullptr;
      	}
      	int data;
      	Node* next;
      };
      
      class List
      {
      public:
      	List() {
      		pList = nullptr;
      	}
      	List(int data) {
      		pList = new Node;
      		if (pList==nullptr)
      		{
      			cout << "内存申请失败" << endl;
      		}
      		pList->data = data;
      		pList = nullptr;
      	}
      	~List()
      	{
      		delete pList;
      		pList = nullptr;
      	}
      	void insertHead(int data)
      	{
      		if (pList==nullptr)
      		{
      			Node* pNewNode = new Node(data);
      			pList = pNewNode;
      		}
      		else
      		{
      			Node* pNewNode = new Node(data);
      			pNewNode->next = pList;
      			pList = pNewNode;
      		}
      	}
      	//类中类
      	//迭代器-->类模仿指针行为
      	class iterator {
      	public:
      		iterator() {
      			pMove = nullptr;
      		}
      		iterator(Node* pMove) :pMove(pMove) {
      
      		}
      		void operator=(Node* pMove) {
      			this->pMove = pMove;
      		}
      		bool operator!=(Node* pMove) {
      			return this->pMove != pMove;
      		}
      		iterator operator++(int) {
      			pMove = pMove->next;
      			return iterator(pMove);
      		}
      		Node*& operator&() {
      			return pMove;
      		}
      
      	protected:
      		Node* pMove;
      
      	private:
      
      	};
      	Node* begin()
      	{
      		return pList;
      	}
      	Node* end()
      	{
      		return nullptr;
      	}
      
      protected:
      	Node* pList;
      private:
      };
      
      int main() {
      	List list;
      	for (int i = 0; i < 5; i++)
      	{
      		list.insertHead(i);
      	}
      	List::iterator iter;
      	for (iter=list.begin();iter!=list.end();iter++)
      	{
      		cout << (&iter)->data << " ";
      	}
      	return 0;
      }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值