Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PROGRAMMING IN C++

3330702
[PRACTICAL-6]
•Explain basic concepts of object-oriented programming.

Enrollment Number: 206250307018


Name of the student: AAFIKHAN MALEK.
Your practical exercise page.
1. Define a class to represent a shape which includes the data
members(length,bredth,height,radius) and Member
functions(area_square() to calculate the area of square,
area_rectangle() to calculate the area of rectangle,
area_circle() to calculate the area of circle, area_cylinder() to
calculate the area of cylinder.)write a main program to test the
program.

#include<iostream>
#define PI 3.14
using namespace std;
class shape
{
double length,breadth,height,radius;
public:
double area_square(double side)
{
return side*side;
}
double area_rectangle(double length,double
breadth)
{
return length*breadth;
}
double area_circle(double r)
{
return PI*r*r;
}
double area_cylinder(double r,double h)
{
double ans=2*PI*r*h;
}
};
int main()
{
shape s1;
cout<<"the area of square is:"<<s1.area_square(5)<<endl;
cout<<"the area of rectangle
is:"<<s1.area_rectangle(10,10)<<endl;
cout<<"the area of circle is:"<<s1.area_circle(5)<<endl;
cout<<"the area of circle is:"<<s1.area_cylinder(3,4);
return 0;
}
Output -

2. Write a program to create a shopping list of items for which we


place an order with a dealer every month. The list includes
details such as the code number and price of each item. We
would like to perform operations such as adding an item to the
list, deleting an item from the list and printing the total value of
the order.

#include<iostream>
#include<cstdlib>
#define N 50
using namespace std;
class Item
{
int codeNumber[N];
double itemPrice[N];
int count;
public:
void setcount()
{
count=0;
}
void addItem()
{
cout<<"Enter Item Code: ";
cin>>codeNumber[count];
cout<<"Enter Item Price: ";
cin>>itemPrice[count];
count++;
}
void displayItems()
{
if(count==0)
{
cout<<"List is empty"<<endl;
}
else
{
cout<<"\nCode Price\n";
for(int i=0;i<count;i++)
{
if(itemPrice[i]==0.0)
{
continue;
}
cout<<codeNumber[i]<<" "<<itemPrice[i]<<endl;
}
}
}
void totalBill()
{
double sum=0;
for(int i=0;i<count;i++)
{
sum=sum+itemPrice[i];
}
cout<<"Total Bill: "<<sum<<endl;
}
void removeItem()
{
int code;
cout<<"Enter Item Code: ";
cin>>code;
for(int i=0;i<count;i++)
{
if(codeNumber[i]==code)
{
itemPrice[i]=0.0;
}
}
}
};
int main()
{
Item order;
int choice;
char ans='y';
order.setcount();
while(ans=='y')
{
cout<<"Shopping List..."<<endl;
cout<<"Add item : enter 1"<<endl;
cout<<"Remove item : enter 2"<<endl;
cout<<"Total Amount : enter 3"<<endl;
cout<<"Display Items: enter 4"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>choice;
switch(choice)
{
case 1:order.addItem();
break;
case 2:order.removeItem();
break;
case 3:order.totalBill();
break;
case 4:order.displayItems();
break;
default:
cout<<"Invalid choice"<<endl;
}
cout<<"Do you want to continue: enter y for YES or n for
NO"<<endl;
cin>>ans;
system("cls");
}
return 0;
}
Output -

You might also like