You are on page 1of 16

EXPERIMENT​–​ 1

AIM:​ To understand the concept of classes and objects.


PROGRAM:​ Define a class garments in c++ with following description:-

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

AIM:​ ​To understand the concept of constructors and destructor.


PROGRAM:
Private:
● Code, type, fabric,=[string].
● Size = [int]
● Price = [float]
Function initprice () which calculate and defines the value as follows
For value of fabric “DENIM”
Type–price
TROUSER–1500
JACKET–2500
For fabric other than “DENIM” –offer price gets reduced by 25%
Public:
● A constructor to assign values of code,type,fabric,with “NOT INTIALISED” and size and price
to 0
● Function input () to input values of code,type,size,fabric,and invoke the initprice () function
● Function display () which displays the content and all the data members for an outfit.

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

AIM:​ ​To understand the concept of function overloading.


PROGRAM:​ Write a program in c++ to implement function overloading in class volume of cube,
cuboid and cylinder using function ​calculate_vol ().

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;

cout<<"\n Enter the radius and height of the cylinder :";


cin>>r>>h;
cout<<"\n Enter the side of the cube :";
cin>>a;
cout<<"\n Enter the radius of the sphere :";
cin>>r1;

cout<<"\n Volume of the cylinder is :"<<vol(r,h);


cout<<"\n Volume of the cube is :"<<vol(a);
cout<<"\n Volume of the sphere is :"<<vol(r1);
getch();

return 0;

float vol(int r,int h)


{
return(3.14*r*r*h);
}
float vol(float r1)
{
return((4*3.14*r1*r1*r1)/3);
}
int vol(int a)
{

5
return(a*a*a); }
​EXPERIMENT –4

AIM:​ ​To implement the type of inheritance.


PROGRAM:​ Write a program to implement single inheritance by declaring the class employee.
● Employee
Protected – Int emp_id, Char emp_name
● Inherit class employee privately in derived class
● Derived
​ loat height, weight
Private –​ F
Public– Function getdata (), printdata ()
​ ODE:
C
#include<iostream.h>
#include<conio.h>

class employee
{
protected:
int emp_id;
char emp_name[20];
};

class derived:private employee


{
private:
float height,weight;

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;
}
};

class area:public triangle,public rectangle


{
private:
float tri_area,rec_area;
public:
void calculate_area()
{
get_rec_data();
get_tri_data();
tri_area=0.5*base*height;
rec_area=length*breadth;

cout<<"\n The area of the triangle is: "<<tri_area;


cout<<"\n The area of the rectangle is: "<<rec_area;
}
};

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 2: if(n >= 0)


{
cout << "enter the element : " ;
cin >> item ;
f = del(a,item,n);
if(f == -1)
{
cout<<"sorry element not found. Make sure the element is within the list shown below.\n";
}
else
{
n--;
}
disp(a,n);
}
else
{
cout << "The programme is UNDERFLOWN\n";
}
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 ;

}while(ch == 'y'|| ch == 'Y');

getch();
}

int insert(int a[],int item,int n)


{
int i,j;
if(item >= a[n-1])
{
a[n] = item;
10
return 0;
}

for(i=0;item >= a[i];i++)


{
for(j=n;j>i;j--)
{
a[j]=a[j-1];
}

a[i]=item;
}
}

int del(int a[],int item,int n)


{
int found = 0,i,j;
if( item == a[n])
{
return 0;
}
for(i=0;i<=n;i++)
{
if(a[i] == item)
{
found = 1;
break;
}
}
if(found == 1)
{
for(j=i;j<n;j++)
{
a[j] = a[j+1];
}
return 0;
}
else
{
return -1;
}
}
void disp(int a[],int n)
{
cout << "\n";
11
for(int i=0;i<=n;i++)
{
cout << " " << a[i] ;
}

cout << "\n";


}

void sort(int a[],int n)


{
int i,j,temp;
cout << " N IS " << n ;
for(i=0;i <= n ;i++)
{
for(j=i;j < n+1;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

​ EXPERIMENT – 7

AIM​: ​Searching in an array


PROGRAM​:​ Write a c++ program to search element in array using binary search and linear search
(menu driven).
CODE​:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>

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

AIM​: ​To implement sorting in an array


PROGRAM​:​ Write a c++ program to sort array using bubble sort and selection sort.
CODE​:

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>

void display(int a[],int n);


void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);

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;
}

void display(int arr[],int n)


{
for(int i=0;i<n;i++)
{
printf(" %d ",arr[i]);
15
}

void bubble_sort(int arr[],int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("After bubble sort , the elements are : ");
display(arr,n);
}

void selection_sort(int arr[],int n)


{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

}
printf("After selection, the sorted elements are : ");
display(arr,n);
}

16

You might also like