Question

In: Computer Science

c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?

c++
Define polymorphism. What is the benefit of using pointers with polymorphic functions?

Solutions

Expert Solution

Polymorphism can be defined as a code that is processed or run multiple times to achieve the desired output. There are two types of polymorphism run-time(Method overriding) or compile-time(Method overloading).

Benefits of Polymorphism

  • Code can be used multiple times
  • A single variable can be used to store multiple data types
  • Assist smooth compilation

E.g

#include <iostream> 
Using namespace std: 

class student {  
   public: 
   void show() {    
      cout << "Hello Welcome to class"; 
   } 
}; 

class School:public student { 
   public: 
   void show() {   
      cout << "Please Study well"; 
   } 
};  

int main() {   
   student x;        // Base class object 
   school y;        // Derived class object 
   x.show();   // student class method is called 
   y.show();   // school class method is called 
   return 0; 
} 

Output

Hello welcome to class

Please study well


Related Solutions

Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
what is DNA polymorphism and define SNP what does it meangenetically
what is DNA polymorphism and define SNP what does it mean genetically
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
Using C++ Create a polymorphic banking program using the Bank-Account hierarchy created in Exercise 2 of...
Using C++ Create a polymorphic banking program using the Bank-Account hierarchy created in Exercise 2 of Written Assignment 4. For each account in the vector, allow the user to specify an amount of money to withdraw from the Bank-Account using member function debit and an amount of money to deposit into the Bank-Account using member function credit. As you process each Bank-Account, determine its type. If a Bank-Account is a Savings, calculate the amount of interest owed to the Bank-Account...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
What do you understand by polymorphism? Why are they useful? Explain each type of polymorphism using...
What do you understand by polymorphism? Why are they useful? Explain each type of polymorphism using a clear example. [15 points]
This class models people moving in together in real life using pointers in C++. What test(s)...
This class models people moving in together in real life using pointers in C++. What test(s) could be added with the code below? At the end of this task, add them! (Answer this.) class Person { public: Person(const string& name) : name(name) {} void movesInWith(Person& newRoomate) { roomie = &newRoomate; // now I have a new roomie newRoomate.roomie = this; // and now they do too } const string& getName() const { return name; } // Don't need to use...
The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers.
  Topic: The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers. What are the advantages and disadvantages of having the functionality of pointers in a programming language?
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !! * DO not use [ ] 'brackets', focus on using malloc, calloc, etc... Program will ask user for the name of the text file to be read Read the datafile with format: 1 2 1 1 2 3 4 So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT