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

OOP

LA-1
ASSIGNMENT

NAME:K.Viddya

USN:1NT18CS065
BRANCH:C
Mr.Alex has Created two class namely C1 and C2, both the class has one private variable A and B
respectively which are initialized using constructor,there is one function called "ADD" which does
not belongs to any of these class, which takes two objects as parameter and does the addition of
two variables A and B and returns the result. But he his getting an error , identify the error and help
him to write a program to implement the same. make use of friend function*/

#include<iostream>
using namespace std;
class c2;
class c1
{
int a;
public:
c1(int i)
{
a=i;
}
friend int add(c1,c2);
};
class c2
{
int b;
public:
c2(int j)
{
b=j;
}
friend int add(c1,c2);
};
int add(c1 p,c2 q)
{
int sum;
sum=p.a+q.b;
return sum;
}
int main()
{
c1 p(5);
c2 q(10);
cout<<"the sum is "<<add(p,q);
return 0;

}
/* A grocessory shop owner wants to get the count of number of customers enter to his shop each
day and the frequency of each customer visit. Assume that each customer is given with a ID and
Name, and also Cash Card which they use for shopping in the shop. Write a program to implement
this requirement of the shop keeper. *make use of static variable*/

#include<iostream>
using namespace std;
class cust
{
public: char name[10];
int id,ccard;
static int count;
int entry(int id,int ccard,char name[10]);
int freq(int id);
void display();
}c[10],m;
int cust::count;
void cust::display()
{
cout<<"total number of customers="<<m.count<<'\n';
}
int cust::entry(int i,int cc,char n[10])
{
m.count++;
return 0;
}
int cust::freq(int cid)
{
int i,f=0;
for(i=0;i<10;i++)
if(cid==c[i].id)
f++;
cout<<f;
}
int main()
{
char ch,x;
int i;
for(i=0;i<10;i++)
{
cout<<"enter id,cc number and name"<<'\n';
cin>>c[i].id>>c[i].ccard>>c[i].name;
m.entry(c[i].id,c[i].ccard,c[i].name);
cout<<"press 'y' to continue 'n' to terminate";
Cin>>ch;
if(ch=='n')
break; }
m.display();
while(1)
{
cout<<"enter the customer id to known their frequency of visit";
cin>>m.id;
m.freq(m.id);
cout<<"continue?(y/n)";
cin>>x;
if(x=='n')
break;
}
return 0;
}
/*Write a program to implement the student record having student Name, USN, Marks of 5
subjects,and calculate the average of marks to find the grade (A Grade 100-95, B Grade 85-94, C
Grade 75-84, D Grade <75) and display the topper of the class based on grades he/she has achieved,
consider class strength as 10. /*make use of array of objects*/
#include<iostream>
using namespace std;
class student
{ public:char name[30], usn[10];
float marks[5];
float sum,avg, topper;
};
int main()
{
student stu[10];
float topper;
int n;
cout<<"enter the number of students";
cin>>n;
cout<<"enter the details of the student\n";
for(int i=0;i<n;i++)
{
cout<<"enter the name";
cin>>(stu[i].name);
cout<<"enter the usn";
cin>>(stu[i].usn);
cout<<"enter the marks in 5 subject";
for(int j=0;j<5;j++)
cin>>stu[i].marks[j];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
stu[i].sum+=stu[i].marks[j];
}
for(int i=0;i<n;i++)
{
stu[i].avg=(stu[i].sum)/n;
if((stu[i].avg>=95)&&(stu[i].avg<=100))
cout<<stu[i].name<<"got a grade\n";
else if((stu[i].avg>=85)&&(stu[i].avg<95))
cout<<stu[i].name<<"got b grade\n";
else if((stu[i].avg>=75)&&(stu[i].avg<85))
cout<<stu[i].name<<"got c grade\n";
else if((stu[i].avg>=65)&&(stu[i].avg<75))
cout<<stu[i].name<<"got d grade\n";
else
cout<<stu[i].name<<"is fail\n";
}
topper=stu[0].sum;
for(int i=0;i<n;i++)
{
if(stu[i+1].sum>topper)
topper=stu[i+1].sum;
}
for(int i=0;i<n;i++)
{
if(topper==stu[i].sum)
{ cout<<"the topper of the class is
"<<stu[i].name<<" with "<<stu[i].sum; } }

return 0;
}
Write a program to implement the movie ticket booking application with the following condition :-
One person should be able to book at least one ticket and a max of 5 tickets,
- Should be able to hold the ticket while booking for a period of 10min
- Assume the seating capacity to be 30. /* make use of default arguments*

#include<iostream>
using namespace std;
int i;
#define max 30
class mov
{
public: char name[20];
int n;
int choice;
int check(char [],int,int,int);
};

int mov::check(char name1[],int n1,int b,int a=10)


{
if(a<10)
cout<<"ticket booked for movie"<<b<<"\nname:"<<name1<<"\nnumber of
tickets:"<<n1<<endl;
else
{
cout<<"ticket not booked\n";
return 1;}
}
int main()
{
mov obj[max];
int r;
int z=1;
int i=0;
while(z!=0)
{
cout<<"how many tickets? min 1 and max 5\n";
cin>>obj[i].n;
cout<<"enter name\n";
cin>>obj[i].name;
cout<<"enter your choice\n"<<"1.6 feet apart\n2.me before you\n3.edge of
tomorrow\n4.mission mangal\n5.kabir singh\n";
cin>>obj[i].choice;
cout<<"do u want to enter more customers?";
obj[i].check(obj[i].name,obj[i].n,obj[i].choice,i+7);
i++;
cout<<"enter 1 for yes and 0 for no";
cin>>z}}
Define a class “COMPLEX” having data members “real” and “imaginary”. Define suitable constructors
and member function to display, add two complex numbers, subtract two complex numbers. Make
subtract function as friend.*/

#include<iostream>
using namespace std;
class complex
{ public:
float real;
float img;
complex()
{}
complex(float a, float b)
{
real=a;
img=b;
}
complex add(complex x1, complex x2)
{

complex res;
res.real=x1.real+x2.real;
res.img=x1.img+x2.img;
return res;
}
void display(complex y1)
{
cout<<"the answer after calculating
is"<<y1.real<<"is the real part"<<y1.img<<"is the imaginary part";
}
friend complex subtract(complex,complex);
};
complex subtract(complex w1, complex w2)
{
complex res1;
res1.real=w1.real-w2.real;
res1.img=w1.img-w2.img;
return res1;
}
int main()
{
complex a,b,result,m;
int n;
cout<<"enter the real and imaginary part of the first
number";
cin>>a.real>>a.img;
cout<<"enter the real and imaginary part of the second
number";
cin>>b.real>>b.img;
cout<<"enter 1 for subtraction and 2 for addition\n";
cin>>n;
switch(n)
{
case 1:
result=subtract(a,b);
m.display(result);
break;
case 2:
result=m.add(a,b);
m.display(result);
break;
default:
cout<<"wrong choice";
}
return 0;

}
Create two Time classes: Time24 (ex: 12:01:00) and Time12(12:01 p.m) with appropriate data
members and member functions Write a program to read Time24 object and convert it to Time12
object using constructor in Time12 class.

#include<iostream>
using namespace std;
class time1;
class time2
{
public:
int h2,m2,s2;
time2(int a,int b,int c)
{
h2=a;
m2=b;
s2=c;
}
void display()
{
cout<<h2<<":";
cout<<m2<<":";
cout<<s2<<" \n";
}
friend class time1;
};
class time1
{
public:
int h1,m1,s1;
time1(time2);
};
time1::time1(time2 p)
{
if(p.h2<13)
cout<<p.h2;
else
cout<<p.h2-12;
cout<<":";
cout<<p.m2<<endl;
}
int main()
{
int h,m,s;
cout<<"enter the time with respect to 24 hour";
cin>>h>>m>>s;
time2 obj(h,m,s);
cout<<"the entered time is \n";
obj.display();
cout<<"the time in 12 hour is";
time1 blue(obj);
return 0;
}
/*Write a program to find the area of a given geometrical shape(consider circle, triangle, cube,
rectangle and hexagon). Name of the function used to find the are should be "AREA()", use the
concept of funciton overloading to implement this. make use of function overloading*/
#include<iostream>
using namespace std;
class shapes
{
public:
float area(float a)
{
float p=0;
p=3.14*a*a;
return p;
}
int area(int c,int b)
{
int p=0;
p=0.5*b*c;
return p;
}
int area(char d,int e,int f)
{
int p=0;
p=e*f;
return p;
}
int area(int g)
{
int p=0;
p=(g*g);
return p;
}
int area(char h,int i)
{
float p=0;
p=6*1.73*i*i;
return p;
}
};
int main()
{
shapes s;
float r,res1=0.0;
int a,p,q,res2=0,z;
char m='a';
cout<<"enter 1 for for circle \n enter 2 for for triangle \n";
cout<<"enter 4 for for cube\n enter 3 for for rectangle \n";
cout<<"enter 5 for for hexagon \n";
cin>>a;
switch(a)
{
case 1:
cout<<"you have selected circle\n";
cout<<"enter the radius";
cin>>r;
res1=s.area(r);
cout<<"the area of the circle is"<<res1;
break;
case 2:
cout<<"you have selected triangle\n";
cout<<"enter the base and height";
cin>>p>>q;
res2=s.area(p,q);
cout<<"the area of the triangle is"<<res2;
break;
case 3:
cout<<"you have selected rectangle\n";
cout<<"enter the length and breadth";
cin>>p>>q;
res2=s.area(m,p,q);
cout<<"the area of the rectangle is"<<res2;
break;
case 4:
cout<<"you have selected cube \n";
cout<<"enter the length";
cin>>z;
res2=s.area(z);
cout<<"the area of the cube is"<<res2;
break;
case 5:
cout<<"you have selected hexagon\n";
cout<<"enter the length of one side";
cin>>r;
res2=s.area(m,r);
cout<<"the area of the hexagon is"<<res2;
break;
default:
cout<<"entered number is wrong";
}
return 0;
}
Fill in the blanks
#include<iostream>
using namespace std;
int main()
{
int x ,y;
cin>>x>>y;
if ( !(x ^ y) )
printf(" both the numbers are equal ");
else
printf(" both the numbersare not equal ");
return 0;
}

include<iostream>

#include<math.h>
using namespace std;
int main ()
{
int a ,b;
cin>>a>>b;
printf("the second number entered is= %d\n", ((a + b) + abs(a - b)) / 2);
printf("the first number entered= %d", ((a + b) - abs(a - b)) / 2);
return 0;
}
Consider the following program and complete the function header of sum() such that it matches the
given sample input and output.

#include <iostream>
using namespace std;
int sum(int a, int b=10, int c=100)
{
return a + b + c;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << sum(a) << ", " << sum(a, b) << ", " << sum(a, b, c) << endl;
return 0;
}

You might also like