Friday, November 28, 2014

C++ Pointers & functions pro



Pointer to derived objects

 
#include 
#include
#include 
using namespace std;
class b_class 
{
  char author[80];
public:
  void put_author(char *s) 
  { 
   strcpy(author, s); 
  }
  void show_author() 
  { 
   cout << author << "\n"; 
  }
} ;
class d_class : public b_class 
{
  char title[80];
public:
  void put_title(char *num) 
  {
    strcpy(title, num);
  }
  void show_title() 
 {
    cout << "Title: ";
    cout <<  title << "\n";
  }
};
int main()
{
  b_class *p;
  b_class b_ob;
 
  d_class *dp;
  d_class d_ob;
  p = &b_ob;  
  
  p->put_author("tom clancy");
  p = &D_ob;
  p->put_author("William Shakespeare");
 
  b_ob.show_author();
  d_ob.show_author();
  cout << "\n";
  dp = &d_ob;
  dp->put_title("The Tempest");
  p->show_author();
  dp->show_title( );
  return 0;
}

Virtual functions and polymorphism

 
#include 
#include
using namespace std;
class figure 
{
protected:
  double x, y;
public:
  void set_dim(double i, double j=0) 
 {
    x = i;
    y = j;
  }
  virtual void show_area() 
 {
    cout << "No area computation defined ";
    cout << "for this class.\n";
  }
} ;
class triangle : public figure 
 {
  public:
    void show_area() 
     {
      cout << "Triangle with height ";
      cout << x << " and base " << y;
      cout << " has an area of ";
      cout << x * 0.5 * y << ".\n";
    }
};
class square : public figure 
{
  public:
    void show_area() 
    {
      cout << "Square with dimensions ";
      cout << x << "x" << y;
      cout << " has an area of ";
      cout << x *  y << ".\n";
    }
};
class circle : public figure
           {
  public:
    void show_area() 
    {
      cout << "Circle with radius ";
      cout << x;
      cout << " has an area of ";
      cout << 3.14 * x * x << ".\n";
    }
} ;
int main()
{
  figure *p; 
  triangle t; 
  square s;
  circle c;
  p = &t;
  p->set_dim(10.0, 5.0);
  p->show_area();
  p = &s;
  p->set_dim(10.0, 5.0);
  p->show_area();
 
  p = &c;
  p->set_dim(9.0);
  p->show_area();
  return 0;
}

A simple program demonstrating references

 
#include 
#include
using namespace std;
int main()
{
   int Len, Wid;     
   int &rLen = Len;
   int &rWid = Wid; 
   Len = 10;         
   Wid = 20;         
   cout << "Len is: " << Len << ", and  Wid is: " << Wid << endl;
   cout << "rLen is: " << rLen << ", and rWid is: " << rWid << endl;
   cout << endl;
   cout << "Address of Len is: " << &Len << endl;
   cout << "Address of rLen is: "  << &rLen << endl;
   if(&Len == &rLen)
   {
               cout << "Address of Len is equal to address of rLen!" << endl;
   }
   cout << "Address of Wid is: " << &Wid << endl;
   cout << "Address of rWid is: "  << &rWid << endl;
   if(&Wid == &Wid)
   {
               cout << "Address of Wid is equal to address of rWid!" << endl;
   }
 
   return 0;
}

Function: passing by references

 
#include 
#include
using namespace std;
void swap(int &i, int &j);  
int main()
{
   int numone = 0;
   int numtwo = 0;
   cout << "Please enter two integers: " << endl;
   cout << "Enter value for NumOne: " ;
   cin >> numone;
   cout << "Enter value for NumTwo: " ;
   cin >> numtwo;
   cout << "Before swapping, NumOne is: " << numone << endl;
   cout << "Before swapping, NumTwo is: " << numtwo<< endl;
   swap(NumOne, NumTwo);
   cout << "After swapping, NumOne is: " << numone << endl;
   cout << "After swapping, NumTwo is: " << numtwo<< endl;
   return 0;
}
void swap(int &i, int &j)
{
   int temp;
 
   temp = i;
   i = j;
   j = temp;
}

No comments:

Post a Comment