C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A question related to virtual function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
babu
Guest





PostPosted: Fri May 18, 2007 9:10 am    Post subject: A question related to virtual function Reply with 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
Back to top
Invincible
Guest





PostPosted: Fri May 18, 2007 9:10 am    Post subject: Re: A question related to virtual function Reply with quote



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





PostPosted: Fri May 18, 2007 9:10 am    Post subject: Re: A question related to virtual function Reply with quote



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






PostPosted: Fri May 18, 2007 9:11 am    Post subject: Re: A question related to virtual function Reply with quote

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





PostPosted: Fri May 18, 2007 9:11 am    Post subject: Re: A question related to virtual function Reply with quote

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





PostPosted: Fri May 18, 2007 9:11 am    Post subject: Re: A question related to virtual function Reply with 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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.