Search This Blog

Saturday, 15 February 2014

multiple objects of a class in c++

 Default arguments in a function

/*
    illustrate the concept of the multiple objects of a single class.
    add(int a,int b=2,int c=1)
   means a has no default value but b and c have their default values. 
*/


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

class Base
{
   public:
       void add(int,int,int);
};

void Base::add(int a,int b=2,int c=1)
{
  cout<<"\nSum is:"<<(a+b+c);
}
void main()
{
    Base b,b1,b2;
    b.add(2);    // means a=2 ,b=2,c=1
    b1.add(1,0,0);   // means  a=1,b=0,c=0
    b2.add(2,1);      //means a=2,b=1,c=1
    getch();
}

Output

Sum is:5
Sum is:1
Sum is:4


No comments:

Popular Posts