Program to Illustrates Virtual Function


#include<iostream.h>
#include<conio.h>
class Base
{
public :
     void display( )
    {
           cout<<"\n Display Base ";
     }
     virtual void show( )
    { 
           cout<<"\n Show Base ";
     }
};
class Derived : public Base
{
            public :
                  void display( )
                  {
                         cout<<"\n Display Derived ";
                   }
        void show( )
       { 
           cout<<"\n Show Derived ";
     }
};
void main( )
{
Base B;
Derived D;
Base *bptr;

cout<<"\n bptr points to Base  ";
bptr = &B;
bptr -> display( );                   //calls Base version
bptr -> show( );                      // calls Base version

cout<<" \n\n bptr points to Derived  ";
bptr = &D;
bptr -> display( );                  //calls Base version
bptr -> show( );                    // calls Derived version

                getch( );
}
READ MORE - Program to Illustrates Virtual Function

Program to illustrates Dynamic binding through Virtual Functions


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle                         // base class
{
        char make[20];
        int milage;
    public :
        vehicle(char*, int);

        virtual void display();        // virtual function
};
class two_wheeler : public vehicle
{
        char is_auto_clutch;
    public :
        two_wheeler(char*, int, char);

        void display();
};
class four_wheeler : public vehicle
{
        char is_ac;
    public :
        four_wheeler(char*, int, char);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
    strcpy(make, a);
    milage = b;
}
void vehicle::display()
{
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_auto_clutch = c;
}
void two_wheeler::display()
{
    cout << "Two-wheeler Details - \n";
    vehicle::display();
    cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_ac = c;
}
void four_wheeler::display()
{
    cout << "Four-wheeler Details - \n";
    vehicle::display();
    cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    vehicle *base;        // base class pointer
    two_wheeler bike("Hero Honda",60,'N');
    base = &bike;
    base->display();
    cout << "\n";
    four_wheeler car("Maruti",18,'Y');
    base = &car;
    base->display();
}
READ MORE - Program to illustrates Dynamic binding through Virtual Functions

Program to illustrates Virtual base class


#include<iostream.h>
#include<conio.h>
class student
{
protected :
       int roll_number;
public:
    void get_number(int a)
    {
        roll_number = a;
     }
     void put_number(void)
    {
         cout<<"Roll No.  : "<<roll_number<<endl;
     }
};


class test : virtual public student
{
 protected :
       float sem1, sem2;
public:
    void get_marks (float s1, float s2)
    {
        sem1 = s1 ;  sem2 =  s2;
     }
     void put_marks(void)
    {
         cout<<"Marks obtained   : "<<"\n"
             <<"Sem1 = " <<sem1<<"\n"
             <<"Sem2 = " <<sem2<<"\n";
     }
};


class sports : public virtual student
{
 protected :
       float score;
public:
    void get_score(float s)
    {
        score = s;
     }
     void put_score(void)
    {
         cout<<" Sports weight : "<<score<<"\n\n";
     }
};

class result : public test, public sports
{
float total;
public :
       void display(void);
};
 void result :: display (void)
{
total = sem1 + sem2 + score;
put_number( );
put_marks( );
put_score( );

cout<<"Total Marks : "<<total<<"\n";
}

void main( )
{
result R1;
R1.get_number(123);
R1.get_marks(1200,1000);
R1.get_score(150);
R1.display( );
getch( );
}
READ MORE - Program to illustrates Virtual base class

Program to illustrates this pointer



#include<iostream.h>
#include<conio.h>
class example
{
private :
         int i;
public :
         void setdata(int ii)
         {
i = ii;                  //one way to set data
cout<<endl<<" My object's address is "<<this << endl;
this->i=ii;          // another way to set data
                    }
                     void showdata( )
                    {
cout<<i;                   // one way to display data
cout<<endl<<" My object's address is "<<this<< endl;
cout<<this->i;         //another way to display data
                     }
};

void main( )
{
example e1;
e1.setdata(10);
e1.showdata( );
getch( );
}
READ MORE - Program to illustrates this pointer

Program to illustrates Pure Virtual Function


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle                                 // Abstract base class
{
    protected :
        char make[20];
        int milage;
    public :
        vehicle(char*, int);

        virtual void display() = 0;    // pure virtual function
};
class two_wheeler : public vehicle
{
        char is_auto_clutch;
    public :
        two_wheeler(char*, int, char);

        void display();
};
class four_wheeler : public vehicle
{
        char is_ac;
    public :
        four_wheeler(char*, int, char);

        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
    strcpy(make, a);
    milage = b;
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_auto_clutch = c;
}
void two_wheeler::display()
{
    cout << "Two-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_ac = c;
}
void four_wheeler::display()
{
    cout << "Four-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    vehicle *base;            // base class pointer
    two_wheeler bike("Hero Honda",60,'N');
    base = &bike;
    base->display();
    cout << "\n";
    four_wheeler car("Maruti",18,'Y');
    base = &car;
    base->display();
}
READ MORE - Program to illustrates Pure Virtual Function

Program to illustrates Single Inheritance by PUBLIC Derivation


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member

        void set_a1(int);        // public member functions
        void display_a1a2();
};
// B as an extension of A
class B : public A                // public derivation
{
        int b;                    // private data memeber
    public :
        void set_a2_and_b(int, int);// public member functions
        void display_b();
};

/*----------------------Class Implementations---------------------*/

// member function definitions for class A
void A::set_a1(int x)
{
    a1 = x;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}

// member function definitions for class B
void B::set_a2_and_b(int x, int y)
{
    a2 = x;    // accessing public data member of base directly
    b = y;
}
void B::display_b()
{
    cout << "b  = " << b << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    B obj;

    // accessing public member function of the base directly
    obj.set_a1(10);

    obj.set_a2_and_b(20,30);

    // accessing public member function of the base directly
    obj.display_a1a2();

    obj.display_b();
}
/*
a1 = 10
a2 = 20
b  = 30
*/
READ MORE - Program to illustrates Single Inheritance by PUBLIC Derivation

Program to illustrates Virtual Function Empty in the Base Class


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle                                 // base class
{
    protected :
        char make[20];
        int milage;
    public :
        vehicle(char*, int);

        virtual void display() { }        // empty virtual function
};
class two_wheeler : public vehicle
{
        char is_auto_clutch;
    public :
        two_wheeler(char*, int, char);

        void display();
};
class four_wheeler : public vehicle
{
        char is_ac;
    public :
        four_wheeler(char*, int, char);

        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
    strcpy(make, a);
    milage = b;
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_auto_clutch = c;
}
void two_wheeler::display()
{
    cout << "Two-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_ac = c;
}
void four_wheeler::display()
{
    cout << "Four-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    vehicle *base;        // base class pointer

    two_wheeler bike("Hero Honda",60,'N');
    base = &bike;
    base->display();
    cout << "\n";

    four_wheeler car("Maruti",18,'Y');
    base = &car;
    base->display();
}
READ MORE - Program to illustrates Virtual Function Empty in the Base Class

Pointers to Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                        // private data member
    public :
        int a2;                        // public data member
        void set_a1(int);        // public member functions
        void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
    a1 = x;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    A obj, *p;        // pointer to object declared
    p = &obj;              // pointer to object assigned
    p->set_a1(10);
    p->a2 = 20;
    (*p).display_a1a2();// same as 'p->display_a1a2()'
}
READ MORE - Pointers to Objects

Pointers to Class Members with Pointers to Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member

        A(int, int);
        void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
A::A(int x, int y)
{
    a1 = x;
    a2 = y;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    int A::*ip;         // declaration of pointer to data member
                            // of class 'A'
    void (A::*fp)();    // declaration of pointer to member function
                            // of class 'A'
    A obj(12,14), *p;
    p = &obj;                // pointer to object assigned
    ip = &A::a2;         // pointers to class members assigned
    fp = &A::display_a1a2;
    p->*ip = 15;        // modified object's data member through pointer
    (p->*fp)();        // call to member function through pointer
}
READ MORE - Pointers to Class Members with Pointers to Objects

Pointers to Class Members


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member
        A(int, int);
        void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::A(int x, int y)
{
    a1 = x;
    a2 = y;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    int A::*ip;         // declaration of pointer to data member
                          // of class 'A'
    void (A::*fp)();    // declaration of pointer to member function
                            // of class 'A'
    A obj(12,14);

    ip = &A::a2;        // pointers to class members assigned
    fp = &A::display_a1a2;

    obj.*ip = 15;    // modified object's data member through pointer

    (obj.*fp)();        // call to member function through pointer
}
READ MORE - Pointers to Class Members

Pointers to Derived Class Objects


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member

        void set(int);
        void display();
};
class B : public A
{
        int a3;                    // private data member
    public :
        int a4;                    // public data member

        void set(int);       // overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set(int x)
{
    a1 = x;
}
void A::display()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
// member function definitions for class B
void B::set(int x)
{
    a3 = x;
}
void B::display()
{
    A::display();
    cout << "a3 = " << a3 << "\n";
    cout << "a4 = " << a4 << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    A *base, obj1;
    base = &obj1;
    base->set(1);
    base->a2 = 2;
    cout << "Base class data members : \n";
    base->display();
    B obj2;                    // Derived class object
    base = &obj2;
    base->set(10);
    base->a2 = 20;
    cout << "Base class data members (through subsumption) : \n";
    base->display();
    B *derived;
    derived = &obj2;
    derived->set(30);
    derived->a4 = 40;
    cout << "Derived class data members : \n";
    derived->display();
}
READ MORE - Pointers to Derived Class Objects

Program Overriding Member Functions


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee
{
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display_heading();
        void display();
};
class manager : public employee
{
        int no_of_projects;
    public:
        void set(int, char*, int);    // functions overriding the
        void display_heading();        // base class definitions

        void display_projects();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'
void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display_heading()
{
    cout << "Employee Information : \n";
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
    no_of_projects = p;
    employee::set(c, s);// base class function explicitly invoked
}
void manager::display_heading()
{
    cout << "Manager Details : \n";
}
void manager::display_projects()
{
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    manager peter;

    peter.set(1000, "Peter", 4);
    peter.display_heading();
    peter.display();
    peter.display_projects();
}
READ MORE - Program Overriding Member Functions

Program to illustrates Container Classes


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class character
{
        char element;
    public :
        void set_char(char);
        void display_char();
};
class word                          // container class
{
        character w[5];         // contain array of objects of
        int length;                        // class 'character'
    public :
        void set_word(char*);
        int get_length();
        void display_word();
};
class line               // container class
{
        word l[6];            // contain array of objects of
                                // class 'word'
    public :
        void set_line(char**);
        void display_line();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'character'
void character::set_char(char a)
{
    element = a;
}
void character::display_char()
{
    cout << element;
}

// member function definitions for 'word'
void word::set_word(char* a)
{
    length = strlen(a);
    for (int i=0; i<length; i++)
        w[i].set_char(a[i]);
}
int word::get_length()
{
    return length;
}
void word::display_word()
{
    for (int i=0; i<length; i++)
        w[i].display_char();
    cout << " ";
}
// member function definitions for 'line'
void line::set_line(char** a)
{
    for (int i=0; i<6; i++)
        l[i].set_word(a[i]);
}
void line::display_line()
{
    cout << "The Line of text is :\n";
    for (int i=0; i<6; i++)
        l[i].display_word();
    cout << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    static char* a[6] = {"abc", "def", "ghij", "klmn", "op", "qr"};
    line L;
    L.set_line(a);
    L.display_line();
}
READ MORE - Program to illustrates Container Classes

Program to illustrates Container Classes with constructors


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int x;
    public :
        A(int);                    // Constructor
        void display();
};
class B
{
        char w[10];
        int y;
    public :
        B(char*,int);            // Constructor
        void display();
};
class C                        // container class
{
        A obj1;                    // object of class A
        B obj2;                    // object of class B
        int z;
    public :
        C(int,int,int,char*);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'A'
A::A(int a)
{
    x = a;
}
void A::display()
{
    cout << "x = "<< x << "\n";
}
// member function definitions for 'B'
B::B(char* a, int b)
{
    strcpy(w, a);
    y = b;
}
void B::display()
{
    cout << "w = " << w << "\n";
    cout << "y = " << y << "\n";
}
// member function definitions for 'C'
C::C(int a, int b, int c, char* d): obj1(a), obj2(d,c)
{
    z = b;
}
void C::display()
{
    obj1.display();
    obj2.display();
    cout << "z = " << z << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    C c(10,20,30,"God");
    c.display();
}
READ MORE - Program to illustrates Container Classes with constructors

Program to Illustrates Virtual Function


#include<iostream.h>
#include<conio.h>
class Base
{
public :
     void display( )
    {
       cout<<"\n Display Base ";
     }
     virtual void show( )
    {
       cout<<"\n Show Base ";
     }
};
class Derived : public Base
{
        public :
          void display( )
          {
             cout<<"\n Display Derived ";
           }
    void show( )
       {
       cout<<"\n Show Derived ";
     }
};
void main( )
{
Base B;
Derived D;
Base *bptr;

cout<<"\n bptr points to Base  ";
bptr = &B;
bptr -> display( );                   //calls Base version
bptr -> show( );                      // calls Base version

cout<<" \n\n bptr points to Derived  ";
bptr = &D;
bptr -> display( );                  //calls Base version
bptr -> show( );                    // calls Derived version

        getch( );
}
READ MORE - Program to Illustrates Virtual Function

Program to illustrates Multi-level Inheritance with protected members


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class employee                        // base class
{
    protected :
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display();
};
class manager : protected employee     // Level - 1
{
    protected :
        int no_of_projects;
    public:
        void set(int, char*, int);     // overriding functions
        void display();
};
class area_manager : protected manager // Level - 2
{
        char location[10];
    public:
        void set(int, char*, int, char*);// overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'

void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'
void manager::set(int c, char* s, int p)
{
    //protected members of the parent are directly available
    empno = c;
    strcpy(name, s);

    no_of_projects = p;
}
void manager::display()
{
    //protected members of the parent are directly available
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";

    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
    //protected members of the grand-parent are directly available
    empno = c;
    strcpy(name, s);

    //protected member of the parent is directly available
    no_of_projects = p;

    strcpy(location, l);
}
void area_manager::display()
{
    //protected members of the grand-parent are directly available
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";

    //protected member of the parent is directly available
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";

    cout << "Location : ";
    cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    area_manager A;
    A.set(1001, "Samarth", 7, "New York");
    A.display();
}
READ MORE - Program to illustrates Multi-level Inheritance with protected members

Program to illustrates Multi-level Inheritance protected derivation



#include <iostream.h>
#include <string.h>

/*----------------------Class Interfaces-------------------------*/
class employee                        // base class
{
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display();
};
class manager : protected employee     // Level - 1
{
        int no_of_projects;
    public:
        void set(int, char*, int);     // overriding functions
        void display();
};
class area_manager : protected manager // Level - 2
{
        char location[10];
    public:
        void set(int, char*, int, char*);// overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'

void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'

void manager::set(int c, char* s, int p)
{
    employee::set(c, s);
    no_of_projects = p;
}
void manager::display()
{
    employee::display();
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
    manager::set(c, s, p);
    strcpy(location, l);
}
void area_manager::display()
{
    manager::display();
    cout << "Location : ";
    cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    area_manager A;

    A.set(1001, "Saachi", 7, "New York");
    A.display();
}
READ MORE - Program to illustrates Multi-level Inheritance protected derivation

Program to illustrates Multi-level Inheritance with private derivation


#include <iostream.h>
#include <string.h>

/*----------------------Class Interfaces-------------------------*/
class employee                        // base class
{
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display();
};
class manager : private employee     // Level - 1
{
        int no_of_projects;
    public:
        void set(int, char*, int);     // overriding functions
        void display();
};
class area_manager : private manager // Level - 2
{
        char location[10];
    public:
        void set(int, char*, int, char*);// overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'employee'

void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'

void manager::set(int c, char* s, int p)
{
    employee::set(c, s);
    no_of_projects = p;
}
void manager::display()
{
    employee::display();
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'
void area_manager::set(int c, char* s, int p, char* l)
{
    manager::set(c, s, p);
    strcpy(location, l);
}
void area_manager::display()
{
    manager::display();
    cout << "Location : ";
    cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    area_manager A;

    A.set(1001, "Kush", 7, "New York");
    A.display();
}
READ MORE - Program to illustrates Multi-level Inheritance with private derivation

Program to illustrates Multi-level Inheritance with public derivation



#include <iostream.h>
#include <string.h>

/*----------------------Class Interfaces-------------------------*/
class employee                        // base class
{
        char name[10];
        int empno;
    public :
        void set(int, char*);
        void display();
};
class manager : public employee     // Level - 1
{
        int no_of_projects;
    public:
        void set(int, char*, int);     // overriding functions
        void display();
};
class area_manager : public manager // Level - 2
{
        char location[10];
    public:
        void set(int, char*, int, char*);// overriding functions
        void display();
};

/*----------------------Class Implementations---------------------*/

// member function definitions for 'employee'

void employee::set(int c, char* s)
{
    empno = c;
    strcpy(name, s);
}
void employee::display()
{
    cout << "Employee No. : " << empno << "\n";
    cout << "Name         : " << name << "\n";
}
// member function definitions for 'manager'

void manager::set(int c, char* s, int p)
{
    employee::set(c, s);
    no_of_projects = p;
}
void manager::display()
{
    employee::display();
    cout << "No of Projects currently handling : ";
    cout << no_of_projects << "\n";
}
// member function definitions for 'area_manager'

void area_manager::set(int c, char* s, int p, char* l)
{
    manager::set(c, s, p);
    strcpy(location, l);
}
void area_manager::display()
{
    manager::display();
    cout << "Location : ";
    cout << location << "\n";
}
/*----------------------Class definitions ends here---------------*/

void main(void)
{
    area_manager A;

    A.set(1001, "Stavan", 5, "New York");
    A.display();
}
READ MORE - Program to illustrates Multi-level Inheritance with public derivation

Program to illustrates Mixed Inheritance Hierarchy with Abstract Classes


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account                                          // Abstract Base class
{
    protected :
        int number;
        char name[10];
    public :
        Account(int, char*);
        virtual void display() = 0;         // pure virtual function
};
class SavingsAccount : public Account
{
        int balance;
    public:
        SavingsAccount(int,char*,int);
        void display();
};
class Deposit                                  // Abstract Base class
{
    protected :
        int amount;
        char maturity_date[9];
    public:
        Deposit(int, char*);
        virtual void display() = 0;     // pure virtual function
};
class DepositAccount : public Account, public Deposit
{                                                 // Abstract Base class
    protected :
        char opening_date[9];
    public:
        DepositAccount(int,char*,int,char*,char*);
        virtual void display() = 0;     // pure virtual function
};
class ShortTerm : public DepositAccount
{
        int no_of_months;
    public:
        ShortTerm(int,char*,int,char*,char*,int);
        void display();
};
class LongTerm : public DepositAccount
{
        int no_of_years;
        int loan_taken;
    public:
        LongTerm(int,char*,int,char*,char*,int,int);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::Account(int a, char* b)
{
    number = a;
    strcpy(name, b);
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::SavingsAccount(int a, char* b, int c) :
                                    Account(a,b)
{
    balance = c;
}
void SavingsAccount::display()
{
    cout << "Savings Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposit'
void Deposit::Deposit(int a, char* b)
{
    amount = a;
    strcpy(maturity_date, b);
}
// member function definitions for 'DepositAccount'
void DepositAccount::DepositAccount(int a, char* b, int c, char* d, char* e)
                                : Account(a,b), Deposit(c,d)
{
    strcpy(opening_date, e);
}
// member function definitions for 'ShortTerm'
void ShortTerm::ShortTerm(int a, char* b, int c, char* d, char*
e, int f)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_months = f;
}
void ShortTerm::display()
{
    cout << "Short Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::LongTerm(int a, char* b, int c, char* d, char* e, int f, int g)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_years = f;
    loan_taken = g;
}
void LongTerm::display()
{
    cout << "Long Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Years : " << no_of_years << "\n";
    cout << "Loan Taken        : " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    Account *top1;            // base class pointer

    top1 = new SavingsAccount(1323, "Vikram", 10000);
    top1->display();
    delete top1;
    cout << "\n";

    cout << "Deposit Display using pointer";
    cout <<    " of type base class 'Account' - \n";

    top1 = new ShortTerm(17099, "Vilas", 25000, "12/05/02",                                    "12/02/02", 3);
    top1->display();
    delete top1;
    cout << "\n";

    top1 = new LongTerm(17169, "Vishwas", 30000, "15/03/04",
                       "15/03/02", 2, 1000);
    top1->display();
    delete top1;
    cout << "\n";

    Deposit *top2;         // base class pointer

    cout << "Deposit Display using pointer";
    cout <<    " of type second base class 'Deposit' - \n";

    top2 = new ShortTerm(17099, "Vilas", 25000, "12/05/02",                                     "12/02/02", 3);
    top2->display();
    delete top2;
    cout << "\n";

    top2 = new LongTerm(17169, "Vishwas", 30000, "15/03/04",
                       "15/03/02", 2, 1000);
    top2->display();
    delete top2;
    cout << "\n";
}
READ MORE - Program to illustrates Mixed Inheritance Hierarchy with Abstract Classes

Program to illustrates Hybrid Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account
{
        int number;
        char name[10];
    public :
        void set(int, char*);
        void display();
};
class SavingsAccount : public Account
{
        int balance;
    public:
        void set(int,char*,int);// Overriding functions
        void display();
};
class Deposite
{
        int amount;
        char maturity_date[9];
    public:
        void set(int, char*);
        void display();
};
class DepositeAccount : public Account, public Deposite
{
        char opening_date[9];
    public:
        void set(int,char*,int,char*,char*);// Overriding functions
        void display();
};
class ShortTerm : public DepositeAccount
{
        int no_of_months;
    public:
        void set(int,char*,int,char*,char*,int);// Overriding functions
        void display();
};
class LongTerm : public DepositeAccount
{
        int no_of_years;
        int loan_taken;
    public:
        void set(int,char*,int,char*,char*,int,int);// Overriding functions
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::set(int a, char* b)
{
    number = a;
    strcpy(name, b);
}
void Account::display()
{
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::set(int a, char* b, int c)
{
    Account::set(a,b);
    balance = c;
}
void SavingsAccount::display()
{
    cout << "Savings Account Details --- \n";
    Account::display();
    cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposite'
void Deposite::set(int a, char* b)
{
    amount = a;
    strcpy(maturity_date, b);
}
void Deposite::display()
{
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
}
// member function definitions for 'DepositeAccount'
void DepositeAccount::set(int a, char* b, int c, char* d, char* e)
{
    Account::set(a,b);
    Deposite::set(c,d);
    strcpy(opening_date, e);
}
void DepositeAccount::display()
{
    Account::display();
    Deposite::display();
    cout << "Date of Opening : " << opening_date << "\n";
}
// member function definitions for 'ShortTerm'
void ShortTerm::set(int a, char* b, int c, char* d, char* e, int f)
{
    DepositeAccount::set(a,b,c,d,e);
    no_of_months = f;
}
void ShortTerm::display()
{
    cout << "Short Term Deposite Account Details --- \n";
    DepositeAccount::display();
    cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::set(int a, char* b, int c, char* d, char* e, int f, int g)
{
    DepositeAccount::set(a,b,c,d,e);
    no_of_years = f;
    loan_taken = g;
}
void LongTerm::display()
{
    cout << "Long Term Deposite Account Details --- \n";
    DepositeAccount::display();
    cout << "Duration in Years : " << no_of_years << "\n";
    cout << "Loan Taken        : " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    SavingsAccount S;
    S.set(1323, "Stavan", 10000);
    S.display();
    cout << "\n";

    ShortTerm ST;
    ST.set(17099, "Kush", 25000, "12/05/02", "12/02/02", 3);
    ST.display();
    cout << "\n";

    LongTerm LT;
    LT.set(17169, "Vishwas", 30000, "15/03/04", "15/03/02", 2, 1000);
    LT.display();
    cout << "\n";
}
READ MORE - Program to illustrates Hybrid Inheritance

Program : constructor, destructor in single inheritance


#include<iostream.h>
class base
{
public:
base()
{
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class


class derived : public base
{
public:
derived()
{
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
};//end of derived class


void main()
{
derived obj;
}
READ MORE - Program : constructor, destructor in single inheritance

Program to illustrates Hierarchical Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account
{
        int number;
        char name[10];
    public :
        void set(int, char*);
        void display();
};
class SavingsAccount : public Account
{
        int balance;
    public:
        void set(int,char*,int);// Overriding functions
        void display();
};
class DepositeAccount : public Account
{
        int amount;
        char opening_date[9];
    public:
        void set(int,char*,int,char*);// Overriding functions
        void display();
};
class ShortTerm : public DepositeAccount
{
        int no_of_months;
    public:
        void set(int,char*,int,char*,int);// Overriding functions
        void display();
};
class LongTerm : public DepositeAccount
{
        int no_of_years;
        int loan_taken;
    public:
        void set(int,char*,int,char*,int,int);// Overriding functions
        void display();
};

/*----------------------Class Implementations---------------------*/

// member function definitions for 'Account'
void Account::set(int a, char* b)
{
    number = a;
    strcpy(name, b);
}
void Account::display()
{
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
}

// member function definitions for 'SavingsAccount'
void SavingsAccount::set(int a, char* b, int c)
{
    Account::set(a,b);
    balance = c;
}
void SavingsAccount::display()
{
    cout << "Savings Account Details --- \n";
    Account::display();
    cout << "Balance : " << balance << "\n";
}
// member function definitions for 'DepositeAccount'
void DepositeAccount::set(int a, char* b, int c, char* d)
{
    Account::set(a,b);
    amount = c;
    strcpy(opening_date, d);
}
void DepositeAccount::display()
{
    Account::display();
    cout << "Amount          : " << amount << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
}

// member function definitions for 'ShortTerm'
void ShortTerm::set(int a, char* b, int c, char* d, int e)
{
    DepositeAccount::set(a,b,c,d);
    no_of_months = e;
}
void ShortTerm::display()
{
    cout << "Short Term Deposite Account Details --- \n";
    DepositeAccount::display();
    cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::set(int a, char* b, int c, char* d, int e, int f)
{
    DepositeAccount::set(a,b,c,d);
    no_of_years = e;
    loan_taken = f;
}
void LongTerm::display()
{
    cout << "Long Term Deposite Account Details --- \n";
    DepositeAccount::display();
    cout << "Duration in Years : " << no_of_years << "\n";
    cout << "Loan Taken        : " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    SavingsAccount S;
    S.set(1323, "Stavan", 10000);
    S.display();
    cout << "\n";

    ShortTerm ST;
    ST.set(17099, "Kush", 25000, "12/02/07", 3);
    ST.display();
    cout << "\n";

    LongTerm LT;
    LT.set(17169, "Saachi", 30000, "15/03/07", 2, 1000);
    LT.display();
    cout << "\n";
}
READ MORE - Program to illustrates Hierarchical Inheritance

Program : constructor, destructor in multiple inheritance


#include<iostream.h>
#include<conio.h>
class base1
{
protected:
    int a;
public:
base1(int x)
{
a=x;
cout<<"Inside base1 constructor"<<endl;
}
~base1()
{
cout<<"Inside base1 destructor"<<endl;
}
};//end of base class

class base2
{
protected:
    int b;
public:
base2(int y)
{
b=y;
cout<<"Inside base2 constructor"<<endl;
}
~base2()
{
cout<<"Inside base2 destructor"<<endl;
}
};//end of derived class
class derived : public base1,public base2
{
int c;
public:
derived(int x,int y,int z):base1(y),base2(z)
{
c=x;
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
void show()
{
    cout<<"a : "<<a<<" "<<"b : "<<b<<" "<<"c : "<<c<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived obj(3,4,5);
obj.show();
}
READ MORE - Program : constructor, destructor in multiple inheritance

Program : constructor, destructor in multilevel inheritance


#include<iostream.h>
#include<conio.h>
class base
{
protected:
int a;
public:
base(int x)
{
a=x;
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class


class derived1 : public base
{
protected:
int b;
public:
derived1(int x,int y):base(x)
{
b=y;
cout<<"Inside derived1 constructor"<<endl;
}
~derived1()
{
cout<<"Inside derived1 destructor"<<endl;
}
};//end of derived class
class derived2 : public derived1
{
int c;
public:
derived2(int x,int y,int z):derived1(x,y)
{
c=z;
cout<<"Inside derived2 constructor"<<endl;
}
~derived2()
{
cout<<"Inside derived2 destructor"<<endl;
}
void show()
{
    cout<<"a : "<<a<<" "<<"b : "<<b<<"  "<<"c : "<<c<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived2 obj(3,4,5);
obj.show();
}
/*
Inside base constructor
Inside derived1 constructor
Inside derived2 constructor
a : 3 b : 4  c : 5
Inside derived2 destructor
Inside derived1 destructor
Inside base destructor
*/
READ MORE - Program : constructor, destructor in multilevel inheritance

Program : constructor, destructor in multiple inheritance


#include<iostream.h>
#include<conio.h>
class base1
{
public:
base1()
{
cout<<"Inside base1 constructor"<<endl;
}
~base1()
{
cout<<"Inside base1 destructor"<<endl;
}
};//end of base class
class base2
{
public:
base2()
{
cout<<"Inside base2 constructor"<<endl;
}
~base2()
{
cout<<"Inside base2 destructor"<<endl;
}
};//end of derived class
class derived : public base1,public base2
{
public:
derived()
{
cout<<"Inside derived constructor"<<endl;
}
~derived()
{
cout<<"Inside derived destructor"<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived obj;
}

/*
Inside base1 constructor
Inside base2 constructor
Inside derived constructor
Inside derived destructor
Inside base2 destructor
Inside base1 destructor
*/
READ MORE - Program : constructor, destructor in multiple inheritance

Array of Pointers to objects


#include <iostream.h>
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member

        void set_a1(int);
        void display_a1a2();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for class A
void A::set_a1(int x)
{
    a1 = x;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}
void main(void)
{
    A obj[5];                // array of objects declaration
    for (int i=0; i<5; i++)    // array of objects initialisation
    {
        obj[i].set_a1(i+1);
        obj[i].a2 = (i+1)*10;
    }
    A *p[5];                // array of pointers to objects - declaration
    for (i=0; i<5; i++) // array of pointers to objects -                                           initialisation
        p[i] = &obj[i];

    for (i=0; i<5; i++)     // usage of array of pointers to objects
    {
        cout << "Object - " << i+1 << ":\n";
        p[i]->display_a1a2();
        cout << "\n";
    }
}

/*
output
Object - 1:
a1 = 1
a2 = 10

Object - 2:
a1 = 2
a2 = 20

Object - 3:
a1 = 3
a2 = 30

Object - 4:
a1 = 4
a2 = 40

Object - 5:
a1 = 5
a2 = 50
*/
READ MORE - Array of Pointers to objects

Program to illustrates Mixed Inheritance Hierarchy with Abstract Classes


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account                                      // Abstract Base class
{
    protected :
        int number;
        char name[10];
    public :
        Account(int, char*);
        virtual void display() = 0;    // pure virtual function
};
class SavingsAccount : public Account
{
        int balance;
    public:
        SavingsAccount(int,char*,int);
        void display();
};
class Deposit                              // Abstract Base class
{
    protected :
        int amount;
        char maturity_date[9];
    public:
        Deposit(int, char*);
        virtual void display() = 0;    // pure virtual function
};
class DepositAccount : public Account, public Deposit
{                       
    protected :
        char opening_date[9];
    public:
        DepositAccount(int,char*,int,char*,char*);
        virtual void display() = 0;    // pure virtual function
};
class ShortTerm : public DepositAccount
{
        int no_of_months;
    public:
        ShortTerm(int,char*,int,char*,char*,int);
        void display();
};
class LongTerm : public DepositAccount
{
        int no_of_years;
        int loan_taken;
    public:
        LongTerm(int,char*,int,char*,char*,int,int);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::Account(int a, char* b)
{
    number = a;
    strcpy(name, b);
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::SavingsAccount(int a, char* b, int c) :
                                    Account(a,b)
{
    balance = c;
}
void SavingsAccount::display()
{
    cout << "Savings Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposit'
void Deposit::Deposit(int a, char* b)
{
    amount = a;
    strcpy(maturity_date, b);
}
// member function definitions for 'DepositAccount'
void DepositAccount::DepositAccount(int a, char* b, int c, char*
d, char* e)
                                : Account(a,b), Deposit(c,d)
{
    strcpy(opening_date, e);
}
// member function definitions for 'ShortTerm'
void ShortTerm::ShortTerm(int a, char* b, int c, char* d, char*
e, int f)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_months = f;
}
void ShortTerm::display()
{
    cout << "Short Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::LongTerm(int a, char* b, int c, char* d, char* e,
int f, int g)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_years = f;
    loan_taken = g;
}
void LongTerm::display()
{
    cout << "Long Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Years : " << no_of_years << "\n";
    cout << "Loan Taken        : " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    Account *top1;            // base class pointer

    top1 = new SavingsAccount(1323, "Stavan", 10000);
    top1->display();
    delete top1;
    cout << "\n";

    DepositAccount *top2;         // base class pointer

    cout << "Deposit Display using pointer";
    cout <<    " of type base class 'DepositAccount' - \n";

    top2 = new ShortTerm(17099, "Kush", 25000, "12/05/02",     "12/02/02", 3);
    top2->display();
    delete top2;
    cout << "\n";

    top2 = new LongTerm(17169, "Samarth", 30000, "15/03/04",
                               "15/03/02", 2, 1000);
    top2->display();
    delete top2;
    cout << "\n";
}
/*
ouput
Savings Account Details ---
Number : 1323
Name   : Stavan
Balance : 10000

Deposit Display using pointer of type base class 'DepositAccount' -
Short Term Deposit Account Details ---
Number : 17099
Name   : Kush
Amount : 25000
Maturity Date : 12/05/02
Date of Opening : 12/02/02
Duration in Months : 3

Long Term Deposit Account Details ---
Number : 17169
Name   : Samarth
Amount : 30000
Maturity Date : 15/03/04
Date of Opening : 15/03/02
Duration in Years : 2
Loan Taken        : 1000

*/
READ MORE - Program to illustrates Mixed Inheritance Hierarchy with Abstract Classes

Program to illustrates Multiple Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class base1                        // base class
{
        int x;
    public :
        void set_x(int);
        void display_x();
};
class base2                        // base class
{
        float y;
    public:
        void set_y(float);
        void display_y();
};
class derived : public base1, private base2
{
        char str[10];
    public:
        void set(float, char*);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'base1'

void base1::set_x(int c)
{
    x = c;
}
void base1::display_x()
{
    cout << "x = " << x << "\n";
}
// member function definitions for 'base2'
void base2::set_y(float c)
{
    y = c;
}
void base2::display_y()
{
    cout << "y = " << y << "\n";
}
// member function definitions for 'derived'
void derived::set(float b, char* c)
{
    /* public member of 'base2' becomes private in 'derived'
     hence, accessed within the function body */
    set_y(b);

    strcpy(str, c);
}
void derived::display()
{
    /* public member of 'base2' becomes private in 'derived'
        hence, accessed within the function body */
    display_y();

    cout << "str = ";
    cout << str << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    derived D;

    /* public member of the 'base1' is public in 'derived'
       hence, can be directly used */
    D.set_x(10);
    D.set(25.3, "God");
    /* public member of the 'base1' is public in 'derived'
       hence, can be directly used */
    D.display_x();

    D.display();
}
READ MORE - Program to illustrates Multiple Inheritance

Program to illustrate static members in class

#include <iostream.h>
#include<conio.h>
class test
{
    int code;
    static int count;    // static member variable
    public :
    void setcode (void)
    {
        code = ++count;
    }
    void showcode (void)
    {
        cout << "Object number : " << code << "\n";
    }
    static void showcount(void)        // static number function
    {
        cout << "Count : "<<count<<"\n";
    }
};
    int test :: count;
    int main()
    {
        test t1, t2;
        clrscr();
        t1.setcode();
        t2.setcode();

        test :: showcount();    // accessing static function

        test t3;
        t3.setcode ( );

        test :: showcount();

        t1.showcode();
        t2.showcode();
        t3.showcode();
        getch();
    }
READ MORE - Program to illustrate static members in class

Overloading Unary Operator as a Friend Function


#include <iostream.h>
#include<conio.h>
/*----------------------Class definition-------------------------*/
class sample
{
        int a;
        int b;
    public :
        sample() { }        // default empty constructor
        sample(int, int);

        // Overloaded unary minus as a friend
        friend void operator - (sample&);
        void print();
};
sample::sample(int x, int y)
{
    a = x;
    b = y;
}
void sample::print()
{
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";
}
/*----------------------Definition ends here---------------------*/
void operator - (sample& x)    // friend function definition
{
    x.a = -x.a;
    x.b = -x.b;
}
void main(void)
{
    sample A(10,20);
    clrscr();
    cout << "Before the application of operator '-'(unary minus) :\n";
    A.print();
    -A;                // invoking operator function
    cout << "After the application of operator '-'(unary minus) :\n";
    A.print();
    operator -(A);    // another way of invoking - as a member function
    cout << "After the second application of operator '-'(unary minus) :\n";
    A.print();
    getch();
}
READ MORE - Overloading Unary Operator as a Friend Function

Program to illustrate Unary Operator Overloading


#include <iostream.h>
#include<conio.h>
/*----------------------Class definition-------------------------*/
class sample
{
        int a;
        int b;
    public :
        sample() { }        // default empty constructor
        sample(int, int);
        void operator - ();    // Overloaded unary minus
        void print();
};
sample::sample(int x, int y)
{
    a = x;
    b = y;
}
void sample::operator - ()
{
    a = -a;
    b = -b;
}
void sample::print()
{
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
    sample A(10,20);
    clrscr();
    cout << "Before the application of operator '-'(unary minus) :\n";
    A.print();
    -A;                // operator function gets invoked
    cout << "After the application of operator '-'(unary minus) :\n";
    A.print();
    A.operator -();    // another way of invoking - as a member function
    cout << "After the second application of operator '-'(unary minus) :\n";
    A.print();
    getch();
}
READ MORE - Program to illustrate Unary Operator Overloading

Program to process shopping list


#include<iostream.h>
#include<conio.h>
#include<process.h>
class item
{
    int itemcode[50];
    float itemprice[50];
    int count;
    public :
    void cnt(void) { count = 0 ; }      // initializes count to 0
    void getitem(void);
    void displaysum(void);
    void remove(void);
    void displayitems(void);
};

void item :: getitem(void)           //assign values to data members of item
{
    cout<<"Enter item code : ";
    cin>>itemcode[count];
    cout<<"Enter item cost : ";
    cin>>itemprice[count];
    count++;
}
void item :: displaysum(void)               //display total value of all items
{
    float sum = 0;
    for(int i = 0; i<count; i++)
    sum=sum + itemprice[i];
    cout<<"\n Total value :"<<sum<<"\n";
}

void item :: remove(void)                     //deleting a specified item
{
    int a;
    cout<<" Enter item code : ";
    cin>>a;
    for(int i = 0; i<count; i++)
                   if (itemcode[i] == a)    
                          itemprice[i] = 0;
}
void item :: displayitems(void)            //displaying items
{
    cout<<"\n Code            Price \n ";
    for(int i = 0; i<count; i++)
    {
           cout<<"\n"<<itemcode[i];
           cout<<"        "<<itemprice[i];
    }
    cout<<"\n";
}
int main( )
{
    item  I;
    I.cnt( );
    int ch;
    do
    {
        cout<<"\n Option are……. \n";
        cout<<"\n1.    Add an item ";
        cout<<"\n2.    Display total value ";
        cout<<"\n3.    Delete an item";
        cout<<"\n4.    Display all items ";
        cout<<"\n5.    Exit";
             cout<<"\n\n  Enter your choice : ";
             cin>>ch; 
        switch(ch)
    {
              case 1 : I.getitem( );  break;
        case 2 : I.displaysum( );  break;
        case 3 : I.remove( );  break;
              case 4 : I.displayitems( );  break;
              case 5 : exit(0);
              default : cout<<"\n Error in input; try again \n";
    }
} while(ch !=5);
}
READ MORE - Program to process shopping list

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|