[go: up one dir, main page]

0% found this document useful (0 votes)
9 views4 pages

7) Polymorphism

The document explains polymorphism in programming, highlighting method overloading and method overriding. Method overloading allows multiple functions with the same name but different parameters, while method overriding involves redefining a base class method in a derived class. It includes examples in C++ to illustrate both concepts and their implementations.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

7) Polymorphism

The document explains polymorphism in programming, highlighting method overloading and method overriding. Method overloading allows multiple functions with the same name but different parameters, while method overriding involves redefining a base class method in a derived class. It includes examples in C++ to illustrate both concepts and their implementations.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

--------------- polymorphism ----------------------

poly -> many


morphism -> forms

with same function name different implementation

eg;
draw() -> can draw circle, square, rectangle

types:
compile time -> method overloading
runtime -> method overriding

----------------- method overloading ----------------


defining a more than one function with same name

how its possible?


by the number and/or type of arguments passed is different.

int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }

Overloaded functions may or may not have different return types but they must have
different arguments. For example,
// Error code
int test(int a) { }
double test(int b){ }

/* Online C++ Compiler and Editor */


#include <iostream.h>
#include<conio.h>
class Addition{
public:
void add(int a){
a=a+1;
cout<<"\na="<<a;
}
void add(int a,int b){
int c= a+b;
cout<<"\nc="<<c;
}
void add(float a,float b,float c){
float d= a+b+c;
cout<<"\nd="<<d;
}
};

void main()
{
Addition a1;
clrscr();
a1.add(10);
a1.add(11,22);
a1.add(1.0,1.0,1.0);
getch();

}
a=11
c=33
d=3
----------------------- Methode overriding --------------------
The same function is defined in both the derived class and the based class.
It overrides the base class methode and executes subclass methode

base class methode -> overridden function


sub class methode -> override function

#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:

void print() {
cout << "Derived Function" << endl;
}
};

void main() {
Derived derived1;
clrscr();
derived1.print();

getch();
}

o/p:
Derived Function

===> Access Overridden Function in main() outside class


scope resolution operator ::
#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:

void print() {
cout << "Derived Function" << endl;
}
};

void main() {
Derived derived1, derived2;
clrscr();
derived1.print();
derived2.Base::print();
getch();
}

o/p:
Derived Function
Base Function
===> Access Overridden Function in subclass overriding methode
#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:

void print() {
Base::print();
cout << "Derived Function" << endl;
}
};

void main() {
Derived derived1;
clrscr();
derived1.print();

getch();
}
----------------- task --------------------
methode overloading
1.Write c ++ program to overload a area() of shapes

#include <iostream.h>
#include<conio.h>

// function with float type parameter


float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}

// function with int type parameter


int absolute(int var) {
if (var < 0)
var = -var;
return var;
}

int main() {

// call function with int type parameter


cout << "Absolute value of -5 = " << absolute(-5) << endl;

// call function with float type parameter


cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}

-------------- overridding ---------------

You might also like