inheritance

Hi guys I'm just wondering about inheritance and why this is and isn't legal

first I don't understand how you can set a base class to be equalled to a derived class in Java it's the other way around,I mean a derived class is a base class so how is it possible you can set a base class = to a derived class( yes this is legal and object slicing occurs)

second I don't understand why you can not set a derived class = to a base class,a derived class is a base class so logically shouldn't this be allowed?? and how come it's not?

thanks

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
  
#include <iostream>
using namespace std;


class base{


public:
	int data = 10;
	virtual void print(){

		cout << "base" << endl;

	}
};

class derived : public base{


public:
	int data = 5;
	virtual void print(){

		cout << "derived" << endl;

	}
};

int main() {

	derived d;
	base b;

	b = d;

	derived c = b; // not possible compile error
}
1. Copy assignment.

Your b = d; calls base::operator=( const base & )
Derived d is essentially a base object and that is why the call of copy assignment operator is legal.
There will be "slicing", because b becomes a copy of the base-portion of d, ignoring all the derived details.

2. Line 37 differs in two ways. First, this is initialization, not assignment.
Lets even the battle-field:
1
2
derived c;
c = b; // attempt to assign 

And then we get to the other part:
 In function 'int main()':
38:4: error: no match for 'operator=' (operand types are 'derived' and 'base')
38:4: note: candidates are:
18:7: note: derived& derived::operator=(const derived&)
18:7: note: no known conversion for argument 1 from 'base' to 'const derived&'
18:7: note: derived& derived::operator=(derived&&)
18:7: note: no known conversion for argument 1 from 'base' to 'derived&&' 


How should one create a derived from a base? There is no default logic for that.
You can define derived constructor and assignment operator that do take a base object and somehow set a whole derived object. The logic is up to you.


3. Your derived objects do have two integer members. The first has name 'data' and the second has name 'data'. Isn't that rather inconvenient?
> how you can set a base class to be equalled to a derived class in Java it's the other way around
in java you may do
1
2
3
4
class base{}
class derived extends base{}

base b = new derived();
I don't see how is it «the other way around»


java doesn't have objects, only (smart) pointers
in c++ you may do
1
2
base *b;
b = new derived(); //no copy slicing 
thanks guys

but derived is a base

a base isn't a derived so how is it possible

1
2
3
4
derived d;
	base b;

	b = d;


thanks
* b is a base
* d is a derived
* derived is a base
=> d is a base

=> b = d
<=>
base = base


Example:
Base is a Shape. Every Shape has position.
Circle is derived from Shape. Circle has radius.
Circle is Shape and thus it has position, right where every Shape has position.
It is ok to copy the position of Circle into another plain Shape object.
Last edited on
d is a derived. Therefore, d is a base.

b is a base.

Therefore, on line 4, you are setting a base equal to something that is a base. That makes sense, right?

To elaborate, you are setting the value of b to be the same as the value of that part of d that is a base. It is possible to set the value of b to be equal to d, because d contains everything needed to completely set the value of b. (d also contains other stuff, but that's ignored).

Now look at the opposite case:

1
2
3
4
derived d;
base b;

d = b;


Now, you are trying to set the value of a derived, to something that is not a derived. b does not contain everything necessary to set the complete value of d, so this operation is illegal.

Does that make sense?
thanks guys great examples makes sense now =)

always had trouble with that
You're welcome - glad it helped!
Topic archived. No new replies allowed.