Professional Documents
Culture Documents
Programming in C++ 3330702 (Practical-6) : - Explain Basic Concepts of Object-Oriented Programming
Programming in C++ 3330702 (Practical-6) : - Explain Basic Concepts of Object-Oriented Programming
3330702
[PRACTICAL-6]
•Explain basic concepts of object-oriented programming.
#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 -
#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 -