 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
babu Guest
|
Posted: Fri May 18, 2007 9:10 am Post subject: A question related to virtual function |
|
|
class CPrintString
{
void printName(Person* p)
{
printf("person");
}
void printName(Student * s)
{
printf("student");
}
};
Here, Person is the base class of Student. Now if we write -
Person * st = new Student();
CPrintString * pr =new CPrintString();
pr->printName(st);
The output is "person". But we need "student" to be printed. Why such
thing occurs?
Now What is the solution so that the correct function is called at
runtime based on the parameter object type, so that "student" can be
printed in the previous calling sequence? Is there any solution
without changing class CPrintString? If exists please give.
Thanks
Babu |
|
| Back to top |
|
 |
Invincible Guest
|
Posted: Fri May 18, 2007 9:10 am Post subject: Re: A question related to virtual function |
|
|
On May 18, 10:38 am, babu <nasif4...@gmail.com> wrote:
| Quote: | On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.com> wrote:
Hi,
If one function is declared Virtual in Base class and derived class
has the implementation for that, then you can call derived version of
function as you had shown by using base class pointer ( Person * st ):
But in this case Base class pointer(Person * ) would be passed to
printName() as calling to printname is through CPrintString* . If you
really want to print "Student" then derive CPrintString from Person
class and make printName() to be virtual there.
~Vinay
But can I find any solution without changing the class CPrintSting?
|
I dont think It would be possible to get solution without changing
CPrintSting. You want behavior of polymorphism so u have to abide by
the rules of it. |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Fri May 18, 2007 9:10 am Post subject: Re: A question related to virtual function |
|
|
On May 18, 1:06 am, babu <nasif4...@gmail.com> wrote:
| Quote: | class CPrintString
{
void printName(Person* p)
{
printf("person");
}
void printName(Student * s)
{
printf("student");
}
};
Here, Person is the base class of Student. Now if we write -
Person * st = new Student();
CPrintString * pr =new CPrintString();
pr->printName(st);
The output is "person". But we need "student" to be printed. Why such
thing occurs?
Now What is the solution so that the correct function is called at
runtime based on the parameter object type, so that "student" can be
printed in the previous calling sequence? Is there any solution
without changing class CPrintString? If exists please give.
Thanks
Babu
|
CPrintString should not be a class unless its purpose is akin to a
printer.
Make it a function instead.
If its purpose was to be a printer, its purpose in life is not to
detect *what* type of Person is being passed to it. Let the Person
object determine that.
The printer doesn't care.
#include <iostream>
#include <string>
class Person
{
std::string m_s;
public:
Person()
: m_s("person") { }
Person(const std::string s)
: m_s(s) { }
std::string getString() const
{
return m_s;
}
};
class Student : public Person
{
public:
Student()
: Person("student") { }
};
struct CPrintString
{
void printName(const Person& r_p)
{
std::cout << r_p.getString();
std::cout << std::endl;
}
};
int main()
{
Student student;
CPrintString printer;
printer.printName(student);
}
This way, if you add another type of Person or Student to your
project, the printer is unaffected. Note: pointers replaced by const
references. |
|
| Back to top |
|
 |
Guest
|
Posted: Fri May 18, 2007 9:11 am Post subject: Re: A question related to virtual function |
|
|
On May 17, 10:38 pm, babu <nasif4...@gmail.com> wrote:
| Quote: | On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.com> wrote:
Hi,
If one function is declared Virtual in Base class and derived class
has the implementation for that, then you can call derived version of
function as you had shown by using base class pointer ( Person * st ):
But in this case Base class pointer(Person * ) would be passed to
printName() as calling to printname is through CPrintString* . If you
really want to print "Student" then derive CPrintString from Person
class and make printName() to be virtual there.
~Vinay
But can I find any solution without changing the class CPrintSting?
I think it does not exist a solution without changing CPrintString.
From the OO object, I sugest you make printName as Person's virtual
function. Because printName closely belongs to Person class! |
|
|
| Back to top |
|
 |
babu Guest
|
Posted: Fri May 18, 2007 9:11 am Post subject: Re: A question related to virtual function |
|
|
On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.com> wrote:
| Quote: | Hi,
If one function is declared Virtual in Base class and derived class
has the implementation for that, then you can call derived version of
function as you had shown by using base class pointer ( Person * st ):
But in this case Base class pointer(Person * ) would be passed to
printName() as calling to printname is through CPrintString* . If you
really want to print "Student" then derive CPrintString from Person
class and make printName() to be virtual there.
~Vinay
|
But can I find any solution without changing the class CPrintSting? |
|
| Back to top |
|
 |
Invincible Guest
|
Posted: Fri May 18, 2007 9:11 am Post subject: Re: A question related to virtual function |
|
|
Hi,
If one function is declared Virtual in Base class and derived class
has the implementation for that, then you can call derived version of
function as you had shown by using base class pointer ( Person * st ):
But in this case Base class pointer(Person * ) would be passed to
printName() as calling to printname is through CPrintString* . If you
really want to print "Student" then derive CPrintString from Person
class and make printName() to be virtual there.
~Vinay |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|