0% found this document useful (0 votes)
13 views8 pages

Polymorphism Program

The document contains code examples that demonstrate function overloading, operator overloading, function overriding, and virtual functions in C++. Function overloading allows multiple functions with the same name but different parameters. Operator overloading allows operators like + to be used with user-defined classes. Function overriding involves redefining a function in a derived class. Virtual functions allow functions to be overridden in derived classes and called dynamically at runtime based on the object type.

Uploaded by

amolia2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
13 views8 pages

Polymorphism Program

The document contains code examples that demonstrate function overloading, operator overloading, function overriding, and virtual functions in C++. Function overloading allows multiple functions with the same name but different parameters. Operator overloading allows operators like + to be used with user-defined classes. Function overriding involves redefining a function in a derived class. Virtual functions allow functions to be overridden in derived classes and called dynamically at runtime based on the object type.

Uploaded by

amolia2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

// C++ program for function overloading

#include <iostream>

using namespace std;

class Geeks

public:

// function with 1 int parameter

void func(int x)

cout << "value of x is " << x << endl;

// function with same name but 1 double parameter

void func(double x)

cout << "value of x is " << x << endl;

// function with same name and 2 int parameters

void func(int x, int y)

cout << "value of x and y is " << x << ", " << y << endl;

};
int main() {

Geeks obj1;

// Which function is called will depend on the parameters passed

// The first 'func' is called

obj1.func(7);

// The second 'func' is called

obj1.func(9.132);

// The third 'func' is called

obj1.func(85,64);

return 0;

value of x is 7

value of x is 9.132

value of x and y is 85, 64


// CPP program to illustrate // Operator Overloading

#include <iostream>

using namespace std;

class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i =0) {real = r; imag = i;}

// This is automatically called when '+' is used with

// between two Complex objects

Complex operator + (Complex const &obj) {

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

void print() { cout << real << " + i" << imag << endl; }

};

int main()

Complex c1(10, 5), c2(2, 4);


Complex c3 = c1 + c2; // An example call to "operator+"

c3.print();

}
// C++ program for function overriding

#include <iostream>

using namespace std;

class base

public:

virtual void print ()

{ cout<< "print base class" <<endl; }

void show ()

{ cout<< "show base class" <<endl; }

};

class derived:public base

public:

void print () //print () is already virtual function in derived class,

//we could also declared as virtual void print () explicitly

{ cout<< "print derived class" <<endl; }

void show ()

{ cout<< "show derived class" <<endl; }

};

//main function

int main()
{

base *bptr;

derived d;

bptr = &d;

//virtual function, binded at runtime (Runtime polymorphism)

bptr->print();

// Non-virtual function, binded at compile time

bptr->show();

return 0;

}
// CPP program to illustrate // concept of Virtual Functions

#include <iostream>

using namespace std;

class base {

public:

virtual void print()

cout << "print base class" << endl;

void show()

cout << "show base class" << endl;

};

class derived : public base {

public:

void print()

cout << "print derived class" << endl;

void show()

{
cout << "show derived class" << endl;

};

int main()

base* bptr;

derived d;

bptr = &d;

// virtual function, binded at runtime

bptr->print();

// Non-virtual function, binded at compile time

bptr->show();

You might also like