Thursday, October 6, 2016

Virtual Functions :)

As we  Know that the Polymorphism is the feature of OOP,which means we use the same name but multiple forms.
So in case of Run time Polymorphism the Virtual Functions Are used which tell about the binding of function procedure with its call at run time.
Virtual Functions done this by checking the object to which the pointer is pointing rather than the type of pointer.
Following is a Simple program to illustrate the above concept

#include<iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"Display base\n";
}
virtual void show()
{
cout<<"Base Virtual\n";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"Display Derived\n"; }
    void show()
{
cout<<"Derived Virtual\n";
}
};
main()
{
Base b;
Derived d;
    Base *p=&b;
p->display();
p->show();
p=&d;
p->display();
p->show();
}

Output:



















So as we can see in the output the Display function of base class is called two times because it is not virtual
On the other hand the show function is called for both derived and base class because it is made virtual.

Wednesday, October 5, 2016

MULTIPATH INHERITANCE AND VIRTUAL CLASSES :)

Multipath Inheritance in C++ implements the use of Virtual Classes.


We make the base class 'virtual' by adding the keyword 'virtual' during declaration of Derived class.

#include <iostream>
using namespace std;
class A
{
public:
    int a1;
    A()
    {
        cout<<"Constructor of Base Class A";
    }
};

class B:public virtual A //virtual public A is also OK
{
public:
    int a2;
    B()
    {
        cout<<"\nConstructor of Base Class B";
    }
};

class C:public virtual A
{
public:
    int a3;
    C()
    {
        cout<<"\nConstructor of Class C";
    }
};

class D:public B,public C
{
public:
    int a4;
    D()
    {
        cout<<"\nConstructor of Derived Class D\n\n";
    }
};
int main()
{
    D obj;
    obj.a1=10;
    obj.a2=20;
    obj.a3=30;
    obj.a4=40;
    cout<<"a="<<obj.a1;
    cout<<"\nb="<<obj.a2;
    cout<<"\nc="<<obj.a3;
    cout<<"\nd="<<obj.a4;
    return 0;
}




Tuesday, October 4, 2016

LOWER TRIANGLE MATRIX PATTERN :)

Program to Display Lower Triangular Green Matrix Pattern with Random Numbers.

#include <iostream>
#include<stdlib.h>
using namespace std;
int main( )
{
    system("COLOR A");   //Function to make Textcolor GREEN
start:
    int i,j;
    short int x;
    x=rand();
    for(j=1; j<=i; j++)
    {
        for(i=1; i<=10; i++)
        {
            if(i<=j)
                cout<<x;
            continue;
        }
        cout<<"\n";
    }
    cout<<"\n";
    goto start;
}


Constructors And Destructors In Multilevel Inheritance:)

If we create Constructors in All Levels in Multilevel Inheritance then the order of execution of Constructors is Same As the level
That is First The Base then Base 1 and Then Derived Class Constructor is Called

Destructors Follows the reverse order of inheritace


Program to Illustrate The Calling Of Constructors And Destructors During Multilevel Inheritance:


#include<iostream>
using namespace std;
class base
{
    int a;
    public:
        base(int b)
        {
            a=b;
            cout<<"\nBase Constructor Called";
            cout<<"\nA= "<<a;
        }
        ~base()
        {
            cout<<"\nBase Destructor Called";
        }
};
class base1:public base
{
    int b;
    public:
        base1(int a,int b):base(b) // the base1 is derived from base
        {
            b=a;
            cout<<"\nBase 1 Constructor Called";
            cout<<"\nB= "<<b;
        }
        ~base1()
        {
            cout<<"\nBase1 Destructor Called";
        }
};
class derived:public base1
{
    int x;
    public:
    derived(int y,int a,int b):base1(a,y)//one parameter for base class and other for base 1
    {
        x=y;
        cout<<"\nDerived Construtor Called";
        cout<<"\nX= "<<x;    }
~derived()
{
    cout<<"\nDerived Destructor Called";
}
};
main()
{
    derived d(10,20,21);
}

Output:















Else in all other inheritances The Base Class Constructor called First Then The Derived Class Constructor.

EMPLOYEE DATABASE~(Solution to E Balagurusamy Problem 8.3) :-

#include <iostream>
#include <stdlib.h>
using namespace std;
class staff
{
protected:
    char name[20],id[5];
    int age;
};

class teacher:public staff
{
protected:
    char subject[10];
public:
    void getinfo()
    {
        cout<<"\nEnter Name: ";
        cin>>name;
        cout<<"Enter Age: ";
        cin>>age;
        cout<<"Enter Employee ID: ";
        cin>>id;
        cout<<"Enter Subject of Publication: ";
        cin>>subject;
    }
    void putinfo()
    {
        cout<<"Name: "<<name;
        cout<<"\nAge: "<<age;
        cout<<"\nEmployee ID: "<<id;
        cout<<"\nSubject: "<<subject;
    }
};

class typist:public staff
{
protected:
    int speed;;
public:
    void typ()
    {
        cout<<"Enter Typing Speed(Out of 5): ";
        cin>>speed;
    }
};

class regular:public typist
{
protected:
    int salary;
public:
    void inp()
    {
        typ();
        cout<<"Enter Monthly Salary: ";
        cin>>salary;
    }
    void outp()
    {
        cout<<"\nTyping Speed:" <<speed;
        cout<<"\nAnnual Salary: "<<salary*12;
    }
};

class casual:public typist
{
protected:
    int days,sal;
public:
    void getdata()
    {
        typ();
        cout<<"Enter Number of Working Days: ";
        cin>>days;
        cout<<"\nEnter Salary per Day: ";
        cin>>sal;
    }
    void showdata()
    {
        cout<<"\nTyping Speed:" <<speed;
        cout<<"\nTotal Salary:"<<sal*days;
    }
};

class officer:public staff
{
protected:
    char grade;
public:
    void getd()
    {
        cout<<"Enter Grade: ";
        cin>>grade;
    }
    void display()
    {
        cout<<"\nGrade: "<<grade;
    }
};

int main()
{
    system("COLOR D");
    char ch;
    cout<<"\t******EMPLOYEE DATABASE******\n";
    teacher t;
    t.getinfo();
    cout<<"Enter Typist Choice:(r/c)\n";
    cout<<"R:Regular Typist  C:Casual Typist\n";
    cin>>ch;
    if(ch=='r'||ch=='R')
    {
        regular s;
        s.inp();
    }
    else
    {
        casual s;
        s.getdata();
    }
    officer x;
    x.getd();
    cout<<"\n\n\t****Employee Information****\n";
    t.putinfo();
    if(ch=='r'||ch=='R')
    {
        regular s;
        s.outp();
    }
    else
    {
        casual s;
        s.showdata();
    }
    x.display();
    return 0;
}


Monday, October 3, 2016

STATIC AND NON-STATIC VARIABLES:

Program to Show Difference between static and non-static member variables.

#include <iostream>
using namespace std;
class sample
{
    static int c; //static variable
    int x; //non-static variable

public:
    void reset()
    {
        x=0;
    }

    void count()
    {
        ++c;
        ++x;
        cout<<"\n\nStatic Variable:\n";
        cout<<"Value of c="<<c;
        cout<<"\nAddress of c="<<(unsigned)&c; //address of c
        cout<<"\n\nNon-Static Variable:\n";
        cout<<"Value of x="<<x;
        cout<<"\nAddress of x="<<(unsigned)&x; //address of x
    }
};
int sample::c=0; //initialising static variable

int main()
{
    sample s1,s2,s3;
    s1.reset();
    s2.reset();
    s3.reset();
    s1.count();
    s2.count();
    s3.count();
    return 0;
}



Bank Systems :) (Solution To E Balagurusamy Problem 8.1:)

#include<iostream>
#include<math.h>
using namespace std;
class account
{
    char name[20];
    long ac_no;
    protected:
        int bal;
        public:
            char type[20];
            void getdetails()
            {
                cout<<"Name of customer";
                cin>>name;
                cout<<"Account Number";
                cin>>ac_no;
                cout<<"Balance";
                cin>>bal;
            }
            void putdetails()
            {
            cout<<"Name: "<<name<<"\nAccount No: "<<ac_no<<"\nBalance: "<<bal;
            }
            void deposit()
            {
                int x;
                cout<<"Enter Amount To Deposit ";
                cin>>x;
                bal=bal+x;
            }
            void withdrawl(){
                int x;
                cout<<"Enter Amount To Withdraw  ";
                cin>>x;
                if(bal>=x)
                {
                bal=bal-x;
            }
            else
            {
                cout<<"\nBalance is Not Sufficient";
            }
        }
};
class savings:public account
{
    int interest;
    public:
        void interestt(float r=0.075,int t=1)
        {
            bal=bal*pow((1+r),t);
        }
};
class current:public account
{

    public:
        void min()
        {
            if(bal<1000)
            {
                bal=bal-100;
            }
        }
        void cheque()
        {
            int x;
            cout<<"Enter The Money To Be Debited ";
            cin>>x;
            bal=bal-x;
        }
};
main()
{ savings s;
     current c;
     int ch;
     cout<<"Enter account type";
     cout<<"\nPress 1 for Saving";
     cout<<"\nPress 2 for Current\n";
     cin>>ch;
     if(ch==1)
     {s.getdetails();}
     else
     {c.getdetails();}
     cout<<"\n\n\nWHAT DO U WANT TO DO\n\n\n";
     cout<<"************************************************************";
     int c1=1;
     while(c1!=7)
        {
         cout<<"\n1. TO WITHDRAWL  ";
         cout<<"\n2. TO DEPOSIT  ";
         cout<<"\n3. TO DISPLAY  ";
         cout<<"\n4. Cheque Withdrawl  ";
         cout<<"\n5. Interest ";
         cout<<"\n6. Minimum Balance ";
         cout<<"\n7. To Exit\n ";
         cin>>c1;
         switch(c1)
            {
             case 1:
                if(ch==1)
                s.withdrawl();
                else
                c.withdrawl();
                break;
             case 2:
                if(ch==1)
                s.deposit();
                else
                c.deposit();
                break;
             case 3:
                if(ch==1)
                s.putdetails();
                else
                c.putdetails();
                break;

             case 4:
                if(ch==1)
                cout<<"NO Chequebook Facility";
                else
                c.cheque();
                break;
             case 5:
                 if(ch==1)
                s.interestt();
                else
                cout<<"Curret Accounts Get No Compound Interest";
                break;
             case 6:
                 if(ch==1)
                cout<<"No Penality";
                else
                c.min();
                break;
            case 7:
                break;
             }
         }
    cout<<"\n\n\nTHANKS FOR USING OUR SERVICE";
}

Output: