answersLogoWhite

0


Best Answer

Polymorphism in VC++ is the same as polymorphism in C++ itself. When you implicitly call a virtual method of a base class or one of its derivatives, then you rightly expect the most-derived override to execute instead, even when the runtime type is completely unknown to the caller and cannot be determined at runtime let alone compile time. You get that for free simply by overriding the known virtual methods of the base class, and without any need for expensive runtime type information, which is only useful if the caller is actually aware of the type in the first place, and which can only be predicted on a closed system. The whole point of polymorphism is that the base class (and therefore the caller) need know nothing whatsoever about any of its derivatives in order to execute more specialised methods. VC++ fully supports this aspect of OOP.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

What is polymorphism? In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts. For example, the + (plus) operator in C++: 4 + 5 <-- integer addition

3.14 + 2.0 <-- floating point addition

s1 + "bar" <-- string concatenation!

In C++, that type of polymorphism is called overloading.

Employee example: Here, we will represent 2 types of employees as classes in C++: * a generic employee (class Employee) * a manager (class Manager) For these employees, we'll store data, like their: * name * pay rate And...we'll require some functionality, like being able to: * initialize the employee * get the employee's fields * calculate the employee's pay To help demonstrate polymorphism in C++, we'll focus on the methods that calculate an employee's pay

# Employee class: Here is a class definition for a generic Employee: class Employee {

public:

Employee(string theName, float thePayRate);

string getName() const;

float getPayRate() const;

float pay(float hoursWorked) const;

protected:

string name;

float payRate;

};

Definitions for each of the methods follow: Employee::Employee(string theName, float thePayRate)

{

name = theName;

payRate = thePayRate;

}

string Employee::getName() const

{

return name;

}

float Employee::getPayRate() const

{

return payRate;

}

float Employee::pay(float hoursWorked) const

{

return hoursWorked * payRate;

}

Note that the payRate is used as an hourly wage. # Manager class: We'll also have a Manager class that is defined reusing the Employee class (i.e., via inheritance). Remember, if a manager inherits from an employee, then it will get all the data and functionality of an employee. We can then add any new data and methods needed for a manager and override (i.e., redefine) any methods that differ for a manager. Here is the class definition for a Manager: #include "employee.h"

class Manager : public Employee {

public:

Manager(string theName,

float thePayRate,

bool isSalaried);

bool getSalaried() const;

float pay(float hoursWorked) const;

protected:

bool salaried;

};

Definitions for the additional or overriddenmethods follow: Manager::Manager(string theName,

float thePayRate,

bool isSalaried)

: Employee(theName, thePayRate)

{

salaried = isSalaried;

}

bool Manager::getSalaried() const

{

return salaried;

}

float Manager::pay(float hoursWorked) const

{

if (salaried)

return payRate;

/* else */

return Employee::pay(hoursWorked);

}

The pay() method is given a new definition, in which the payRate has 2 possible uses. If the manager is salaried, payRate is the fixed rate for the pay period; otherwise, it represents an hourly rate, just like it does for a regular employee.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Polymorphism allows you to use derived objects generically. That is, it is not necessary to know the runtime type of an object in order to invoke specific behaviour. You get that for free via the virtual table.

For instance, suppose you design a generic shape class and from that you derive circle and square objects. The generic shape class has a draw method that is overridden by the derived classes. Thus it is not necessary for the calling code to know if the shape it holds is a generic shape or a specific shape. You simply call the generic draw method and the actual object (the runtime type) takes care of the specifics. In other words, you get polymorphic behaviour for free simply by calling virtual methods.

Since your calling code is generic, this makes it possible to cater for new objects (such as triangles), without the need to alter the calling code.

If you find yourself in the position where you have to determine the actual runtime type of an object then you should seriously consider redesigning your classes to make them more generic.

This answer is:
User Avatar

User Avatar

KING-OMKAR

Lvl 3
2y ago

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. To learn more about C Programming visit codeforhunger .com

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

c programming is structured programming

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain the meaning of polymorphism describe how polymorphism is accomplished in C taking a suitable example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

1 Do polymorphism and mutation explain the same biological phenomenon?

no polymorphism and mutation are different.polymorphism represents overall change in an organism whereas mutation may or may not bring overall change.


Explain suitable example 1NF 2NF 3NF?

Sure! 1NF (First Normal Form): Each column in a table should hold atomic values (values that cannot be divided further), and each row should be unique. 2NF (Second Normal Form): Every non-key attribute must be fully functionally dependent on the entire primary key, meaning no partial dependencies are allowed. 3NF (Third Normal Form): In addition to 2NF rules, no transitive dependencies should exist, meaning that non-key attributes should not depend on other non-key attributes.


What is difference between explain and describe?

Describe is what it is and explain is why it is as it is


What is the difference between describe and explain?

Describe is what it is and explain is why it is as it is


Explain listing process with suitable example and Diagram?

1 Explain listing process with suitable example &amp; Diagram


What is the describe and explain child's right in Cambodia?

describe and explain child's right?


Explain what is meant by the fetch-execute cycle and describe its action in RLT?

Explain what is meant by the fetch-execute cycle and describe its action in RLT?" Explain what is meant by the fetch-execute cycle and describe its action in RLT?"


Explain with suitable example how switch statement is differ from if statement?

www.assignmentsclub.com


What do you mean by recursive function explain with suitable example?

Type your answer here...


Explain earth 's position in the solar system and describe its neighbors?

explain earth's position in the solar system and describe its closest neighbors.


What is an noun for the word describe?

A synonym for describe is 'explain'.


What is the name of pole in the ventilation that connects to all the poles in a building with the word ''stop'' in it?

The question provides insufficient information and cannot be answered. You should describe about which pole are you asking and what ventilation are you referring to. Please explain to receive a suitable answer.