CO2023
CO2023
1 Program 1 16.08.2023
2 Program 2 16.08.2023
3 Program 3 16.08.2023
4 Program 4 23.08.2023
5 Program 5 23.08.2023
6 Program 6 06.09.2023
7 Program 7 06.09.2023
8 Program 8 11.10.2023
9 Program 9 11.10.2023
10 Program 10 11.10.2023
11 Program 11 25.10.2023
12 Program 12 25.10.2023
Sign of faculty:-
___________________
Q1) Write a C++ program to print your personal details name, surname (single
character), total marks, gender(M/F), result(P/F) by taking input from user
CODE:
#include<iostream>
#include<string>
using namespace std;
int main(){
int marks;
char surname,gender,result;
string name;
cout<<"enter name: ";
cin>>name;
cout<<"enter surname: ";
cin>>surname;
cout<<"enter marks: ";
cin>>marks;
if(marks>=35){
result='P';
}
else{
result='F';
}
cout<<"enter gender(M/F): ";
cin>>gender;
if(gender != 'M' && gender != 'F' ){
cout<<"enter valid gender (M/F)";
return 0;
}
cout<<"The name is "<<name<<" "<<surname<<", marks are: "<<marks<<", gender: "<<gender<<" and the result is "<<result;
}
OUTPUT:
Q2) create a class ‘EMPLOYEE’ that has a ‘EMPNUMBER’ and ‘EMPNAME’ as
data members and member functions getdata() to input data display() to output
data. Write a main function to create an array “EMPLOYEE” objects. Accept and
print the details of at least 6 employees.
CODE:
#include<iostream>
#include<string>
using namespace std;
class Employee{
public:
int Empnumber;
string Empname;
void getdata(){
cout<<"enter employee number and name: ";
cin>>Empnumber;
cin>>Empname;
}
void display(){
cout<<"employee number is "<<Empnumber<<" and employee name is "<<Empname<<"\n";
}
};
int main(){
string name;
int num;
Employee arr[6];
for(int i=0;i<6;i++){
arr[i].getdata();
}
for(int i=0;i<6;i++){
arr[i].display();
}
}
OUTPUT:
Q3) Write a program to swap two numbers by both call by value and call by
reference mechanism, using two functions swao_value() and swap_reference()
respectively. By getting the choice from the user and executing the user’s choice
by switch function.
CODE:
#include<iostream>
#include<string>
using namespace std;
void swap_value(int a,int b){
int c=a;
a=b;
b=c;
}
void swap_reference(int * a,int * b){
int *c;
*c=*a;
*a=*b;
*b=*c;
}
int main(){
int num1,num2;
int choice;
cout<<"enter num1: ";
cin>>num1;
cout<<"enter num2: ";
cin>>num2;
cout<<"use which method to swap the numbers?(for value: 1/for reference: 2): ";
cin>>choice;
switch(choice){
case 1:
swap_value(num1,num2);
break;
case 2:
swap_reference(&num1,&num2);
break;
}
cout<<"after swapping: num1="<<num1<<", num2="<<num2;
return 0;
}
OUTPUT:
a) by value
b) by reference
Q4) Write a program to accept 5 different numbers by creating a class called
friendfunc1 and friendfunc2 taking 2 and 3 arguments respectively and calculate
the average of these numbers by passing objects of the class to the friend func.
CODE:
#include<bits/stdc++.h>
using namespace std;
class friendfunc1;
class friendfunc2{
public:
int n4,n5;
int avgnums(friendfunc1 ,friendfunc2 );
};
class friendfunc1{
int n1,n2,n3;
public:
friendfunc1(){
n1=5;
n2=8;
n3=10;
}
friend int friendfunc2::avgnums(friendfunc1,friendfunc2);
};
int friendfunc2::avgnums(friendfunc1 f1,friendfunc2 f2){
return ((f1.n1+f1.n2+f1.n3+f2.n4+f2.n5)/5);
}
int main(){
friendfunc1 f1;
friendfunc2 f2;
f2.n4=6;
f2.n5=7;
int ans=f2.avgnums(f1,f2);
cout<<ans;
return 0;
}
OUPUT:
Q5) Write a C++ program to perform different arithmetic operations such as
addition, subtraction, division, modulus, and multiplication using inline function
CODE:
#include<iostream>
using namespace std;
int main(){
int n1,n2;
cin>>n1;
cin>>n2;
OUTPUT:
Q6) WAP to perform string operation using operator overloading in C++ i.=String
Copy ii. ==, Equality iii. +Concatenation.
CODE:
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
class stringop{
public:
char s[50];
stringop() {}
stringop(char str[]){
strcpy(s,str);
}
stringop operator+(stringop& s2){
return strcat(this->s,s2.s);
}
void operator=(stringop s2){
strcpy(this->s,s2.s);
}
bool operator==(stringop s2){
if(strcmp(this->s,s2.s)){
return false;
}
return true;
}
};
int main(){
char s1[]="hello";
char s2[]="world";
stringop sop1(s1);
stringop sop2(s2);
stringop sop3;
sop3=sop1+sop2; //concatenation and assignment operators
cout<<sop3.s<<endl;
if(sop1==sop2){ //equality operator
cout<<"the strings are equal"<<endl;
}
else{
cout<<"the strings are unequal"<<endl;
}
return 0;
}
OUTPUT:
Q7) Write a C++ program to explain virtual function(polymorphism) by creating a
base class c_polygon which has virtual function area().Two classes c_rectangle
and c_triangle derived from c_polygon and they have area() to calculate the area
of rectangle and triangle respectively.
CODE:
#include<bits/stdc++.h>
using namespace std;
class c_polygon{
public:
virtual int area(){
}
};
int area(){
return (h*b)/2;
}
};
int main(){
c_polygon * poly1;
c_polygon * poly2;
c_rectangle rect(5,4);
c_triangle tri(3,6);
poly1=▭
poly2=&tri;
cout<<poly1->area()<<endl;
cout<<poly2->area()<<endl;
return 0;
}
OUTPUT:
Q8)WAP to accept values from users ,find sum, products differences ,division of
two numbers (a) using 3-5 inline functions (b) using reference variables , (c)
using macros
CODE:
#include<iostream
#define mac_add(a,b) a+b
#define mac_subtract(a,b) a-b
#define mac_product(a,b) a*b
#define mac_divide(a,b) a/b
using namespace std;
inline int in_add(int a,int b){return a+b;}
inline int in_subtract(int a,int b){return a-b;}
inline int in_product(int a,int b){return a*b;}
inline int in_divide(int a,int b){return a/b;}
int ref_add(int & a,int & b){
return a+b;
}
int ref_difference(int & a,int & b){
return a-b;
}
int ref_product(int & a,int & b){
return a*b;
}
int ref_divide(int & a,int & b){
return a/b;
}
int main(){
int n1,n2;
cout<<"enter the two numbers: ";
cin>>n1;
cin>>n2;
cout<<"using inline functions: "<<endl;
cout<<"the sum of "<<n1<<" and "<<n2<<" is "<<in_add(n1,n2)<<endl;
cout<<"the difference of "<<n1<<" and "<<n2<<" is "<<in_subtract(n1,n2)<<endl;
cout<<"the product of "<<n1<<" and "<<n2<<" is "<<in_product(n1,n2)<<endl;
cout<<"the quotient of "<<n1<<" and "<<n2<<" is "<<in_divide(n1,n2)<<endl<<endl;
cout<<"using macros: "<<endl;
cout<<"the sum of "<<n1<<" and "<<n2<<" is "<<mac_add(n1,n2)<<endl;
cout<<"the difference of "<<n1<<" and "<<n2<<" is "<<mac_subtract(n1,n2)<<endl;
cout<<"the product of "<<n1<<" and "<<n2<<" is "<<mac_product(n1,n2)<<endl;
cout<<"the quotient of "<<n1<<" and "<<n2<<" is "<<mac_divide(n1,n2)<<endl<<endl;
cout<<"using reference variable: "<<endl;
cout<<"the sum of "<<n1<<" and "<<n2<<" is "<<ref_add(n1,n2)<<endl;
cout<<"the difference of "<<n1<<" and "<<n2<<" is "<<ref_difference(n1,n2)<<endl;
cout<<"the product of "<<n1<<" and "<<n2<<" is "<<ref_product(n1,n2)<<endl;
cout<<"the quotient of "<<n1<<" and "<<n2<<" is "<<ref_divide(n1,n2)<<endl;
return 0;
}
OUPUT:
Q9)Write a program in C++ using static variable to get the sum of the salary of 10
employees
CODE:
#include<iostream>
OUPUT:
class complexno{
int real;
int img;
public:
complexno(int r,int i){
real=r;
img=i;
}
void displaycom(){
cout<<"("<<real<<") + i("<<img<<")"<<endl;
}
friend complexno c_sum(complexno,complexno);
};
int main(){
complexno z1(5,6);
complexno z2(2,-1);
z1.displaycom();
z2.displaycom();
complexno z3=c_sum(z1,z2);
cout<<"sum of these complex numbers: ";
z3.displaycom();
return 0;
}
OUPUT:
Q11) Write a program to overload ! (not) operator (unary) all data members of a
class.
CODE:
#include<iostream>
using namespace std;
class a{
int num;
public:
a(){
num=5;
}
a(int num){
this->num=num;
}
bool operator!(){
if(num!=0){
return false;
}
return true;
}
};
int main(){
a obj1(0);
a obj2(5);
cout<<!obj1<<endl;
cout<<!obj2<<endl;
return 0;
}
OUTPUT:
Q12) Use template function to sort an array call function with 5 different
combinations including pointer, float, int etc.
CODE:
#include<bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
int a[5] = { 10, 50, 30, 40, 20 };
int n = sizeof(a) / sizeof(a[0]);
OUTPUT: