Define virtual function?, C/C++ Programming

Define virtual function?

Posted Date: 3/15/2013 6:20:12 AM | Location : United States





A: While derived class overrides the base class method through redefining the same function, then if client wished to access redefined the method from derived class via a pointer from base class object, then you have to described this function in base class as virtual function.

class parent                       

{

void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child; parent_object_ptr->show() // calls parent->show() now we goto virtual world...

class parent

{

virtual void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child;

parent_object_ptr->show() // calls child->show()

 

Posted by | Posted Date: 3/15/2013 6:20:39 AM

A: While derived class overrides the base class method through redefining the same function, then if client wished to access redefined the method from derived class via a pointer from base class object, then you have to described this function in base class as virtual function.

class parent                       

{

void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child; parent_object_ptr->show() // calls parent->show() now we goto virtual world...

class parent

{

virtual void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child;

parent_object_ptr->show() // calls child->show()

 

Posted by | Posted Date: 3/15/2013 6:29:13 AM

A: While derived class overrides the base class method through redefining the same function, then if client wished to access redefined the method from derived class via a pointer from base class object, then you have to described this function in base class as virtual function.

class parent                       

{

void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child; parent_object_ptr->show() // calls parent->show() now we goto virtual world...

class parent

{

virtual void Show()

{

cout << "i''m parent" << endl;

}

};

class child: public parent

{

void Show()

{

cout << "i''m child" << endl;

}

};

parent * parent_object_ptr = new child;

parent_object_ptr->show() // calls child->show()

 

Posted by | Posted Date: 3/15/2013 6:29:27 AM


Related Discussions:- Define virtual function?, Assignment Help, Ask Question on Define virtual function?, Get Answer, Expert's Help, Define virtual function? Discussions

Write discussion on Define virtual function?
Your posts are moderated
Related Questions
Flash my terminal vx670 Project Description: Have VeriFone software need it to communicate with the vx670 through com port 1 it is not talking I'm not sure why have all the r

c program for padovan string   Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindromes

array itself is a pinter.explain

Requirements: 1.  This assignment as well as other assignments in this class must be finished on Windows operating system. 2.  Zip your program and submit the zip file on ANG

program for singly linked list with out header

Problem Description Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new techn

Is it possible to create your own header files? - Yes, it's possible to create a customized header file. - To do this, you just need to include function prototypes that you

Q: How can Define an Array? An Array is defined in much the alike manner as ordinary variables except that every array name must be accompanied by a size specification (that is