0

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.

Guptill
  • 61
  • 1
  • 1
  • 3
  • 1
    See http://stackoverflow.com/questions/274626/what-is-object-slicing specifically the answer http://stackoverflow.com/questions/274626/what-is-object-slicing#274636 – druckermanly Apr 03 '15 at 04:23

3 Answers3

0

when second function2 is called, the instance of type A is created(this is x as argument of function2) and assigned(or, copied) from b which is type B.

so both prints This is class A.

Upcasting is only for when parent type pointer references instance of derived class and use pointer as parent type pointer.

Chickenchaser
  • 184
  • 2
  • 13
0

There are a few problems

class A {
public:
    // This needs to be a virtual function
    void display() {
        cout << "\nThis is in class A\n";
    }
};

The following causes object slicing see here and here

void function2(A x) {
    x.display();
}

Instead use the following signature and then this allows the upcast just as with a pointer.

void function2(A& x) {
    x.display();
}

will the first case still be upcasting if the inheritance was private instead of public?

No, private inheritance is not an IS-A relationship rather it's an implemented in terms of relationship. Furthermore, if private inheritance was used it wouldn't compile.

James Adkison
  • 9,412
  • 2
  • 29
  • 43
0

This seems to be the still-unanswered part of your question:

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.

If you try it you'll see member functions in the derived class B are able to invoke your non-member void function(A* x); on this (in much the same way that member functions can provide other code with pointer or references to private data members).

#include <iostream>

struct X { void f() { std::cout << "hello\n"; } };

void f(X* p) { p->f(); }

struct Y : private X { void g() { ::f(this); }};

int main()
{
    Y y;
    y.g();
}

Output:

hello

Code available/runnable here

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252