Connecting Tech Pros Worldwide Forums | Help | Site Map

A question related to virtual function

babu
Guest
 
Posts: n/a
#1: May 18 '07

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


Invincible
Guest
 
Posts: n/a
#2: May 18 '07

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

babu
Guest
 
Posts: n/a
#3: May 18 '07

re: A question related to virtual function


On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.comwrote:
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?

xsws5638@gmail.com
Guest
 
Posts: n/a
#4: May 18 '07

re: A question related to virtual function


On May 17, 10:38 pm, babu <nasif4...@gmail.comwrote:
Quote:
On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.comwrote:
>
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 ):
>
Quote:
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.
>
Quote:
~Vinay
>
But can I find any solution without changing the class CPrintSting?
I think it does not exist a solution without changing CPrintString.
Quote:
>From the OO object, I sugest you make printName as Person's virtual
function. Because printName closely belongs to Person class!

Invincible
Guest
 
Posts: n/a
#5: May 18 '07

re: A question related to virtual function


On May 18, 10:38 am, babu <nasif4...@gmail.comwrote:
Quote:
On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.comwrote:
>
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 ):
>
Quote:
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.
>
Quote:
~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.

Salt_Peter
Guest
 
Posts: n/a
#6: May 18 '07

re: A question related to virtual function


On May 18, 1:06 am, babu <nasif4...@gmail.comwrote:
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.


babu
Guest
 
Posts: n/a
#7: May 18 '07

re: A question related to virtual function


On May 18, 12:41 pm, Invincible <vinay.jmis...@gmail.comwrote:
Quote:
On May 18, 10:38 am, babu <nasif4...@gmail.comwrote:
>
Quote:
On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.comwrote:
>
Quote:
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 ):
>
Quote:
Quote:
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.
>
Quote:
Quote:
~Vinay
>
Quote:
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.
I think If I type cast in calling pr->printName(st) then I can find
solution. In that case callin may be pr->printName((Student *)st)

Ian Collins
Guest
 
Posts: n/a
#8: May 18 '07

re: A question related to virtual function


babu wrote:
Quote:
On May 18, 12:41 pm, Invincible <vinay.jmis...@gmail.comwrote:
Quote:
>On May 18, 10:38 am, babu <nasif4...@gmail.comwrote:
>>
Quote:
>>On May 18, 11:24 am, Invincible <vinay.jmis...@gmail.comwrote:
>>>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.
>
I think If I type cast in calling pr->printName(st) then I can find
solution. In that case callin may be pr->printName((Student *)st)
>
It's "cast", not "type cast".

If you know it's a Student, why not declare it as one? If you don't
(hence the base pointer), how do you know what to cast to? If you are
going to cast, at least use a C++ style cast.

--
Ian Collins.
KarthikSankar.R@gmail.com
Guest
 
Posts: n/a
#9: May 18 '07

re: A question related to virtual function


You can alternately implement like below.And yes the PrintName should
be virtual
class Person
{
private:

char *m_str;

public:

Person(char *str)
{
m_str=str;
}

virtual void PrintName()
{
cout<<"\nPerson:"<<m_str;
}
};

class Student : public Person
{
private:
char *m_str;
public:
Student(char *str):Person(str)
{
m_str=str;
}

virtual void PrintName()
{
cout<<"\nStudent:"<<m_str;
}

};

void main()
{
Person *ptr=new Student("Student");
ptr->PrintName();
Person *ptr1=new Person("Person");
ptr1->PrintName();
}
Output:
Student:Student
Parent:Parent

If virtual is not used
Output:
Parent:Parent
Parent:Parent

Salt_Peter
Guest
 
Posts: n/a
#10: May 18 '07

re: A question related to virtual function


On May 18, 7:18 am, KarthikSanka...@gmail.com wrote:
Quote:
You can alternately implement like below.And yes the PrintName should
be virtual
class Person
{
private:
>
char *m_str;
>
public:
>
Person(char *str)
{
m_str=str;
}
>
virtual void PrintName()
{
cout<<"\nPerson:"<<m_str;
}
>
};
>
class Student : public Person
{
private:
char *m_str;
public:
Student(char *str):Person(str)
{
m_str=str;
}
>
virtual void PrintName()
{
cout<<"\nStudent:"<<m_str;
}
>
};
>
void main()
int main()
Quote:
{
Person *ptr=new Student("Student");
ptr->PrintName();
Person *ptr1=new Person("Person");
ptr1->PrintName();}
>
Output:
Student:Student
Parent:Parent
>
If virtual is not used
Output:
Parent:Parent
Parent:Parent
This is C++, not Java.
The above is undefined behaviour since you did not provide a required
virtual d~tor in the Person class. Also, any new or new[] without a
corresponding delete or []delete is a memory leak.
You are storing the same pointer in both the derived class and the
base class. Why?.
PrintName() need not be virtual and it should be a const function.
A heap allocation that involves a type with a member pointer to a
literal (char*) is just begging for trouble. Use a std::string, a
std::vector< char or a primitive char array instead.

Finally: have you ever heard of init lists?

class Person
{
std::string m_s;
public:
Person(std::string s)
:m_s(s) { }
virtual ~Person() { }
void PrintName() const { ... }
};


Closed Thread


Similar C / C++ bytes