




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





// C++ program for function overloading #include <bits/stdc++.h> 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; }
// CPP program to illustrate // Operator Overloading #include
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
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(); }
p = &obj1; // Early binding because fun1() is non-virtual // in base p->fun_1(); // Late binding (RTP) p->fun_2(); // Late binding (RTP) p->fun_3(); // Late binding (RTP) p->fun_4(); // Early binding but this function call is // illegal(produces error) becasue pointer // is of base type and function is of // derived class // p->fun_4(5); }
// An abstract class class Test { // Data members of class public: // Pure Virtual Function virtual void show() = 0; /* Other members */ };
#include