Sunday, January 29, 2012

Complex program of Function in C++

Q1)output of the program
#include "iostream.h"
#include "conio.h"
class test
{
 private:int a;
                void get()
                {
                                a=100;
                }
 public:void call()
                  {
                              get();
                  }
                  void put()
                  {
                                cout<<a;
                  }
};

main()
{
test t;
t.call();
t.put();
getch();
}

Ans:-100
Discussion:-As get()  function is declared as private function, so the public function call() is calling to get() function. It is not possible to call directly get() function by object.

Q2)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
 private: int a;
 public :void get()                        //Get without  paramiter
                {
                                a=100;
                }
                void get(int n)             //Get with parameter.
                {
                                a=n;
                }
                 void put()
                 {
                              cout<<a;
                 }
};

main()
{
test t;
t.get();                                         //Call get() without argument.
t.put();
getch();
}
Ans:-100
Discussion:-Here the concept of function overloading is used.The get() function without argument will call.


Q3)output of the program.
#include "iostream.h"
#include "conio.h"
class test
{

 private :static void hello()
                {
                                  cout<<"Hello Static";
                }
 public :static void callhello()
                {
                                 hello();
                }

};

main()
{
test::callhello();       //Static function call.
getch();
}
Ans:-Hello Static
Discussion:-Here one static function is call to another static function. One feature of static function is without object we can call static function ,only using classname.


Q4)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{

 private:static int a;
 public :void count()
                                                {
                                                  a++;
                                                }
                                                void show()
                                                {
                                                 cout<<a;
                                                }

};

main()
{
test t;
t.show();

getch();
}
Ans:-Linking error.
Discussion:-The statement     int test::a;  is missing here.


Q5) Output of the program
#include "iostream.h"
#include "conio.h"
class test
{

 private:static int a;
 public :static void count()
                {
                                  a++;
                }
                void show()
                {
                                 cout<<a;
                }

};

int test::a;
main()
{
test t;
test::count();
t.show();
getch();
}
Ans:-1
Discussion:-When we declare a static member ,it automatically initialize by zero. Here count() function increment the value of static data member.

Q6) Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
 private:int a;
                int b;
 public:void abc(test a)
                {
                   int t=a.a+a.b;
                  cout<<t;
                };
                void getdata()
                {
                  a=100;
                  b=100;
                 };
};

main()
{
test t;
t.getdata();
t.abc(t);
getch();
}
Ans:-200
Discussion:-Here getdata()  function initialize the value of t object. And  within abc function the two attribute of t object is adding.

Q7)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
 private:int a;
                                                int b;
 public:test abc(test a,test b)
                                                {
                                                 test t;
                                                 t.a=a.a+b.a;
                                                 t.b=a.b+b.b;
                                                 return t;

                                                };
                                                void getdata()
                                                {
                                                 a=100;
                                                 b=100;
                                                };
                                                void putdata()
                                                {
                                                 cout<<a;
                                                 cout<<b;
                                                }

};

main()
{
test t1,t2,t3;
t1.getdata();
t2.getdata();

t3=t1.abc(t1,t2);
t3.putdata();
getch();
}
Ans:-200 200
Discussion:-Here abc function taking two object as argument and returning one argument.

Q8)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
 private:int a;
                int b;
 public:  void swap(int *a,int *b)
                {
                                 int t=*a;
                                 t=*a;
                                 *a=*b;
                                 *b=t;
                }
                void print()
                {
                                 cout<<a;
                                 cout<<b;
                }

};

main()
{
test t;
int a=100,b=200;
t.swap(&a,&b);
cout<<a;
cout<<b;
getch();
}
Ans:-200 100
Discussion:-Swap function Takes two pointer type argument. And it swaping the actual value of a and b;


Q9)Ooutput of the program.
#include "iostream.h"
#include "conio.h"

void extra()
{
 cout<<"Hello extra";
}

main()
{
 extra()
 {
  cout<<"Extra call";
 }

 getch();
}
Ans:-Compile time error.
Discussion:-Here extra() function is defined within the main() function .It violate the rule of c and c++ program.


10)output of the program
#include "iostream.h"
#include "conio.h"

class A;
class B
{
 private :int b;
 public:void getdata()
                                  {
                                                 b=100;
                                  };
                                  friend void cal(A,B);
};

class A
{
 private :int a;
 public:void getdata()
                                  {
                                                 a=100;
                                  };
                                  friend void cal(A,B);
};

void cal(A p,B q)
{
 cout<<p.a+q.b;

}

main()
{
  A t1;
  B t2;

  t1.getdata();
  t2.getdata();
  cal(t1,t2);

  getch();
}
Ans:-200
Discussion:-Cal()  function  is declared as friend function. So friend function can access the value of private data member.



No comments:

Post a Comment