inheritance

If i have a base class and a derived class which inherits base privately then can a friend function access the base class private members:

1)Why not, when base is also inherited by derived.

2)why when the assignment operator overload in base is moved to the protected, then the call works. Why?

eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class base
  8 {
  9 private:
 10         base(const base&);
 11         base& operator=(const base&){cout<<"in base"<<endl;}
 12 protected:
 13         base(){}
 14         ~base(){}
 15 };
 16 
 17 class derived:private base
 18 {
 19 private:
 20         int val;
 21         string myName;
 22         //derived(const derived&){}
 23         //derived& operator=(const derived&){cout<<"in derived"<<endl;}
 24 public:
 25         derived(){}
 26         derived(string &name, int &t):myName(name),val(t){}
 27         ~derived(){}
 28         void disp(){cout<<myName<<" "<<val<<endl;}
 29         friend void f(derived&, derived&);
 30 
 31 };
 32 
 33 void f(derived& nobj, derived& ref)
 34 {
        nobj = ref;// this involes the assignment operator overload

     }

f is only a friend of derived, not of base, so you still can't call base class members. If you had defined a new operator = in derived you should be able to call operator =.

If you put it into protected, then derived can access it and thus you don't need a new operator =.
just a question: Why will the base class operator be called if the object is of derived type and the operation is on two derived type object?

*Assuming operator= is in the protected of the base class.
Last edited on
Because you haven't defined an operator = in the derived class. Because of that, the default automatically calls the base class(es)'s operator = as well as using it to set all of the elements.
i just want to be clear, the private data of the base class is inherited by the derived class but however there is no way derive member can access it. so lets say i have :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Customer {

public:

  ...

  Customer(const Customer& rhs);

  Customer& operator=(const Customer& rhs);

  ...



private:

  std::string name;

};

Customer::Customer(const Customer& rhs)

: name(rhs.name)                                 // copy rhs's data

{

  logCall("Customer copy constructor");

}



Customer& Customer::operator=(const Customer& rhs)

{

  logCall("Customer copy assignment operator");



  name = rhs.name;                               // copy rhs's data



  return *this;                                  

}




class PriorityCustomer: public Customer {                  // a derived class

public:

   ...

   PriorityCustomer(const PriorityCustomer& rhs);

   PriorityCustomer& operator=(const PriorityCustomer& rhs);

   ...



private:

   int priority;

};

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)

: priority(rhs.priority)

{

  logCall("PriorityCustomer copy constructor");

}



PriorityCustomer&

PriorityCustomer::operator=(const PriorityCustomer& rhs)

{

  logCall("PriorityCustomer copy assignment operator");



  priority = rhs.priority;



  return *this;

}



However my derived class has not copied the inherited base class's name data. So it seems like a incomplete copy.

So is it necessary for the name to be also copied or need not since derive cant access this private data name of base.

can anyone let me know if a base class is inherited, so would the private data members. Hence in that case do we have to also do an assignment of them if we do a copy assignment of the derived class?

eg:

the code above continued:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PriorityCustomer&

PriorityCustomer::operator=(const PriorityCustomer& rhs)

{

  logCall("PriorityCustomer copy assignment operator");
  
  priority = rhs.priority;
  customer::operator=(rhs);//added, do we need to do this, why or why not


  return *this;

}
Not to hijack/distract but I always see rhs in copy constructors what's that stand for/mean?

-noobuleus
rhs means right hand side...since it depicts the right hand side of the operator...and usually the left hand side is the calling function. Hope tat answers
Yes, you would because you can't access the private members directly...that is generally why base class members are made protected rather than private.
Topic archived. No new replies allowed.