Saturday, September 24, 2016

Single Inheritance

Program To Illustrate The Single Inheritance :
#include<iostream>
using namespace std;
class base
{
int a;
protected:
int b;
public:
int c;
void getdata();
   void putdata();
};
void base:: getdata()
{
cout<<"Enter The Values Of A,B,C\n";
cin>>a>>b>>c;
}
void base::putdata()
{
cout<<"Values Are\n";
cout<<"A="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
class derive:public base
{
int d;
protected:
int e;
public:
void display()
{
cout<<"\nD="<<d<<"\nE="<<e;
}
void input()
{
cout<<"\nEnter D AND E\n";
cin>>d>>e;
}
};
main()
{
derive p;
p.getdata();
p.input();
p.putdata();
p.display();
}


Ambiguity:

When we use same name for member function in both base class and derived class then there is an ambiguity that which function will be invoked by the object of derived class in main function.

Solution:

So After checking it i found that the function in derived class is invoked by the object.Only if it is defined,if it is not defined then it will call the base class function.
We can Also Solve This Ambiguity By Using Scope Resolution Operator As Follows:

p.base::display();
p.derive::display();


Also See Our C program To print Alphabet pattern

ALPHABET Pattern :)

#include <stdio.h>
#include<conio.h>
main()
{
    int i, j;
    char input, alphabet = 'A';

    printf("Enter the uppercase character you want to print in last row: "); //asking number of rows from user
    scanf("%c",&input); //scanning number of rows user entered

    for(i=1;i<=(input-'A'+1);i++) //tells number of rows
    {
        for(j=1;j<=i;j++) //tells number of letters in a row
        {
            printf("%c", alphabet); //printing pattern
        }
        ++alphabet; //increment letter in next row

        printf("\n"); //jump to next row
    }
    getch();
}
OUTPUT:
A
BB
CCC
DDDD
EEEEEE

Friday, September 23, 2016

OPERATIONS ON QUEUES

Program to Perform Insertion and Deletion Operations on Queues Implemented as an Array.

#include<iostream>
#include <string.h>
using namespace std;
void add(int q[],int front,int rear);
void deletion(int q[],int front,int rear);
int main()
{
int q[30],choice;
int i,n,front=1,rear;
start:
cout<<"\n\t**OPERATIONS ON QUEUES**\n";
cout<<"Enter Size of Queue: ";
cin>>n;
cout<<"Enter Elements of the Queue: ";
for(i=front;i<=n;i++)
{
cin>>q[i];
rear=i;
}
cout<<"\n1: Insertion\n2: Deletion\n";
    cout<<"Enter your Choice(Press F6 to Exit): ";

    while(cin>>choice)
    {

        switch(choice)
        {
        case 1:
            add(q,front,rear);
            cout<<"\nEnd of Program";
            break;
        case 2:
            deletion(q,front,rear);
            cout<<"\nEnd of Program";
            break;
        default:
            cout<<"\nInvalid Input";
        }
        goto start;
    }
    return 0;
}

void add(int q[],int front,int rear)
{
int i,item;
cout<<"\nEnter Element to Be Inserted: ";
cin>>item;
rear++;
q[rear]=item;
cout<<"\nNew Queue is:\n";
for(i=front;i<=rear;i++)
cout<<q[i]<<" ";
}

void deletion(int q[],int front,int rear)
{
int i;
front++;
cout<<"\nQueue after Deletion is:\n";
for(i=front;i<=rear;i++)
cout<<q[i]<<" ";
}



Matrix Transpose

Program to Calculate Transpose of A Matrix:

#include<iostream>
using namespace std;
main()
{
int a[10][10],b[10][10],m,n,i,j;
cout<<"\nEnter The Order Of Matrix\n";
cin>>m>>n;
cout<<"Enter The Elements Rowwise";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\na["<<i<<"]["<<j<<"] =";
cin>>a[i][j];
}
}
cout<<"\nTranspose is\n";
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
b[j][i]=a[i][j];
}
for(i=0;i<n;i++)
{ cout<<"\n";
for(j=0;j<m;j++)
{
cout<<b[i][j]<<"\t";
}
}
}

Least Common Multiple

Program To Find LCM of Two Numbers:

#include<iostream>
using namespace std;
main()
{
    int num1,num2,min,hcf,i,lcm;
    cout<<"Enter Two Numbers To Find LCM\n";
    cin>>num1>>num2;
    min=(num1<num2)? num1:num2;
    for(i=1;i<=min;i++)
    {
        if(num1%i==0&&num2%i==0)
        {
            hcf=i;
        }
    } lcm=(num1*num2)/hcf;  
    cout<<"\nLeast Common Multiple Is "<<lcm;
    return 0;
}

Friend Function

Program To Illustrate The Concept Of Friend Function:

#include<iostream>
using namespace std;
class test;
class sample
{
    int x;
    public:
        sample()
        {
            cout<<"\nEnter The Value Of X ";
            cin>>x;
        }
        friend void max(sample &a,test &b);
};
class test
{
    int y;
    public:
        test()
        {
            cout<<"\nEnter The Value Of Y ";
            cin>>y;
        }
        friend void max(sample &a,test &b);
};
void max(sample &a,test &b)
{
    if(a.x>b.y)
    {
        cout<<a.x<<" is Greater Than "<<b.y;
    }
    else
    {
        cout<<b.y<<" is Greater Than "<<a.x;
    }
}
main()
{
    sample c;
    test d;
    max(c,d);
}

Inline Function

Program To Illustrate The Concept Of Inline Function:
#include<iostream>
using namespace std;
class sample
{
    int x;
    public:
        void getdata()
        {
            cout<<"Enter Value Of x ";
            cin>>x;
        }
        void display();
        void increment();
       
};
inline void sample::display()
{
    cout<<"\nValue is\n"<<x;
}
inline void sample::increment()
{
    x++;
    cout<<"\nAfter Incrementing\n";
}
main()
{
    sample s;
    s.getdata();
    s.display();
    s.increment();
    s.display();
}