C++翁恺学习12-对象组合
2022/2/6 14:43:40
本文主要是介绍C++翁恺学习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)
- the default constructor is called if
-
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 原则。
这篇关于C++翁恺学习12-对象组合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享
- 2024-07-03微信支付提示订单号重复-icode9专业技术文章分享
- 2024-07-02微服务启动nacos注册上去了,但是一直没有收到请求-icode9专业技术文章分享
- 2024-07-02如何检查文件的编码格式-icode9专业技术文章分享
- 2024-07-02sublime 更改编码格式-icode9专业技术文章分享
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享