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 array declaration c++ 
Cpp :: what is function c++ 
Cpp :: how to format big numbers with commas in c++ 
Cpp :: accumulate in cpp 
Cpp :: ue4 int to enum c++ 
Cpp :: . Write a C++ program to calculate area of a Triangle 
Cpp :: remove something from stringstream 
Cpp :: how to increase array memory in c++ 
Cpp :: lists occurrences of characters in the string c++ 
Cpp :: Abstract factory C++ code 
Cpp :: has substr c++ 
Cpp :: initialize a vector with same values 
Cpp :: c++ print array of arrays with pointer 
Cpp :: time complexity of best sort algorithm 
Cpp :: compare function in c++ 
Cpp :: concatenate string in cpp 
Cpp :: and c++ 
Cpp :: insertion overloading in c++ 
Cpp :: flutter text direction auto 
Cpp :: sort 2d vector c++ 
Cpp :: c++ virtual function 
Cpp :: cuda shared array 
Cpp :: largest subarray with zero sum 
Cpp :: how to do if command in c++ 
Cpp :: how-to-read-until-eof-from-cin-in-c++ 
Cpp :: variable modulus 5 meaning in c++ 
Cpp :: ++m in c 
Cpp :: string in int in cpp 
Cpp :: softwareegg.courses4u 
Cpp :: nothrow new in cpp 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =