ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-02-06 14:01:15  阅读:142  来源: 互联网

标签:12 const name cents saver SavingAccount C++ print 翁恺


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

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

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

标签:12,const,name,cents,saver,SavingAccount,C++,print,翁恺
来源: https://blog.csdn.net/hymn1993/article/details/122788363

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有