4. C++类的组合

简介: 4. C++类的组合

C++类的组合

什么是类的组合

类的组合就是以另一个的对象为数据成员,这种情况称之为类的组合

  • 优先使用组合而不是继承
  • 组合表达式的含义一部分的关系

初始化参数列表

初始化参数列表是构造函数的另一种写法

应用场景:

  • 形参名和数据成员相同,避免二义性问题
  • 类和组合 必须要初始化参数列表的方式写构造
  • 常数据成员必须采用初始化参数列表的方式
  • 继承中子类的构造函数也必须初始化参数列表的方式

初始化参数列表基本形态

构造函数名(形参1,形参2....):数据成员1(形参1),数据成员2(形参2)...

初始化参数列表其他形态

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
  //初始化参数列表
  //可以避免形参名和数据成员名字相同
  MM(string name, int age) :name(name), age(age)
  {}
  MM(string name) 
  {
    MM::name = name;    //用类名标识一下帮助IDE去识别
  }
  void print() 
  {
    cout << name << "\t" << age << endl;
  }
private:
  string name;
  int age;
};
//初始化参数列表其他写法
string name = "initName";
int returnValue() 
{
  return 23;
}
class Boy 
{
public:
  //无参构造函数
  Boy() :name("Boy"), age(111) 
  {
  }
  //::name标识name是全局的
  Boy(int age) :name(::name), age(returnValue())
  {
    cout << age << endl;   //就近原则
  }
  void  print() 
  {
    cout << name << "\t" << age << endl;
  }
private:
  string name;
  int age;
};
int main() 
{
  MM mm("name", 18);
  mm.print();
  Boy boy;
  boy.print();
  Boy pInt(12);
  pInt.print();
  return 0;
}

类的组合案例分析

  • 类组合包含的类的对象,必须采用初始化参数列表方式调用各自类当中的构造函数去初始化
  • 组合中初始化参数列表的写法
  • 要通过包含的类的构造函数决定组合类的构造函数怎么写
构造函数名(形参1,形参2,形参3....):对象1(形参1,形参2),对象2(形参3)...

组合类注意问题

  • 组合类必须要调用包含对象所属类的构造函数
  • 形式上看不到包含对象所属类构造函数调用,必须准备无参的构造函数
#include <iostream>
using namespace std;
//一种的关系
//一部分:组合
class Button 
{
public:
  Button()
  {
    cout << "Button" << endl;
  }
  Button(int x, int y, int w, int h) :x(x), y(y), w(w), h(h)
  {
    cout << "Button" << endl;
  }
  void Draw() 
  {
    cout << "按钮..." << endl;
  }
private:
  int x;
  int y;
  int w;
  int h;
};
class Edit 
{
public:
  Edit()
  {
    cout << "Edit" << endl;
  }
  Edit(int x, int y) :x(x), y(y) 
  {
    cout << "Edit" << endl;
  }
  void Draw() 
  {
    cout << "编辑框...." << endl;
  }
private:
  int x;
  int y;
};
class Label 
{
public:
  Label() 
  {
    cout << "Label" << endl;
  }
  Label(int x, int y, string text) :x(x), y(y), text(text)
  {
    cout << "Label" << endl;
  }
  void Draw() 
  {
    cout << "标签:" << text << endl;
  }
private:
  int x;
  int y;
  string text;
};
class Window 
{
public:
  //window():button(),label(),edit(){}
  Window()    //形式上没有调用,实际构造对象,必定调用包含对象的无参构造函数
  {
  }
  Window(int bx, int by, int bw, int bh, int lx, int ly, string text, int ex, int ey)
    :button(bx, by, bw, bh),edit(ex, ey), label(lx, ly, text)
  {
  }
  void Show() 
  {
    button.Draw();
    label.Draw();
    edit.Draw();
  }
  Button getButton() { return button; }
  Label  getLabel() { return label; }
  Edit getEdit() { return edit; }
private:
  //以其他类的对象为数据成员
  //构造顺序只和此处有关,和初始化参数列表顺序无关
  Button button;
  Label label;
  Edit edit;
};
//另一种包含指针写法
class A
{
public:
  A(int a) :a(a) {}
  int geta() { return a; }
private:
  int a;
};
class B 
{
public:
  B(int b) :b(b) {}
  int getb() { return b; }
private:
  int b;
};
class C 
{
public:
  C() 
  {
    pa = new A(12);
    pb = new B(123);
  }
  C(int a, int b) :pa(new A(a)), pb(new B(b)) {}
  void visitData() 
  {
    cout << pa->geta() << endl;
    cout << pb->getb() << endl;
  }
private:
  A* pa;
  B* pb;
};
int main() 
{
  Window object;    //优先构造包含对象,在构造自身对象
  //object.getButton().Draw();
  //object.getEdit().Draw();
  //object.getLabel().Draw();
  object.Show();
  Window window(10, 10, 10, 10, 20, 20, "Label", 30, 30);
  window.Show();
  C c;
  c.visitData();
  C value(1, 2);
  value.visitData();
  return 0;
}

组合中构造和析构顺序问题

  • 一般构造顺序和析构是相反
  • 类的组合中,优先构造包含对象,在构造自身对象
  • 类的组合中,包含对象的构造顺序只和定义顺序有关,和初始化参数列表无关
#include <iostream>
using namespace std;
class A
{
public:
  A() { cout << "A"; }
  ~A() { cout << "A" ; }
};
class B
{
public:
  B() { cout << "B"; }
  ~B() { cout << "B"; }
};
class C
{
public:
  C() { cout << "C"; }
  ~C() { cout << "C"; }
};
class D 
{
public:
  D() { cout << "D"; }    //初始化参数列表写出去迷惑
  ~D() { cout << "D"; }
  A a;    //A
  B b;    //B
  C c;    //C
        //D
};
int main() 
{
  {
    D d;
  }
  return 0;
}

this指针

任何类中都存在一个this指针,this指针只允许在类中函数的函数中使用

this指针代表的是每一个对象抽象地址

基本用法

避免形参名和数据成员的名相同

#include <iostream>
#include <string>
using namespace std;
class MM 
{
public:
  MM(string name, int age);
  void modifyData(string name, int age) 
  {
    //MM::name = name;
    //MM::age = age;
    this->name = name;
    this->age = age;
    cout << this << endl;
  }
  void print();
protected:
  string name;
  int age;
};
//初始化参数列表类外也行
MM::MM(string name, int age) :name(name), age(age) 
{
}
void MM::print() 
{
  cout << this->name << "\t" << this->age << endl;
}
int main() 
{
  MM mm("mm", 18);
  mm.modifyData("MM", 28);  //this=&mm;
  cout << "&mm:" << &mm << endl;
  MM girl("girl", 19);
  girl.modifyData("girl", 29);//this=&girl; 
  cout << "&girl:" << &girl << endl;
  return 0;
}

其他作用

操作对象自身做一些事情

  • 返回对象本身函数
  • 返回对象本身的地址
#include <iostream>
#include <string>
using namespace std;
class MM 
{
public:
  MM(string name, int age);
  void modifyData(string name, int age) 
  {
    //MM::name = name;
    //MM::age = age;
    this->name = name;
    this->age = age;
    cout << this << endl;
  }
  MM& returnMM() 
  {
    return *this;
  }
  MM* returnMMPoint() 
  {
    return this;
  }
  void print();
protected:
  string name;
  int age;
};
//初始化参数列表类外也行
MM::MM(string name, int age) :name(name), age(age) 
{
}
void MM::print() 
{
  cout << this->name << "\t" << this->age << endl;
}
int main() 
{
  MM mm("mm", 18);
  mm.modifyData("MM", 28);  //this=&mm;
  cout << "&mm:" << &mm << endl;
  MM girl("girl", 19);
  girl.modifyData("girl", 29);//this=&girl; 
  cout << "&girl:" << &girl << endl;
  mm.returnMM().returnMM().returnMM().returnMM().print();
  mm.returnMMPoint()->returnMMPoint()->returnMMPoint()->print();
  mm.print();
  mm.returnMMPoint()->print();
  return 0;
}

类中类

类中类就是一个类定义在另一个类当中

  • 掌握访问类中类中即可
  • 掌握类中的函数在类实现的写法
#include <iostream>
using namespace std;
struct Node
{
  int data;
  Node* next;
  Node() :next(nullptr) {}
  Node(int data) :data(data), next(nullptr) {}
  Node(int data, Node* next) :data(data), next(next) {}
};
class List
{
public:
  List();
  void insertData(int data);
  void printList() 
  {
    Node* pmove = headNode->next;
    while (pmove != nullptr)
    {
      cout << pmove->data << " ";
      pmove = pmove->next;
    }
    cout << endl;
  }
  Node* begin()
  {
    return headNode;
  }
private:
  Node* headNode;
public:
  //类中类
  class Iterator 
  {
  public:
    Iterator(Node* pmove=nullptr);
  private:
    Node* pmove;
  };
};
List::List() 
{
  headNode = new Node;
}
void List::insertData(int data)
{
  headNode->next = new Node(data, headNode->next);
}
//类中类的访问剥洋葱
List::Iterator::Iterator(Node* pmove):pmove(pmove)
{
}
int main() 
{
  List list;
  List::Iterator it=list.begin();
  list.insertData(1);
  list.insertData(2);
  list.insertData(3);
  list.printList();
  return 0;
}

小试牛刀

//自己想一个组合案案例,写一下测试代码
//准备综合类
//多个分支类
//每一个实现一下
//案例1
//给孩子起名字
//父亲类
  //姓
  //名
//母亲类
  //姓
  //名
//孩子类-->综合类
  //姓 =父姓+母姓
  //名 =自己传一个
//案例2
//头部描述
//头类-->综合类
//眼睛 -->string描述什么样的眼睛
//鼻子 -->string描述什么样的眼睛  
//耳朵 -->string描述什么样的眼睛
//自由发挥也行
//........
目录
相关文章
|
3天前
|
设计模式 安全 编译器
【C++11】特殊类设计
【C++11】特殊类设计
22 10
|
8天前
|
C++
C++友元函数和友元类的使用
C++中的友元(friend)是一种机制,允许类或函数访问其他类的私有成员,以实现数据共享或特殊功能。友元分为两类:类友元和函数友元。类友元允许一个类访问另一个类的私有数据,而函数友元是非成员函数,可以直接访问类的私有成员。虽然提供了便利,但友元破坏了封装性,应谨慎使用。
39 9
|
3天前
|
存储 编译器 C语言
【C++基础 】类和对象(上)
【C++基础 】类和对象(上)
|
12天前
|
编译器 C++
【C++】string类的使用④(字符串操作String operations )
这篇博客探讨了C++ STL中`std::string`的几个关键操作,如`c_str()`和`data()`,它们分别返回指向字符串的const char*指针,前者保证以&#39;\0&#39;结尾,后者不保证。`get_allocator()`返回内存分配器,通常不直接使用。`copy()`函数用于将字符串部分复制到字符数组,不添加&#39;\0&#39;。`find()`和`rfind()`用于向前和向后搜索子串或字符。`npos`是string类中的一个常量,表示找不到匹配项时的返回值。博客通过实例展示了这些函数的用法。
|
12天前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `&lt;`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。
|
17天前
|
C++
【C++】日期类Date(详解)②
- `-=`通过复用`+=`实现,`Date operator-(int day)`则通过创建副本并调用`-=`。 - 前置`++`和后置`++`同样使用重载,类似地,前置`--`和后置`--`也复用了`+=`和`-=1`。 - 比较运算符重载如`&gt;`, `==`, `&lt;`, `&lt;=`, `!=`,通常只需实现两个,其他可通过复合逻辑得出。 - `Date`减`Date`返回天数,通过迭代较小日期直到与较大日期相等,记录步数和符号。 ``` 这是236个字符的摘要,符合240字符以内的要求,涵盖了日期类中运算符重载的主要实现。
|
12天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。
|
12天前
|
C++
C++】string类的使用③(修改器Modifiers)
这篇博客探讨了C++ STL中`string`类的修改器和非成员函数重载。文章介绍了`operator+=`用于在字符串末尾追加内容,并展示了不同重载形式。`append`函数提供了更多追加选项,包括子串、字符数组、单个字符等。`push_back`和`pop_back`分别用于在末尾添加和移除一个字符。`assign`用于替换字符串内容,而`insert`允许在任意位置插入字符串或字符。最后,`erase`函数用于删除字符串中的部分内容。每个函数都配以代码示例和说明。
|
12天前
|
安全 编译器 C++
【C++】string类的使用②(元素获取Element access)
```markdown 探索C++ `string`方法:`clear()`保持容量不变使字符串变空;`empty()`检查长度是否为0;C++11的`shrink_to_fit()`尝试减少容量。`operator[]`和`at()`安全访问元素,越界时`at()`抛异常。`back()`和`front()`分别访问首尾元素。了解这些,轻松操作字符串!💡 ```
|
12天前
|
存储 编译器 Linux
【C++】string类的使用②(容量接口Capacity )
这篇博客探讨了C++ STL中string的容量接口和元素访问方法。`size()`和`length()`函数等价,返回字符串的长度;`capacity()`提供已分配的字节数,可能大于长度;`max_size()`给出理论最大长度;`reserve()`预分配空间,不改变内容;`resize()`改变字符串长度,可指定填充字符。这些接口用于优化内存管理和适应字符串操作需求。