I know that it's using an address of a derived class object where an address of a base class object is expected.
Eg:
#include <iostream>
#include <string>
using namespace std;
class A {
public:
void display() {
cout << "\nThis is in class A\n";
}
};
class B : public A {
public:
void display() {
cout << "\nThis is in class B\n";
}
};
void function(A* x) {
x->display();
}
int main() {
A* pa = new A;
B* pb = new B;
function(pa);
function(pb); // Upcasting
return 0;
}
Can I also say that Upcasting is using an object of a derived class where an object of the base class is expected? (Notice that I have omitted the term address here)
Eg:#include <iostream>
#include <string>
using namespace std;
class A {
public:
void display() {
cout << "\nThis is in class A\n";
}
};
class B : public A {
public:
void display() {
cout << "\nThis is in class B\n";
}
};
void function2(A x) {
x.display();
}
int main() {
A a;
B b;
function2(a);
function2(b); // Upcasting? Is it?
return 0;
}
Finally, will the first case still be upcasting if the inheritance was private instead of public? I mean, now, the derived class object cannot be treated as a base class object as the base class interface in now lost.