Virtual Functions : Programs

************************PROGRAM 1****************************
//Demonstration of friend function
#include <iostream>
using namespace std;
class ABC
{
    int a;
    public:
    ABC(int i)
    {
    a=i;
    }
    void show();
    friend void display(ABC);
};
void ABC::show()
{
    cout<<"Value is :\t"<<a;
}

void display(ABC o1)
{
    cout<<"\nValue is :\t"<<o1.a;
}

/*void other(ABC o2)
{
    cout<<"\nValue is :\t"<<o2.a;
}*/
int main()
{
    ABC a(100);
    a.show();
    display(a);
//    other(a);

 return 0;
}

************************PROGRAM 2****************************

//Demonstration of a function friend of two classes. 
//Note that single function can be friend of any number of classes
#include <iostream>
using namespace std;
class PQR;
class ABC
{
    int a;
    public:
    ABC(int i)
    {
    a=i;
    }
    void show();
    friend void display(ABC,PQR);
};
void ABC::show()
{
    cout<<"\nValue is :\t"<<a;
}
class PQR
{
    int b;
    public:
    PQR(int i)
    {
    b=i;
    }
    void show();
    friend void display(ABC,PQR);
};
void display(ABC o1,PQR o2)
{
    cout<<"\nAddition is :\t"<<o1.a+o2.b;
}
void PQR::show()
{
    cout<<"\nValue is :\t"<<b;
}

int main()
{
    ABC a(100);
    a.show();
    PQR b(200);
    b.show();
    display(a,b);
//    other(a);

 return 0;
}


***********************PROGRAM 3 *******************
//DEMONSTRATION OF POOINTER TO OBJECT

#include<iostream>
using namespace std;
class Time
{
    short int hh, mm, ss;
    public:
        Time()
        {
            hh = mm = ss = 0;
        }
        void getdata(int i, int j, int k)
        {
            hh = i;
            mm = j;
            ss = k;
        }
        void prndata(void)
        {
            cout<<"\nTime is "<<hh<<":"<<mm<<":"<<ss<<"\n";
        }
};
int main()
{
    Time T1, *tptr;
    cout<<"Initializing data members using the object\n";
    T1.getdata(12,22,11);
    cout<<"Printing members using the object ";
    T1.prndata();
    tptr = &T1;
    cout<<"Printing members using the object pointer ";
    tptr->prndata();
    cout<<"\nInitializing data members using the object pointer, with values 15, 10, 16\n";
    tptr->getdata(15, 10, 16);
    cout<<"printing members using the object ";
    T1.prndata();
    cout<<"Printing members using the object pointer ";
    tptr->prndata();

    cout<<"\nInitializing data members using the object pointer, with values 22, 33, 44\n";
    (*tptr).getdata(22,33,44);
    cout<<"Printing members using the object pointer ";
    (*tptr).prndata();


}


************************PROGRAM 4****************************
//Program to demonstrate base class pointer pointing to derived class
#include <iostream>
using namespace std;
class B
{
    public:
       void display()
         { cout<<"Content of base class.\n"; }
};

class D : public B
{
    public:
       void display()
         { cout<<"Content of derived class.\n"; }
};

int main()
{
    B *b;
    D d;
    b->display();
    b = &d;    /* Address of object d in pointer variable */
    b->display();
    return 0;
}

************************PROGRAM 5****************************
// pointers to base class
#include <iostream>
using namespace std;
class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
   
};

class Rectangle: public Polygon {
  public:
    int area()
      { return width*height; }
};

class Triangle: public Polygon {
  public:
    int area()
      { return width*height/2; }
};

int main () {
  Rectangle rect;
  Triangle trgl;
  Polygon * ppoly1 = &rect;
  Polygon * ppoly2 = &trgl;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  cout << rect.area() << '\n';
  cout << trgl.area() << '\n';
 
   return 0;
}

************************PROGRAM 6****************************

//PROGRAM TO DEMONSTRATE POINTER TO DERIVED CLASS
#include <iostream>
using namespace std;
class BaseClass {
  int x;
public:
  void setx(int i) { 
     x = i; 
  }
  int getx() { 
     return x; 
  }
};

class DerivedClass : public BaseClass {
  int y;
public:
  void sety(int i) { 
     y = i; 
  }
  int gety() { 
     return y; 
  }
};

int main()
{
  BaseClass *p;         
      // pointer to BaseClass type
  BaseClass baseObject;
             // object of BaseClass
  DerivedClass derivedObject; 
      // object of DerivedClass

  p = &baseObject;  
                 // use p to access BaseClass object
  p->setx(10);                      
          // access BaseClass object
  cout << "Base object x: " << p->getx() << '\n';


  p = &derivedObject;                         // point to  DerivedClass object 
  p->setx(99);                                // access DerivedClass object

  
  derivedObject.sety(88);                  
   // can't use p to set y, so do it directly
  cout << "Derived object x: " << p->getx() << '\n';
  cout << "Derived object y: " << derivedObject.gety() << '\n';

  return 0;
}

************************PROGRAM 7****************************

//ANOTHER PROGRAM TO DEMONSTRATE POINTER TO OBJECT
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
            int rollno;
            string name;
public:
            student():rollno(0),name("")
            {}
            student(int r, string n): rollno(r),name (n)
            {}
            void get()
            {
                        cout<<"enter roll no";
                        cin>>rollno;
                        cout<<"enter  name";
                        cin>>name;
            }
            void print()
            {
                        cout<<"roll no is "<<rollno;
                        cout<<"name is "<<name;
            }
};
void main ()
{
            student *ps=new student;
            (*ps).get();
            (*ps).print();
            delete ps;
}