Saturday, 28 April 2018

POLYMORPHISM IN C++



POLYMORPHISM IN C++

C++ achieves polymorphism through:
i) Function overloading
ii)Operator overloading

By the term polymorphism we understand as ‘one interface, multiple methods’. In both the ways i.e. function and operator overloading, one interface or entity is made to appear having multiple methods.

FUNCTION OVERLOADING:
In C++, two or more functions are allowed to have same name but should have different parameters declaration. When two or more functions share same name then they are said to be overloaded. This process is referred as function overloading.
For example,

#include<iostream>
using namespace std;
void sum1( int i,int j);
void sum1(float x,float y);
int main()
{ int a,b;
a=10;
b=12;
float p=10.2,q=20.6;
sum1(a,b);
sum1(p,q);
}
void sum1(int i, int j)
{ cout<<"int::"<<i+j;
}
void sum1(float x, float y)
{ cout<<"\nfloat:"<<x+y;
}

OUTPUT:
int::22
float:30.8

OPERATOR OVERLOADING:
When an operator is overloaded in C++, it takes on an additional meaning relative to a certain class retaining its old meanings.
In other words, operators can be overloaded by defining what they mean relative to a specific class.

Thursday, 29 March 2018

Friend function in c++



FRIEND FUNCTION:

Access to the private and protected members of a class by non-member function is not allowed in normal. But it is possible to grant this access to non-member function.
A function which is declared as friend to a class have access to all its members i.e. private and protected as well as public.
To declare a function as friend, following syntax is used:
class class_name
             {      data members and functions;
               public:
                    friend ret_type function_name(arg_list);
                    member functions and variables;
              };
Sample Program:
#include<iostream>
using namespace std;
class sum
{
int a, b;
public:
friend int sumfrd(sum);                          // friend function
sum();                                                     //constructor
}
sum::sum()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
int sumfrd(sum x)
{ return x.a+x.b;}

int main()
{   sum s;
     cout<<"Sum is: "<<sumfrd(s);
}


NOTE: friend is a keyword in C++

The circumstances in which friend functions are valuable:
  1. Friend functions make creation of some types of I/O functions easier.
  2. It is useful when we need to relate two classes in some sort.
  3. It is useful in overloading certain types of operators.

Data member and member function



DATA MEMBERS AND MEMBER FUNCTION IN C++

The variables declared in a class are called data members. These are also known as member variables.
Member functions are the functions declared within class. The member functions have the accessibility of every element of the class.



Restrictions applied to class members:
  1. A non-static member variable cannot have an initializer.
  2. No member can be an object of the class that is being declared.
  3. No member can be declared as auto, extern, or register.

POLYMORPHISM IN C++

POLYMORPHISM IN C++ C++ achieves polymorphism through: i) Function overloading ii)Operator overloading By the...