C++-Virtual function as private

Hi All,

Could you help me with the output for below programs?

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
  class base{

    private: 
   virtual void display()        //Please note I have made this class private

{
     cout<<"Base"<<endl;
}

};

  class derived{

    public: 
    void display()
{
     cout<<"derived"<<endl;
}

};

int main()

{
   base*bptr=new derived();
    bptr->display()
return 0;
}



output??


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
  class base{

   public: 
   virtual void display()       

{
     cout<<"Base"<<endl;
}

};

  class derived{

     private: 
    void display()    //Please note I have made this function private
{
     cout<<"derived"<<endl; 
}

};

int main()

{
   base*bptr=new derived();
    bptr->display()
return 0;
}


output??



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
  class base{

    private: 
    void display()        //Please note this is non virtual function and private

{
     cout<<"Base"<<endl;
}

};

  class derived{

    public: 
    void display()
{
     cout<<"derived"<<endl;
}

};

int main()

{
   base*bptr=new derived();
    bptr->display()
return 0;
}


output??



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
  class base{

    public: 
     void display()        //Please note this is non virtual function

{
     cout<<"Base"<<endl;
}

};

  class derived{

    private: 
    void display()                 
{
     cout<<"derived"<<endl;      //Please note this is private
}

};

int main()

{
   base*bptr=new derived();
    bptr->display()
return 0;
}


output??





Last edited on
Why don't you just run the programs and see for yourself?
Hi,

Do you have your own compiler? You could fix these up, then find out for yourself.

If not, you can use the gear wheel on the top right of the code you posted, to compile the code yourself. On the compilation tab, set all 3 of the warning levels.

There are errors involving iostream std::cout std::endl and a missing semicolon on line 26. After fixing these see if it compiles.

Good Luck !!
Thanks it worked in all cases when function in base class is made public.
Hi,

Well none of those compiled for me - what do you understand private to mean?
There is a semicolon missing in the line 26.

My point was just to understand if the virtual function call would be resolved successfully even if we declare function in base class as private,but it doesnt work ;)

I meant it didn't compile for me after I fixed all the other problems, as you say, it doesn't work.

My reply was in response to this:

Thanks it worked in all cases when function in base class is made public.


The version with a public base and private derived doesn't work either, contradicting your post which I just quoted.

I think you understand now that private really does mean for the use of that class only.

You can however have a protected interface.

Hope this helps :+)
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
class base{

   public: 
   virtual void display()       

{
     cout<<"Base"<<endl;
}

};

  class derived{

     private: 
    void display()    //Please note I have made this function private
{
     cout<<"derived"<<endl; 
}

};

int main()

{
   base*bptr=new derived();
    bptr->display();
return 0;
}


This worked for me and it displayed "derived" which means private function in derived class got executed.
Last edited on
You forgot to inherit from the base class.

 
class derived : public base


Note that this only works because the pointer is a base pointer.

1
2
base*bptr=new derived();
bptr->display();

If it was a pointer to derived this would not work because derived::display is private.

I can't think of a good reason to have a function public in the base class and private in the derived class. It's just confusing, because it doesn't prevent you from calling the function. All you have to do is cast the derived pointer to a base pointer before calling the function. The opposite (private in base, public in derived) makes more sense.
Last edited on
something like this ?

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
class Base
{
protected:
	Base(){}
public:
	virtual ~Base(){}

	virtual void display() {}
};

class Derived
	: public virtual Base
{

	virtual void display() override final {}

public:
	Derived() : Base() {}

	virtual ~Derived(){}
	
};

int main()
{

	Base* ptr = new Derived; //a manager own this instance

	ptr->display(); //good , the vtable will do its job

	Derived* pptr = new Derived; //another class only gets that

	pptr->display(); //incorrect if the other class tries to call it

	delete ptr;

	delete pptr;

   return 0;
}


@Peter In my opinion I prefer putting method private in the derived class , especially if another class tries to call update , etc. But I agree to put the virtual ~dtor always public.
Last edited on
@Ericool

Just wondering why you have virtual inheritance, isn't that a different thing altogether? As in it's use is mainly for multiple inheritance?
@TheIdeasMan , the keyword virtual is optional , sometimes necessary for specific situations.


this works with or without virtual specification:

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

class Base1
{
protected:
	virtual ~Base1(){ fputs("Killing Base1\n", stdout); }
	Base1(){}
	virtual void update(){ fputs("Updating Base1\n", stdout); }
};

class Base2
{
protected:
	virtual ~Base2(){ fputs("Killing Base2\n", stdout); }
	Base2(){}
	virtual void update(){ fputs("Updating Base2\n", stdout); }
};

class Derived
	: private Base1
	, private Base2
{
public:
	Derived() : Base1(), Base2() {}
	virtual ~Derived(){ fputs("Killing Derived\n", stdout); }
	virtual void update() override final
	{
		fputs("Updating Derived\n", stdout);
		Base1::update();
		Base2::update();
	}
};

int main()
{

//	Base1* ptr1 = new Derived; //error
//	Base2* ptr2 = new Derived; //error
	Derived* ptr3 = new Derived; //good

	ptr3->update();

	delete ptr3;
  return 0;
}


if you remove a class , will still work fine. The concept of vtable is EACH class have their own table. In this example if you replace

1
2
Base1::update();
Base2::update();


by

update(); in the update method , you will have a recursion that will certainly in this case cause a stack overflow.

It might have not answered your interrogation .. , let me known , if it was not clear.
Last edited on
@Ericool

I am not sure whether you understood my question.

It's not clear why you had virtual inheritance on line 12 in your first example. Why would you do this when there is single inheritance happening, not multiple inheritance.

Virtual inheritance is handy for solving the diamond problem, so I still don't see why you had it for single inheritance.
Diamond problem happens until you start UML and Design Correctly.

You can remove virtual , in this case it is optional.
Topic archived. No new replies allowed.