【翁恺】12-对象组合

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

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

reusing the implementation

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

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!

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 原则。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值