HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

POLYMORPHISM

POLYMORPHISM

FUNCTION SIGNATURE:
The signature of function, depends on the type, no. of parameters and it’s sequence. If the type of parameter or no. of parameter or sequence of parameter differs then the signature also differ.

Examples:
void fun(int;
void fun(char);

Here, the type of parameter differ.

void fun(int);
void fun(int, float);
Here, both type and no. of parameter differ.

void fun(char);
void fun(char, char);
Here, the no. of parameter differ.

Note:
The return type is not a part of function’s signature. Hence, the following two declarations cannot occur in the same program.

void fun();
int fun();

FUNCTION OVERRIDING:
The derived class contains same name and same signature as that of base class function, then the derived class function is said to override the base class function.

The scope resolution (: : ) operator can be used to access base class functions through an object of the derived class.

// Ex-28: PGM ILLUSTRATES FUNCTION OVERRIDING

#include<iostream.h>
#include<conio.h>
class base
{
private:
int x;
public:
void set(int a)
{ x=a; }
void get()
{ cout<<" x = "<<x<<endl; }
};

class derived : public base
{
private:
int y;
public:
void set(int c)
{ y=c; }
void get()
{ cout<<" y = "<<y<<endl; }
};
void main(void)
{
derived obj;
obj.base::set(20);
obj.set(40);
clrscr();
obj.base::get();
obj.get();
getch();
}

FUNCTION OVERLOADING:
When more than one member function with the same name is declared in a class, the function is said to be overloaded in that class. The scope of the overloaded function is restricted to a particular class. Other classes might use the same name again, overloading it differently or even not overloading it at all. These classes have entirely separate scopes, and the compiler keeps them separated.

Overloaded functions need to differ in signature, not in function name.

// Ex-29: PGM ILLUSTRATES FUNTION OVERLOADING

#include<iostream.h>
#include<conio.h>
class overlod
{
private:
int no;
public:
void show(int n)
{ no=n; }
int show()
{ return no; }
};

void main()
{
overlod ob;
ob.show(14);
int i=ob.show();
clrscr();
cout<<i<<endl;
getch();
}

Here, one function is used to read and the other one to write a value of variable. This is the common use of overloaded functions. An overloaded function need not be defined as inline.

CONSTRUCTOR OVERLOADING:
Apart from performing the special role of initialization, constructors are similar to other functions. This includes function overloading. Overloaded constructors are commonly used in C++.

// Ex-30: PGM ILLUSTRATES CONSTRUCTOR OVERLOADING

#include<conio.h>
#include<iostream.h>
class calculate
{
private:
int number1, number2, tot;
public:
calculate(); // Default Constructor
calculate(int, int); // Constructor with two arguments
void input(int, int);
void add();
void disp();
};

calculate :: calculate() // Default Constructor
{
number1=number2=tot=0;
}
calculate :: calculate(int num1, int num2) // Argument Constructor Definition
{
number1 = num1;
number2 = num2;
tot = 0;
}
void calculate :: input(int num1, int num2)
{
number1 = num1;
number2 = num2;
}

void calculate :: add()
{
tot = number1 + number2;
}
void calculate :: disp()
{
cout<<"The sum of two number is "<<tot<<endl;
}

void main()
{
calculate cal1; // Default Constructor Invoked
calculate cal2(10,4); // Argument Constructor Invoked
calculate *calptr;
calptr = new calculate(14,10);
// Pointer is initialized with address and values assigned to member data
cal2.add(); // Add function invoked to initialize member data
clrscr();
cal2.disp();
calptr->add();
calptr->disp();
delete calptr;
getch();
}

Here, the object cal1 is a default constructor is invoked because cal1 is created without arguments. But, for the object cal2, and the pointer calptr, the two-argument constructors are invoked as they are created with two arguments.

Note:
A class can have many constructors, each differing in the number, type and order of arguments. As mentioned earlier, the number, type and order of arguments are collectively referred to as the signature of the function.

OPERATOR OVERLOADING:
Operators are overloaded to increase the scope of the operator. It is used to make abstract data types (ADTs) more natural and similar to built-in data types.

Operator overloading refers to giving additional meaning to the normal C++ operators when they are applied to ADTs. Only a predefined set of C++ operators can be overloaded. An operator can be overloaded by defining a function for it. The function for the operator is declared by using the keyword operator.

CLASSIFICATION OF OPERATORS:

1. Binary operators.
2. Unary operators.

OVERLOADING BINARY OPERATORS:
Binary operators are those which work on two operands. Examples of binary operators are as follows.
Arithmetic operator : +, -, *, /, %
Arithmetic assignment operator : +=, -=, *=, /=
Comparison operator : <, >, <=, >=, ==, !=

Syn: return_type operator +(parameters)

// Ex-31: OVERLOADING BINARY OPERATORS

#include<conio.h>
#include<iostream.h>
class Load
{
private:
int x,y;
public:
Load()
{ }
Load(int a, int b)
{
x=a;
y=b;
}

void get()
{
cout<<"x = "<<x<<" "<<"y = "<<y<<endl;
}
Load operator +(Load ob);
};

Load Load :: operator + (Load ob)
{
Load l;
l.x = x+ob.x;
l.y = y+ob.y;
return l;
}

void main(void)
{
Load ob1(2,4),ob2(6,8),ob3;
clrscr();
ob1.get();
ob2.get();
ob3=ob1+ob2;
ob3.get();
getch();
}

OVERLOADING UNARY OPERATOR:
Unary operators are those operators that act on a single operand. Examples of unary operators are ‘++’ and ‘- -‘.

// Ex-32: OVERLOADING UNARY OPERATOR

#include<iostream.h>
#include<conio.h>
class distance
{
int fage;
float mage;
public:
distance(int f, float m);
void operator ++(void);
void display();
};

distance :: distance(int f, float m)
{
fage = f;
mage = m;
}
void distance :: operator ++(void)
{
fage++;
++mage;
}
void distance :: display()
{
cout<<"Age of female "<<fage<<endl;
cout<<"Age of male "<<mage<<endl;
}
void main()
{
distance dist(18,21);
++dist;
clrscr();
dist.display();
getch();
}

Prefix and Postfix:
The ++ operator, overloaded in the above program cannot be used as a postfix operator. The compiler invokes the operator function with an int argument for the postfix application of the operator. For example,

int operator ++(int);

Here, the argument int is a dummy argument, which is used only to distinguish between the prefix and the postfix operators.
x++ will be resolved as x.operator ++(0)
++x will be resolved as x.operator ++()