Showing posts with label constructor. Show all posts
Showing posts with label constructor. Show all posts

Wednesday, August 28, 2013

C++ program to count member function calls

/* C++ program to count member funtion calls */

#include<iostream>
using namespace std;

class A
 {
  int data;
  static int i,j;
  public:
  void input()
  {
  cout<<"enter num : ";
  cin>>data;
  i++;
  }
  void display()
  {
  cout<<"\nnum is "<<data<<endl;
  j++;
  }
  void count()
  {
  cout<<"\nInput calls = "<<i<<"\nDisplay calls = "<<j<<endl;
  }
 };
int A :: i=0;
int A :: j=0;

int main()
 {
  A A1;
  int c;
  do
  {
  cout<<"\n1.INPUT\n2.OUTPUT\n3.COUNT\n4.EXIT\nENTER CHOICE : ";
  cin>>c;
  switch(c)
  {
  case 1:
  A1.input();
  break;
case 2:
  A1.display();
  break;
case 3:
  A1.count();
  break;
default :
return 0;
  }
  }
  while(c);
  return 0;
 }






C++ program using constructor,destructor

/* C++ program using constructor,destructor */



#include<iostream>
using namespace std;
class t
{
  int hour;
  int min;
 public:
 // void input();
  t(int,int);
  t();
  void display(void)
  {
  cout<<"hour="<<hour<<"\n";
  cout<<"min="<<min<<"\n";
  }
  t(t &t11)
  {
  hour=t11.hour;
  min=t11.min;
  }
  ~t()
  {
   cout<<"\ndestroyed\n";
  }
};
t::t(int h,int m)
  {
   hour=h;
   min=m;
  }
 t::t()
    {
     hour=0;
     min=0;
    }

int main()
{
 t t1;
 cout<<"\nobject1 "<<"\n";
 t1.display();
 t1=t(2,45);
 cout<<"\nobject2"<<"\n";
 t1.display();
 t t2(t1);
 cout<<"\nobject3 "<<"\n";
 t2.display();
 return 0;
}