By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

C++: Copying protected members of base class to derived class

P: 5
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class.
Now the thing is, I want to extend the functionality of `Base` class. I do that by inheriting `Base` class by `Derived`, but then I can't use `MakeBase()` function. It all makes sense to me, but I don't seem to find an appropriate solution.
Since I don't want to give any access to the private / protected members of these classes, I don't see how I can implement copy constructors
So I'm basically asking for suggestions. Maybe this is just bad design and I need to think differently (the `MakeBase()` thing).

This code gives the basic idea:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Base
  6. {
  7.   protected:
  8.     int i;
  9.     int n;
  10.  
  11.   public:
  12.     Base(int _i, int _n)
  13.     {
  14.       i = _i;
  15.       n = _n;
  16.     }
  17.     void Print()
  18.     {
  19.       cout << "i = " << i << endl;
  20.       cout << "n = " << n << endl;
  21.     }
  22. };
  23.  
  24.  
  25. Base MakeBase()
  26. {
  27.   /* This is originally a member function of a class,
  28.      and the values are protected members of this class */
  29.   return Base(1,2);
  30. }
  31.  
  32.  
  33. class Derived : public Base
  34. {
  35.   public:
  36.     /* ... or any other function that adds
  37.        functionality to the base class */
  38.     void Increase()
  39.     {
  40.       i++;
  41.       n++;
  42.     }
  43. };
  44.  
  45.  
  46. int main()
  47. {
  48.  
  49.   cout << "Base class:\n========" << endl;
  50.   Base base_class = MakeBase();
  51.   base_class.Print();
  52.  
  53.   cout << "----" << endl;
  54.  
  55.   cout << "Derived class:\n========" << endl;
  56.   Derived derived_class = MakeBase(); /* This obviously isn't valid, but it illustrates what I want */
  57.   derived_class.Print();
  58.  
  59.   return 0;
  60. }
  61.  
Sep 19 '08 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
Create an operator= in the base class that takes a type of Base &

Create an operator= in the derived class that takes a type of Base &, with-in this operator call the Base class operator= using the passed parameter.
Sep 19 '08 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You are trying to implement the Factory design pattern. In this case, an Abstract Factory.

You might want to Google on this or get a design patterns book.
Sep 19 '08 #3

P: 5
I see. I've changed a couple of things to get a solution, and it's fine, but I will read about design patterns anyway.

Thanks,
Edan
Sep 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.