How to call a derived class function from a base class

Hi, everyone!
So from main(), V[i].display() first calls B::display() or C::display (depending on what an user types in main()). And then they call A::display(). And from there, I'm trying to go back to B::display() or C::display() (to whichever one that had called A::display())...... WITHOUT ADDING OR CHANGING ANYTHING IN MAIN().

The line 12, where I put //////////
is the line I don't know what to write in order to call back B::display() or C::display(). I know I can't just write B::display() or C::display() there because the base class doesn't know about the derived class (from what I was taught).

Then how can I possibly do that!?!?

Thanks so much in advance.... :)

p.s. Sorry if the code I wrote is too "noob" ...xD

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

class A {
	private:
		int num1 =123;
	public:
		void setnum1(int n){num1=n;}
		virtual void display(){
		cout<<num1;
		//////////////////////
		}
};

class B:public A {
	private:
		int num2;
	public:
		void setnum2(int n){num2=n;}
		void display(){
		cout<<num2;
		A::display();
		}
};

class C:public A {
	private:
		int num3;
	public:
		void setnum3(int n){num3=n;}
		void display(){
		cout<<num3;
		A::display();
		}
};

int main()
{
	int x, x2, x3;
	B b;
	C c;
	vector<A> V;
	for(int i=0;i<5;i++)
	{
		cin >> x;
		if(x==1)
		{
			cin >> x2;
			b.setnum2(x2);
			V.push_back(b);
		}
		else
		{
			cin >> x3;
			c.setnum3(x3);
			V.push_back(c);
		}
	}
	for(int i=0;i<V.size();i++)
	{
		V[i].display();
	}
	return 0;
}
Last edited on
> And from there, I'm trying to go back to B::display() or C::display()
> (to whichever one that had called A::display()). how can I possibly do that!?!?

You do not have to do anything to achieve that. There is an implicit return statement at the end of A::display(); return is a jump statement which unconditionally transfers control back to the caller of the function - if B::display called A::display, control goes back to B::display; if C::display called A::display, control goes back to C::display etc.

(When control reaches the end of a function with the return type void, return ; is automatically executed.)
ohhhh i seeeeeee!
Um sorry but can i ask you one more thing then..? If i were to get rid of the word "virtual" in class A, A::display() would get called first right? Is there anyway i can call either B::display() or C::display() from there directly?? Do i have to make class B (or C) object to do that?
In your code, the vector contains values of type 'A';
A::display() would be called for each object of type 'A' in the vector.

To get polymorphic behaviour, we need to operate on either values of type 'pointer to A' or variables of type 'reference to A'

Run this program, first as it is, observe the behaviour.
Then comment out the virtual specifier in A::display(), recompile and rerun,
and observe the difference in behaviour.

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
#include <iostream>
#include <vector>

class A
{
    private: int num1 ;

    public:
        A( int n1 = 0 ) : num1(n1) {}

        virtual void display() const { std::cout << "num1 == " << num1 << '\n' ; }
};

class B : public A
{
    private: int num2 ;

    public:
        B( int n1 = 0, int n2 = 0 ) : A(n1), num2(n2) {}

        /*virtual*/ void display() const /*override*/
        {
            A::display() ;
            std::cout << "num2 == " << num2 << '\n' ;
        }
};

class C : public A
{
    private: int num3 ;

    public:
        C( int n1 = 0, int n3 = 0 ) : A(n1), num3(n3) {}

        /*virtual*/ void display() const /*override*/
        {
            A::display() ;
            std::cout << "num3 == " << num3 << '\n' ;
        }
};

int main()
{
    A a(12) ;
    B b( 23, 45 ) ;
    C c( 56, 78 ) ;

    {
        std::cout << "vector of values of type 'A' - calls A::display()\n" ;
        std::vector<A> vec { a, b, c } ;
        for( const A& a : vec ) { a.display() ; std::cout << '\n' ; }
    }

    std::cout << "-------------------------------\n" ;

    {
        std::cout << "vector of values of type 'pointer to A'"
                  << "\n\t- calls overridden display() if A::display() is virtual"
                  << "\n\t- calls A::display otherwise\n\n" ;
        std::vector<A*> vec { std::addressof(a), std::addressof(b), std::addressof(c) } ;
        for( const A* ptr : vec ) { ptr->display() ; std::cout << '\n' ; }
    }
}

http://coliru.stacked-crooked.com/a/79fa3ceb23893978
http://coliru.stacked-crooked.com/a/dacdcb3921577536
Oh wow I finally get it!!
Thank you sooooooo much for the help...
I really learn so much from this website than any other place :D
Topic archived. No new replies allowed.