Search
 
SCRIPT & CODE EXAMPLE
 

CPP

polymorfism c++

//That's a good and easy example of polymorphism

#include <iostream>
using namespace std;
class Addition {
public:
    int ADD(int X,int Y)   // Function with parameter 
    {
        return X+Y;     // this function is performing addition of  two Integer value
    }
    int ADD() {              // Function with same name but without parameter
        string a= "HELLO";
        string b="SAM";   // in this function concatenation is performed
       string c= a+b;
       cout<<c<<endl;
        
    }
};
int main(void) {
    Addition obj;   // Object is created  
    cout<<obj.ADD(128, 15)<<endl; //first method is called
    obj.ADD();  // second method is called
    return 0;
}
Comment

c++ polymorphism

#include <iostream>
 
class Animal {
public:
  void action() {
    std::cout << "The animal does something.
";
  }
};
 
class Fish: public Animal {
public:
  void action() {
    std::cout << "Fish swim.
";
  }
};
 
class Bird: public Animal {
public:
  void action() {
    std::cout << "Birds fly.
";
  }
};
 
int main() {
  Animal newAnimal;
  Fish newFish;
  Bird newBird;
 
  newAnimal.action();
  newFish.action();
  newBird.action();
 
  return 0;
}
Comment

C++ polymorphism

// Base class
class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound 
" ;
  }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow 
" ;
  }
}; 

int main() {
  Animal myAnimal;
  Dog myDog;

  myAnimal.animalSound();
  myDog.animalSound();
  return 0;
} 
Comment

polymorphism in c++

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.
Comment

C++ Polymorphism

// Base class
class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound 
" ;
  }
};

// Derived class
class Pig : public Animal {
  public:
    void animalSound() {
    cout << "The pig says: wee wee 
" ;
  }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow 
" ;
  }
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: char to int in c++ 
Cpp :: string number to integer number C++ 
Cpp :: how to make a function in c++ 
Cpp :: what do you mean by smallest anagram of a string 
Cpp :: kmp algorithm c++ 
Cpp :: lower bound and upper bound in c++ 
Cpp :: c++ inheritance constructor 
Cpp :: take a function argument 
Cpp :: toString method in c++ using sstream 
Cpp :: c++ Attribute Parser 
Cpp :: one away coding question 
Cpp :: clear previous terminal output c++ 
Cpp :: 1768. Merge Strings Alternately leetcode solution in c++ 
Cpp :: c++ get active thread count 
Cpp :: matrix c++ 
Cpp :: what was the piep piper app 
Cpp :: . Write a C++ program to calculate area of a Triangle 
Cpp :: Basic Input / Output in C++ 
Cpp :: closing a ifstream file c++ 
Cpp :: int cpp 
Cpp :: how can I delete a substring from a string in c++? 
Cpp :: how to convert char to int in c++ 
Cpp :: operator overloading in c++ 
Cpp :: how to take input in 2d vector in c++ 
Cpp :: binary to decimal 
Cpp :: && c++ 
Cpp :: Check whether the jth object is in the subset 
Cpp :: what is a .h file in c++ 
Cpp :: how to do if command in c++ 
Cpp :: how to make c++ read strlen 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =