-
January 12th, 2020, 05:32 AM
#1
implicit conversion in derived classes
Code:
public:
int a;
string nome;
string cognome;
int eta;
Persona() { cout << "class Persona" << a << endl; }
void stampa() { cout << "method Persona " << a << endl << endl; }
};
class Studente: public Persona {
public:
int esami;
int facolta;
Studente() { cout << "class Studente" << a << endl; }
void stampa() { cout << " method Studente " << a << endl << endl; }
};
int main() {
Persona* pp = NULL;
Studente* ps = NULL;
pp = new Persona;
ps = new Studente;
cout << endl << endl;
pp->a = 10;
ps->a = 99;
pp->stampa();
ps->stampa();
pp = ps;
pp->stampa(); // call method Persona
I don't understand implicit conversion pp = ps
I read in some notes that implicit conversion from pointer of derived class to pointer of base class, members of derived class become inaccessible
while in *pp = * ps , implicit conversion from object of derived class to object of base class , members of derived class disappear ?
It's right ?
Last edited by zio_mangrovia; January 12th, 2020 at 06:37 AM.
-
January 12th, 2020, 06:47 AM
#2
Re: implicit conversion in derived classes
Are you trying to do polymorphism? If so, stampa() in the base class should be defined as virtual. If stampa() is changed, then the output is:
Code:
method Persona 10
method Studente 99
method Studente 99
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 12th, 2020, 07:15 AM
#3
Re: implicit conversion in derived classes
Originally Posted by 2kaud
Are you trying to do polymorphism?
[/code]
No polymorphism.
I'd like to understand what happens in these 2 cases:
- implicit conversion from pointer of derived class to pointer of base class e.g. pp = ps;
- implicit conversion from object of derived class to object of base class eg. *pp = *ps;
in case 1, members (esami, facolta) of derived class Studente become inaccessible ?
in case 2, members (esami, facolta) of derived class Studente disappear ?
-
January 12th, 2020, 07:16 AM
#4
Re: implicit conversion in derived classes
You also have a memory leak - as you are using new without a corresponding delete. And as the original value of pp is overwritten, you then can't do a delete as required for the original allocated value of pp! if you want to overwrite pp, then a copy should be made that can be used to delete the allocated memory:
Code:
const auto po = pp;
pp = ps;
pp->stampa(); // call method Persona
delete po;
delete ps;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 12th, 2020, 08:17 AM
#5
Re: implicit conversion in derived classes
1), 2) pp can only be used to access members of the base class - as that's its type (pointer to base)
2) only base member variable values are copied from the derived class - as again pp is a pointer to base.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 12th, 2020, 10:00 AM
#6
Re: implicit conversion in derived classes
Originally Posted by 2kaud
1), 2) pp can only be used to access members of the base class - as that's its type (pointer to base)
Ok. thanks for your help but I'm trying to apply your explanations:
Code:
#include <iostream>
using namespace std;
class R {
protected:
int z;
int y;
public:
R() { z = 5; y=2;
cout << "new R" << endl;
};
void print(){
cout << z << endl;
cout << y << endl;
z++;
y++;
}
};
class S : public R {
protected:
int y;
public:
S() { z++; y=3;
cout << "new S" << endl;
};
void print (){
cout << y << endl;
y++;
z++;
};
void virtual print1()=0; };
class U: public S {
public:
U() { z=11; y=7;
cout << "new U" << endl;
};
void print (){
cout << z << endl;
cout << y << endl;
y++;
z++;
};
void print1(){
cout << z << endl;
cout << y << endl;
};
};
int main() {
U* obj = new U;
S* obj1 = obj;
R* obj2 = obj1;
obj->print();
obj1->print();
obj2->print();
obj1->print1();
}
result is:
new R
new S
new U
11
7
8
13
2
14
9
Problems start with 8, that is obj1->print()
It's called print() of S class because obj1 is pointer type of S class.
I see both obj1 and obj point to the same object so initially I would say 8 value is referenced to to y of class U but according to your indication when obj1 (S base class) = obj (U derived class),
obj1 can access only to members of the base class that is S, so I thought to y=3 of Class R.
What do you think?
-
January 12th, 2020, 01:17 PM
#7
Re: implicit conversion in derived classes
Originally Posted by zio_mangrovia
I'd like to understand what happens in these 2 cases:
- implicit conversion from pointer of derived class to pointer of base class e.g. pp = ps;
- implicit conversion from object of derived class to object of base class eg. *pp = *ps;
The first case is fine. It's called an upcast. The base class pointer will point to the derived object and access it as if it were a base object. The other way around is called a downcast and it has to be explicit because it's not typesafe.
The second case is not fine. It is called the slicing problem. You should never copy a derived object to a base object.
Last edited by wolle; January 12th, 2020 at 01:30 PM.
-
January 13th, 2020, 11:56 PM
#8
Re: implicit conversion in derived classes
Originally Posted by wolle
The second case is not fine. It is called the slicing problem. You should never copy a derived object to a base object.
Thanks.
These ones are examples for learning derived class, not really used in C++ programming.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|