Tuesday, December 2, 2014

Polymorphism in C++ & C++ Exception Handling Pro



Polymorphism in C++

 
#include  
#include
using namespace std;
 class shape 
{
   protected:
      int width, height;
   public:
      shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public Shape
{
   public:
      rectangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class triangle: public shape
{
   public:
      triangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
int main( )
{
   shape *shape;
   rectangle rec(10,7);
   triangle  tri(10,5);
   shape = &rec;
   shape->area();
   shape = &tri;
   shape->area();
              return 0;
}
 
 Output
 
Rectangle class area
Triangle class area

C++ Exception Handling

#include 
#include
using namespace std;
double division(int a, int b)
{
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}
int main ()
{
   int x = 50;
   int y = 0;
   double z = 0;
    try 
    {
     z = division(x, y);
     cout << z << endl;
   }
    catch (const char* msg) 
   {
     cerr << msg << endl;
   }
   return 0;
}
 
 Output
 
Division by zero condition!
 

 Define New Exceptions

#include 
#include
#include 
using namespace std;
struct myexception : public exception
{
  const char * what () const throw ()
  {
    return "C++ Exception";
  }
};
int main()
{
  try
  {
    throw myexception();
  }
  catch(myexception& e)
  {
    std::cout << "MyException caught" << std::endl;
    std::cout << e.what() << std::endl;
  }
  catch(std::exception& e)
  {
      }
}
 
Output
 
My Exception caught
C++ Exception

Terminating Threads

#include 
#include
#include 
#include 
using namespace std;
#define num_threads     5
void *printhello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}
int main ()
{
   pthread_t threads[num_threads];
   int rc;
   int i;
   for( i=0; i < num_threads; i++ )
   {
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, printhello, (void *)i);
      if (rc)
      {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

Passing Arguments to Threads

#include 
#include
#include 
#include 
using namespace std;
#define num_threads    5
struct thread_data
{
   int  thread_id;
   char *message;
};
void *PrintHello(void *threadarg)
{
   struct thread_data *my_data;
   my_data = (struct thread_data *) threadarg;
   cout << "Thread ID : " << my_data->thread_id ;
   cout << " Message : " << my_data->message << endl;
   pthread_exit(NULL);
}
int main ()
{
   pthread_t threads[num_threads];
   struct thread_data td[num_threads];
   int rc;
   int i;
   for( i=0; i < num_threads; i++ )
     {
      cout <<"main() : creating thread, " << i << endl;
      td[i].thread_id = i;
      td[i].message = "This is message";
      rc = pthread_create(&threads[i], NULL, printhello, (void *)&td[i]);
      if (rc)
      {
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}
 
Output
 
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Thread ID : 3 Message : This is message
Thread ID : 2 Message : This is message
Thread ID : 0 Message : This is message
Thread ID : 1 Message : This is message
Thread ID : 4 Message : This is message

No comments:

Post a Comment