C++翁恺学习12-对象组合

继承是软件重用的一种方式。

组合也是软件重用的一种方式。

reusing the implementation

  • composition(组合):construct new object with existing objects
  • it is the relationship of "has-a"

C++翁恺学习12-对象组合

composition

  • objects can be used to build up other objects
  • ways of inclusion
    • fully(成员变量就是对象本身)
    • by reference(成员变量是一个指针)
  • inclusion by reference allows sharing

  • for example,an employee has a
    • name
    • address
    • health plan
    • salary history
      • collection of raise objects
    • supervisor(by reference)
      • another employee object!

C++翁恺学习12-对象组合

 

example

class Person{...};
class Currency{...};
class SavingAccount{
public:
    SavingAccount(const char* name,const char* address,int cents);
    ~ SavingAccount();
    void print();
private:
    Person m_svaer; // 两个其他类的对象,不是指针,是fully类型的组合
    Currency m_balance;
};

SavingAccount::SavingAccount(const char* name,const char* address,int cents):m_saver(name,address),m_balance(0,cents){}
void SavingAccount::print(){
    m_saver.print();
    m_balance.print();
}

SavingAccount对象里面有两个其他类的对象。意味着,创建SavingAccount类的对象需要初始化SavingAccount,其他两个对象有自己的构造函数。

那这两个对象怎么初始化?

        自己初始化自己。 列表初始化的形式:m_saver(name,address),m_balance(0,cents)。构造函数之前进行执行,如果不这么做,这两个类需要有默认构造函数,并且需要做 默认构造+赋值。

    m_saver.print();
    m_balance.print();       
// 不破坏对象边界,独立

SavingAccount::SavingAccount(const char* name, // 构造函数
    const char* address,int cents):m_saver(name,address),m_balance(0,cents){}
void SavingAccount::print(){
    m_saver.print();
    m_balance.print();
}

embedded objects

  • all embedded objects(嵌入对象) are initialized

    • the default constructor is called if
      • you don't supply the arguments,and there is a default constructor(or one can be built)
  • constructors can have initialization list(列表初始化)

    • any number of objects separated by commas(逗号)
    • is optional
    • provide arguments to sub-constructors
  • syntax:

    name(args)  [':' init-lsit]  '{'

 如果类里面有成员变量是对象,那么用列表初始化,不能放到构造函数中。

question

  • if we wrote the constructor as (assuming we have the set accessors for the subobjects):

    SavingsAccount::SavingAcount (const char *,const char * assress, int cents){
        m_saver.set_name(name);
        m_saver.set_address(address);
        m_balance.set_cents(cents);
    }
  • default constructors wold be called。需要有默认构造函数。

public vs. private

  • it is common to make embedded objects private:

    • they are part of the underlying implementation
    • the new class only has part of the public interface of the old class
  • can embed as a public object if you want to have the entire public interface of the subobject available in the new object:

    class SavingAcdount{
        public:
        Person m_saver;...
    }//assume Person class has set_name
    SavingAccount account;
    account.m_saver.set_name("Fred");

    这违反了 OOP 原则。

上一篇:Java多线程12-线程强制执行join


下一篇:NOIP2021 游记