Professional Documents
Culture Documents
Private members:-
Code – string
Type– string
Size– integer
Fabric– string
Price – float
A function assign () which calculate the value of price as follows. For the value of fabric “COTTON”:-
Type – PRICE
TROUSER– 1300
SHIRT– 1100
For fabric other than “COTTON”, the above mentioned price gets reduced by 10%.
Public members:-
A function input () to get the values of the data members code,type,size and fabric and invoke the assign
function.
A function display () which displays the content of all the data members for a garment.
CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class garments
{
char code[20],type[20],fabric[20];
int size;
float price;
void assign()
{
if(strcmp(fabric,”COTTON”)==0)
{
if(strcmp(fabric,"COTTON")==0)
price=1300;
else if(strcmp(type,"TROUSER")==0)
price =1100;
}
else
{
1
if(strcmp(type,"TROUSER")==0)
price=1300-(0.10*1300);
else if(strcmp(type,"SHIRT")==0)
price=1100-(0.10*1100);
}
}
public:
void input()
{
cout<<"\n Enter the code of the garment :";
gets(code);
cout<<"\n Enter the type of the garment (TROUSER/SHIRT) :";
gets(type);
cout<<"\n Enter the size of the fabric :";
cin>>size;
cout<<"\n Enter the type of the outfit :";
gets(fabric);
assign();
}
void display()
{
cout<<"\n The code of the Garment is :";puts(code);
cout<<"\n The type of the Garment is :";puts(type);
cout<<"\n The size of the Garment is :"<<size;
cout<<"\n The fabric of the Garment :";puts(fabric);
cout<<"\n The price of the fabric is :"<<price;
}
};
void main()
{
clrscr();
garment g;
g.input;
g.display;
getch();
}
2
EXPERIMENT – 2
CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class outfit
{
private:
char code[20],type[20],fabric[20];
int size;
float price;
void initprice()
{
if((strcmp(fabric,"DENIM"))==0||(strcmp(fabric,"denim")==0))
{
if(strcmp(fabric,"TROUSER")==0)
price=1500;
else if(strcmp(type,"JACKET")==0)
price=2500;
}
else
{
if(strcmp(type,"TROUSER")==0)
3
price=1500*0.25;
else if(strcmp(type,"JACKET")==0)
price=2500*0.25;
}
}
public:
outfit()
{
strcpy(code,"NOT INTIALISED");
strcpy(code,"NOT INTIALISED");
strcpy(fabric,"NOT INTIALSED");
size=price=0;
}
void input()
{
cout<<"\n Enter the code of the outfit :";
cin>>code;
cout<<"\n Enter the fabric of the outfit:";
cin>>fabric;
cout<<"\n Enter the size of the outfit :";
cin>>size;
cout<<"\n Enter the type of the outfit :";
cin>>type;
initprice();
}
void display()
{
cout<<"\n The code of the outfit is :"<<code;
cout<<"\n The type of the outfit is :"<<type;
cout<<"\n The fabric of the outfit is :"<<fabric;
cout<<"\n The price of the outfit is :"<<price;
cout<<"\n The size of the outfit is :"<<size;
}
};
void main()
{
clrscr();
outfit ou;
ou.input();
ou.display();
getch();
}
4
EXPERIMENT – 3
CODE:
#include<iostream.h>
#include<conio.h>
float vol(int,int);
float vol(float);
int vol(int);
int main()
{
clrscr();
int r,h,a;
float r1;
return 0;
5
return(a*a*a); }
EXPERIMENT –4
class employee
{
protected:
int emp_id;
char emp_name[20];
};
public:
void getdata()
{
cout<<"\n Enter the employee id :";
cin>>emp_id;
cout<<"\n Enter the employee's name :";
cin>>emp_name;
cout<<"\n Enter the height of the employee :";
cin>>height;
cout<<"\n Enter the weight of the employee :";
cin>>weight;
}
void printdata()
{
cout<<"\n Employee ID :"<<emp_id;
cout<<"\n Employee Name :"<<emp_name;
cout<<"\n Employee Height :"<<height<<"cm";
cout<<"\n Employee Weight :"<<weight<<"Kg";
}
};
6
void main()
{
clrscr();
derived ou;
ou.getdata();
ou.printdata();
getch();
}
EXPERIMENT-5
AIM: To understand the concept of multiple inheritances.
PROGRAM: Define a class triangle and a class rectangle with the following specifications
Class Triangle class rectangle
Protected:
Height, base=float length, breadth = float
Public:
Function get_tri_data() function get_rec_data()
Also define a class area which will derive class triangle and class rectangle publicaliy
CODE:
#include<iostream.h>
#include<conio.h>
class triangle
{
protected:
float height,base;
public:
void get_tri_data()
{
cout<<"\n Enter the height of triangle: ";
cin>>height;
cout<<"\n Enter the base lenght of the triangle: ";
cin>>base;
}
};
class rectangle
{
7
protected:
float length,breadth;
public:
void get_rec_data()
{
cout<<"\n Enter the length of the rectangle: ";
cin>>length;
cout<<"\n Enter the breadth of the rectangle: ";
cin>>breadth;
}
};
void main()
{
clrscr();
area ol;
ol.calculate_area();
getch();
}
EXPERIMENT-6
AIM: To implement Insertion and deletion in an array
PROGRAM: Write a c++ program to implement insertion and deletion in an array (menu driven)
CODE:
8
#include <iostream>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
int insert(int [],int,int);
int del(int [],int,int);
void sort(int [],int);
void disp(int [],int);
int a[20],x,i,j,n=-1,lim,item,f=0,choice;
char ch;
cout << "\nEnter the limit : ";
cin >> lim;
for(i=0;i<lim;i++)
{
n++;
cout << "\nEnter the element " << i+1 <<" : ";
cin >> a[i];
}
sort(a,n);
disp(a,n);
do
{
cout << "1.INSERT \n2.DELETE \n3.EXIT \nNOTE: MAXIMUM LIMIT - 20
ELEMENTS.\nEnter your choice :";
cin >> choice ;
switch(choice)
{
case 1: if(n<20)
{
cout << "Enter the element : " ;
cin >> item ;
n += 1;
insert(a,item,n);
disp(a,n);
}
else
{
cout << "The programme OVERFLOWN\n";
}
9
break;
case 3:
exit(0);
default:
cout << "Invalid Input , Make sure you entered the right Choice\n";
continue;
}
cout << "\nDo you want to continue : if yes press \'y\' : " ;
cin >> ch ;
getch();
}
a[i]=item;
}
}
EXPERIMENT – 7
12
void main()
{
clrscr();
while(1)
{
int a[25],beg,item,last,n,num,i,ch,mid,f=0;
printf("\nMENU\n");
printf("\n 1.Linear Search");
printf("\n 2.Binary Search");
printf("\n 3.Exit");
printf("\n Enter the choice: ");
scanf("%d", &ch);
if(ch==1)
{
printf("\n Enter the number of elements in the array: ");
scanf("%d",&n);
printf("\n Enter the sorted array:\n ");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("\n Enter the item to be searched for :");
scanf("%d", &item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\n Item found at position:d%d", i+1);
break;
}
}
if(i==n)
printf("\nItem not found");
}
if(ch==2)
{
printf("\nEnter the number of elements in the array:\n");
scanf("%d",&n);
printf("Enter the sorted array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Item to be searched:\n");
scanf("%d", &item);
last=n-1;
13
mid=(beg+last)/2;
while(beg<=last)
{
if(item==a[mid])
{
printf("\n item found at position: %d", mid+1);
break;
}
else if(a[mid]>item)
last=mid-1;
else beg=mid+1;
mid=(beg+last)/2;
}
}
if(ch==3)
{
exit(0);
}
getch();
}
}
EXPERIMENT – 8
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
clrscr();
14
int n,choice,i;
char ch[20];
printf("Enter no. of elements you want to sort : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&arr[i]);
}
printf(" Select any option Given Below for Sorting : \n");
while(1)
{
printf("\n1. Bubble Sort\n2. Selection Sort\n3. Display Array.\n4. Exit the Program.\n");
printf("\nEnter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
case 3:
display(arr,n);
break;
case 4:
return 0;
default:
printf("\nPlease Select only 1-4 option ----\n");
}
}
getch();
return 0;
}
}
printf("After selection, the sorted elements are : ");
display(arr,n);
}
16