Example : Queue using Single
Inheritance.
#include <iostream.h>
#include<stdio.h>
#include <process.h>
#define MAX 20
class
Queue
{
protected:
int front,rear;
int
arr[MAX];
int cap;
int item;
public:
Queue(int
x)
{
cap=x;
front=-1;
rear=-1;
}
void
push()
{
if(rear==-1
&& front==-1)
{
front=rear=0;
}
cout<<"\n\tEnter
The element: ";
cin>>item;
arr[rear++]=item;
}
void
pop()
{
cout<<"\n\tPopped
element is :"<<arr[front];
front++;
}
void
display()
{
int
i=front;
cout<<"\n\tElements
in the Queue : ";
while(i<rear)
{
cout<<"
"<<arr[i];
i++;
}
}
};
class
My_Queue: public Queue
{
public:
My_Queue(int
x):Queue(x)
{
}
int
push_check()
{
if(rear>=cap)
{
cout<<"\n\tQueue
in Overflow Condition!\n";
return
0;
}
else
{
return
1;
}
}
int
pop_check()
{
if(front==rear)
{
cout<<"\n\tQueue
in Underflow Condition!\n";
return
0;
}
else
return
1;
}
};
void
main()
{
int c,flag;
cout<<"\n\tEnter
Queue Size : ";
cin>>c;
My_Queue
Q(c);
c=0;
while(c!=4)
{
cout<<"\n\t1.Push";
cout<<"\n\t2.Pop";
cout<<"\n\t3.Display";
cout<<"\n\t4.Exit";
cout<<"\n\tEnter
a Choice : ";
cin>>c;
switch(c)
{
case
1: flag=Q.push_check();
if(flag==1)
{
Q.push();
}
break;
case
2: flag=Q.pop_check();
if(flag==1)
{
Q.pop();
}
break;
case
3: flag=Q.pop_check();
if(flag==1)
{
Q.display();
}
break;
case
4: exit(1);
default:
cout<<"INVALID CHOICE"<<endl;
}
}
}
Example – Using Parameterize Constructor
#include <iostream.h>
class
Product
{
protected:
char
Pname[20];
int
Pcode;
public:
Product(char
N[], int P)
{
strcpy(Pname,N);
Pcode=P;
}
void
Show()
{
cout<<"\n\tProduct
Name : "<<Pname;
cout<<"\n\tProduct
code : "<<Pcode;
}
};
class
Sales:private Product
{
protected:
float
Quantity,Price;
public:
Sales(float
Q,float p,char N[],int P):Product(N,P)
{
Quantity=Q;
Price=p;
}
void
Calculate()
{
float
Sale;
Sale=Quantity*Price;
Show();
cout<<"\n\tRate
of product : "<<Price;
cout<<"\n\tQuantity : "<<Quantity;
cout<<"\n\tTotal
Sale : "<<Sale;
}
};
void main()
{
float Qt,pr;
int
pc;
char
Nm[50];
cout<<"\n\tProduct
Name? ";
cin.getline(Nm,50);
cout<<"\n\tProduct
Code? ";
cin>>pc;
cout<<"\n\tProduct
Price? ";
cin>>pr;
cout<<"\n\tQuantity
Sold? ";
cin>>Qt;
Sales
S(Qt,pr,Nm,pc);
S.Calculate();
}
Example – Using Parameterize Constructor
#include<iostream.h>
#include<string.h>
class
Employee
{
protected:
char Ename[20];
float
Basic;
public:
Employee()
{}
Employee(char
nm[],float b)
{
strcpy(Ename,nm);
Basic=b;
}
void
Display()
{
cout<<"\n\tEmployee
Name : "<<Ename;
cout<<"\n\tBasic
Salary : "<<Basic;
}
};
class
Salary:public Employee
{
private:
int
Days;
float
Sal;
public:
Salary()
{}
Salary(char
n[],float b,int d):Employee(n,b)
{
Days=d;
}
void
Cal()
{
Sal=((Basic/30)*Days);
cout<<"\n\tDays
: "<<Days;
cout<<"\n\tSalary
: "<<Sal;
}
};
void
main()
{
char
n[20];
int
d;
float
b;
cout<<"\n\tEnter
Employee Name : ";
cin.getline(n,20);
cout<<"\n\tEnter
Days Worked : ";
cin>>d;
cout<<"\n\tEnter
Basic Salary : ";
cin>>b;
Salary
S(n,b,d);
S.Display();
S.Cal();
}
No comments:
Post a Comment