Polymorphism

Recently took a C++ online quiz and ran into this code

#include <iostream>

class B
{
public:
virtual int shift(int n = 2) const { return n; }
};

class D
: public B
{
public:
int shift(int n = 3) const { return n; }
};

int main()
{
const D d;
const B *b = &d;

std::cout << b->shift() << std::endl;

return 0;
}

Why the output is 2 not 3?
Last edited on
Because "filling in" the default parameters is done by the caller.
Thanks a lot.
Yea, polymorphism only calls the function of the class that the pointer is well, pointing to.

Object Oriented is badass. It's what really got me interested in programming and I when I am forced to program procedurally I miss it dearly. Both styles have their uses and downfalls but trying to establish loose coupling in procedural programming is ridiculously challenging.
Last edited on
What about the const key word in the virtual function is't it wrong to use . .. also in the main () i do not find any scenario that uses const object of the class for polymorphism in practical application programming .Can some one explain as to where do you use this scenario of const object in polymorphism.
Thanks
xxx
What about the const key word in the virtual function is't it wrong to use

No, it's not wrong.

Can some one explain as to where do you use this scenario of const object in polymorphism.

constness has nothing to do with polymorphism. You make an object const to signal to the reader that it is not going to be modified in the following code (and to a lesser extent, to prevent accidental changes and give less intelligent compilers more room for optimizations).
The const keyword is only a guarantee that the function will not modify any member data of the object on which it is operating. The object on which it is operating does not need to be const, but if it were, there is no problem using that function. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Class C
{
public:
     void consFunc() const {}
     void nonConstFunc {}
};

int main()
{
     C c1;
     const C c2;

     c1.constFunc();      // OK
     c1.nonConstFunc();   // OK

     c2.constFunc();      // OK
     c2.nonConstFunc();   // ERROR!
}
If I want 3 to be displayed I would need to cast the pointer to the derived class, right? that's the only difference I can see!!!
Someone answer my question please?
Thanks Athar and doug4
from here i understand that constant object only can be used with it's constant function .
please correct me if i am wrong .


Thanks
xx

@ TheDestroyer you need to remove the default values first .. the use the polymorphism concept .

xx
Last edited on
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
// Polymorphisum210420121146.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector> 
using namespace std; 

class B
{
private:
	int ba;
public:
				B() { } 
				virtual int shift( ) const { ba = 2 ; return ba; }
				~B() { }
};

class D : public B
{
	int ca ; 
public:
		D() { } 
		int shift( )   const { ca = 3 ;  return ca; }
		~D() { cout<<"\n Dectructor of d"; } 
};



int _tmain(int argc, _TCHAR* argv[])
{
	const B* b = new D();
	cout <<b->shift();
	delete b;
	
	return 0;
}


I get the error .. i am not able to understand why ?



1>j:\work\polymorphisum210420121146\polymorphisum210420121146\polymorphisum210420121146.cpp(15) : error C2166: l-value specifies const object
1>j:\work\polymorphisum210420121146\polymorphisum210420121146\polymorphisum210420121146.cpp(24) : error C2166: l-value specifies const object
Last edited on
@bluecoder, The function is marked as const. That means you can not modify any of the member variables of the object.
TheDestroyer wrote:
If I want 3 to be displayed I would need to cast the pointer to the derived class, right?

Yes
std::cout << ((D*)b)->shift() << std::endl;
Topic archived. No new replies allowed.