How to store data that can be used by another class?

Problem

Based on the One Responsibility Principle, we usually want the data as a separate component from the implementation class. This implys an “is implemented in terms of” relationship.

Implement the “is implemented in terms of” relationship

  1. Through Composition (most preferred way)

    // In Data.cpp
    class Data{
    public:
    	explicit Data(): width(0), height(0){}
    public:
        int width;
        int height;
    	
    	// these two should be define since we want to control 
    	// the copy process by ourselves
    	// copy constructor
    	Data(const Data& data);
    	// = operator
    	Data& operator=(const Data& data);
    	
    	void loadData(...){...}
    };
    
    // In DataConsumer.cpp
    #include "Data.cpp"
    
    class DataConsumer{
    	explicit DataConsumer(): data(Data()){}
    	...
    	Data data; // composition happened here
    	... 
    	void printDataWidth(){cout << data.width << endl;}
    };
    

    Note: Load data can be non-member function or in the implementation class as well. It is not necessary to be handled by data class.

  2. Private Inheritence

    // In Data.cpp
    class Data{
    public:
    	explicit Data(): width(10), height(0){}
    public:
        int width;
        int height;
    	
    	// these two should be define since we want to control 
    	// the copy process by ourselves
    	// copy constructor
    	Data(const Data& data);
    	// = operator
    	Data& operator=(const Data& data);
    	
    	void loadData(...){...}
    };
    
    // In DataConsumer.cpp
    #include "Data.cpp"
    
    class DataConsumer : private Data{
    	explicit DataConsumer(): Data(){}
    	...
    	void printDataWidth(){cout << data.width << endl;}
    };
    

    Private inheritance doesn’t mean is-a. In contrast to public inheritance, compilers will generally not convert a derived class object into a base class object if the inheritance relationship between the classes is private. Private class is used to make an “is implemented in terms of” relationship.

    When is it need?

    When you’re dealing with two classes not related by is-a where one either needs access to the protected members of another or needs to redefine one or more of its virtual functions.

  3. Through nested class composition

    // In Data.cpp
    class Data{
    public:
    	explicit Data(): width(0), height(0){}
    public:
        int width;
        int height;
    	
    	// these two should be define since we want to control 
    	// the copy process by ourselves
    	// copy constructor
    	Data(const Data& data);
    	// = operator
    	Data& operator=(const Data& data);
    	
    	virtual void loadData(...){...}
    };
    
    // In DataConsumer.cpp
    #include "ParentData.cpp"
    #include <iostream>
    
    using namespace std;
    
    class DataConsumer{
    public:
        explicit DataConsumer(): data(Data()){}
        
        void printDataWidth(){cout << data.width << endl;}
        void printDatauMin(){cout << data.uMin << endl;}
    private:
    	// declare a nested data class that 
    	// publicly inheriented from parentData
        class Data : public ParentData{
        public:
            explicit Data(): ParentData(), uMin(11), uMax(20){}
        public:
            int uMin;
            int uMax;
            
            // these two should be define since we want to control 
            // the copy process by ourselves
            // copy constructor
            Data(const Data& data);
            // = operator
            Data& operator=(const Data& data);  
        public: 
        	virtual void loadData(...){...}
        };
    
        Data data;
    };
    

    Two reason to use this method over the private inheritance:

    • Design DataConsumer to allow for derived classes, but prevent derived classes from redefining loadData.
    • Minimize DataConsumer’s compilation dependencies.
      If DataConsumer inherits from ParentData, ParentData’s definition must be available when DataConsumer is compiled, so the file defining DataConsumer probably has to #include ParentData.h.
      On the other hand, if Data is moved out of DataConsumer and DataConsumer contains only a pointer to a Data, DataConsumer can get by with a simple declaration for the Data class; it need not #include anything to do with ParentData
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值