C++ Assignment Answer
C++ Assignment Answer
#include<iostream>
using namespace std;
void swp(int &,int &);
void print(int&,int&);
int main()
{
int x,y;
cout<<"enter x and y"<<endl;
cin>>x>>y;
swp(x,y);
// cout<<"after swaping"<<"x-"<<x<<" "<<"y-"<<y<<endl;
print(x,y);
}
void swp(int &a,int &b)
{
int temp;
temp=a,a=b,b=temp;
}
void print(int &a ,int &b)
{
cout<<"after swaping"<<"x-"<<a<<" "<<"y-"<<b<<endl;
2.Write a C++ program to swap two integers, floats and characters using function
Overloading.
#include<iostream>
using namespace std;
void swp(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void swp(char &ch1,char &ch2)
{
char temp;
temp=ch1;
ch1=ch2;
ch2=temp;
}
void swp(float &f1,float &f2)
{
float temp;
temp=f1;
f1=f2;
f2=temp;
}
int main()
{
int x,y;
float f1,f2;
char ch1,ch2;
cout<<"float"<<endl;
cin>>f1>>f2;
swp(f1,f2);
cout<<"f1="<<f1<<"f2="<<f2<<endl;
cout<<"int"<<endl;
cin>>x>>y;
swp(x,y);
cout<<"x="<<x<<"y="<<y<<endl;
3.Write a C++ program to find the area of rectangle, triangle, and sphere using function
Overloading.
#include<iostream>
using namespace std;
int area(int &L,int &B)
{
return L*B;
}
float area(float &L,float &H)
{
float f;
f= ((L*H)/2);
return f;
}
float area(float &R)
{
return 4*3.14*R*R;
}
int main()
{
float rad;
int len,b;
float len1,h;
////////////////////area of reactangle//////////////////////
cout<<"Area of rectancle"<<endl;
cout<<"enter the lenght and breadth"<<endl;
cin>>len>>b;
cout<<"area of
reactangle:-"<<area(len,b)<<endl<<"----------------------------------------"<<endl;
/////area(len,b)//function call
//////////////area of triangle///////////////////////////////
cout<<"Area of traingle"<<endl;
cout<<"enter the lenght and height"<<endl;
cin>>len1>>h;
cout<<"area of
triangle:-"<<area(len1,h)<<endl<<"------------------------------------"<<endl; //area(len1,h)//
function
////////////////area of sphere////////////////////////
cout<<"area of sphere"<<endl;
cout<<"enter the radius"<<endl;
cin>>rad;
cout<<"area of sphere:-"<<area(rad)<<endl;///calling the area() with same and
printing the area
cout<<"-----------------------------------------------------"<<endl;
}
4.Write a C++ program to define a function with default Arguments. Whenever the function
needs default values of arguments, it will prompt the user to enter a default value. Display
the values?
#include<iostream>
using namespace std;
int input()
{
int z;
cout<<"enter the z"<<endl;
cin>>z;
cout<<"z:-"<<z<<endl;
return z;
}
int add(int x,int y,int z=input())
{
cout<<"z:- " << z << endl;
return x+y+z;
}
int main()
{
int a,b,c=0,res;
cout<<"enter a b value"<<endl;
cin>>a>>b;
if(c==0)
res=add(a,b);
else
res=add(a,b,c);
// int res=add(10,20,30);
cout<<"res:- "<<res<<endl;
}
6.Write C++ program to calculate simple interest. Define the class called interest and hide
the data elements of the class using private keyboard. Define the setdata member
function to read the data through keyboard and define the print member function outside the
class.
Formulae : interest = (P * N * R) / 100;
Total_amount = interest + P;
here, P -> Principle amount
N -> Period (No.of years)
R -> rate of interest
#include<iostream>
using namespace std;
class intrest
{
int p,n,i;
float r;
float t;
public:
void setdata()
{
cout<<"principle amount="<<endl;
cin>>p;
cout<<"n=period of yers"<<endl;
cin>>n;
cout<<"rate of intrest"<<endl;
cin>>r;
i=(p*n*r)/100;
t=i+p;
}
void print();
};
void intrest::print()
{
cout<<"print function"<<endl;
cout<<"intrest"<<i<<"total="<<t<<endl;
}
int main()
{
intrest obj1;
obj1.setdata();
obj1.print();
7.Write a C++ program to define a class called Student with data members as roll, name
and marks. Define the appropriate member functions, 1)setdata 2)display for reading and
displaying the data and also define a member function called sort to sort 5 students
records according to roll, name & marks.
#include<iostream>
#include<cstring>
using namespace std;
class student
{
private :
int roll;
string name;
float marks;
public :
void setdata();
void print();
int main()
{
student st[5];
int i;
for(i=0;i<5;i++)
{
st[i].setdata();
}
int i,j;
student temp;
switch(op)
{
case 1 : for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(st[j].roll > st[j+1].roll)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
break;
case 2 : for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(st[j].name > st[j+1].name)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
break;
case 3 : for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(st[j].marks > st[j+1].marks)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
break;
8.Write a C++ program to create a class complex with real and imaginary parts perform
addition and subtraction of two complex objects.
#include<iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
void setdata(int a,int b);
Complex add(Complex& );
Complex sub(Complex&);
void print();
};
void Complex:: setdata(int a,int b)
{
real=a,imag=b;
}
Complex Complex::add(Complex& ob2)
{
Complex temp;
temp.real=real+ob2.real;
temp.imag=imag+ob2.imag;
return temp;
}
Complex Complex::sub(Complex& ob2)
{
Complex temp;
temp.real=real-ob2.real;
temp.imag=imag-ob2.imag;
return temp;
}
obj3=obj1.add(obj2);
obj3.print();
cout<<"after subtracting"<<endl;
obj3=obj1.sub(obj2);
obj3.print();
}
9.Write a C++ Program to define 3 classes. Read & Display the data for 3 classes using
friend function.
#include<iostream>
using namespace std;
#include<cstring>
class BOI;
class SBI;
class ICICI
{
private :
int balance;
string name;
public :
void setdata(int a,string p)
{
balance = a;
name = p;
}
friend void display(ICICI &,SBI &,BOI &);
};
class SBI
{
private :
int balance;
string name;
public :
void setdata(int a,string p)
{
balance = a;
name = p;
}
friend void display(ICICI &,SBI &,BOI &);
};
class BOI
{
private :
int balance;
string name;
public :
void setdata(int a,string p)
{
balance = a;
name = p;
}
friend void display(ICICI &,SBI &,BOI &);
};
int main()
{
ICICI obj1;
SBI obj2;
BOI obj3;
obj1.setdata(10000,"Chirag");
obj2.setdata(15000,"Dipak");
obj3.setdata(20000,"Vivek");
display(obj1,obj2,obj3);
}
10.Write a C++ Program to exchange values of two variables b/w two classes using friend
function.
#include<iostream>
using namespace std;
class B;
class A
{
int x,y,temp;
public:
void setdata()
{
cout<<"enter the a value"<<endl;
cin>>x;
cout<<"enter the b value"<<endl;
cin>>y;
}
void print()
{
cout<<"x="<<x<<"y="<<y<<endl;
}
};
class B
{
int x,y,temp;
public:
void setdata()
{
cout<<"enter the a value"<<endl;
cin>>x;
cout<<"enter the b value"<<endl;
cin>>y;
}
void print()
{
cout<<"x="<<x<<"y="<<y<<endl;
}
temp=ob1.y;
ob1.y=ob2.y;
ob2.y=temp;
int main()
{
cout<<"first"<<endl;
A obj1;
obj1.setdata();
obj1.print();
cout<<"second"<<endl;
B obj2;
obj2.setdata();
obj2.print();
swap(obj1,obj2);
cout<<"after swap"<<endl;
obj1.print();
obj2.print();
}
11.Write a C++ program to find total salary of husband and wife using a friend
function. Create
#include<iostream>
using namespace std;
class husband
{
string str;
int age;
float salary;
public:
void setdata()
{
cout<<"Enter the name"<<endl;
cin>>str;
cout<<"enter the age"<<endl;
cin>>age;
cout<<"Enter the salary"<<endl;
cin>>salary;
}
void print()
{
cout<<"name="<<str<<endl;
cout<<"age="<<age<<endl;
cout<<"salary="<<salary<<endl;
}
friend void addition(husband &H,class wife &W);
};
class wife
{
string str;
int age;
float salary;
public:
void setdata()
{
cout<<"Enter the name"<<endl;
cin>>str;
cout<<"enter the age"<<endl;
cin>>age;
cout<<"Enter the salary"<<endl;
cin>>salary;
}
void print()
{
cout<<"name="<<str<<endl;
cout<<"age="<<age<<endl;
cout<<"salary="<<salary<<endl;
}
friend void addition(husband &H,wife &W);
};
int main()
{
cout<<"Husband detail..\n";
husband hus;
hus.setdata();
hus.print();
cout<<"wife detail..\n";
wife wi;
wi.setdata();
wi.print();
12. Write a C++ Program to declare 3 classes. Declare private integers array as data
members in each class. Perform addition of two data member arrays into array of third class
using friend function.
#include<iostream>
using namespace std;
class B;
class C;
class A
{
int x[5],i,ele;
public:
void setdata()
{
ele=sizeof(x)/sizeof(x[0]);
for(i=0;i<ele;i++)
cin>>x[i];
}
void print()
{
for(i=0;i<ele;i++)
cout<<"x["<<i<<"]=";
cout<<endl;
}
class B
{
int y[5],j,ele1;
public:
void setdata()
{
ele1=sizeof(y)/sizeof(y[0]);
for(j=0;j<ele1;j++)
cin>>y[j];
}
void print()
{
for(j=0;j<ele1;j++)
cout<<"y[j]";
cout<<endl;
}
class C
{
int z[5];
friend void add(C &ob3,A &ob1 , B &ob2);
};
int main()
{
cout<<"first array"<<endl;
A obj1;
obj1.setdata();
obj1.print();
cout<<"second array"<<endl;
B obj2;
obj2.setdata();
obj2.print();
C obj3;
add(obj3,obj1,obj2);
}
#include<iostream>
#include<cstring>
using namespace std;
class A
{
char *str;
public:
A(const char *p)
{
cout<<"parameter constructor..\n";
str = new char[strlen(p)+1];
strcpy(str,p);
}
void modify()
{
str[0]='s';
}
void print()
{
cout<<"str="<<str<<endl;
}
};
int main()
{
A obj1("Vector");
A obj2 = obj1;
obj1.modify();
obj1.print();
obj2.print();
}
#include<iostream>
#include<cstring>
using namespace std;
class A
{
char *str;
public:
A (const char*p)
{
cout<<"parametrized constructor..\n";
str=new char(strlen(p)+1);
strcpy(str,p);
}
A (A &ob)
{
str=new char(strlen(ob.str)+1);
strcpy(str,ob.str);
}
void modify()
{
str[0]='s';
}
void print()
{
cout<<"str="<<str<<endl;
}
};
int main()
{
A obj1("vector");
A obj2=obj1;
obj1.modify();
obj1.print();
obj2.print();
}
#include<iostream>
using namespace std;
class ATM
{
static int count;
private:
ATM()
{
++count;
}
public:
static void ATM_fun()
{
cout<<"ATM_fun function"<<endl;
ATM log;
}
#include<iostream>
//#include<cstring>
using namespace std;
class A
{
int x,y;
public:
A():x(10),y(20)
{
cout<<" default constructor"<<endl;
}
A(A* &ob)
{
cout<<"copy constructor"<<endl;
x=ob->x,y=ob->y;
}
void print()
{
cout<<"x:-"<<x<<"y-"<<y<<endl;
}
};
int main()
{
A *obj1=new A; //default //creating dma object
obj1->print(); // access using -> because of pointer obj1 obj2 obj3 are pointers
17.Write a C++ program to sort the given five names from the keyboard and print it in the
sorted order. (Use C++'s DMA).
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
char **p;
char *temp;
p = new char *[5];
for(i=0;i<5;i++)
p[i] = new char [10];
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
for(k=0;;)
{
if(p[j+1][k] < p[j][k])
{
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
else if(p[j+1][k] == p[j][k])
{
k++;
}
else
break;
}
}
}
matrix::::
#include<iostream>
using namespace std;
int main()
{
//////////////// alloctae dynamic memory///////////
int **p,i,k,j,r1,r2,c1,c2,f,**q,**s;
cout<<"Enter the r1="<<endl;
cin>>r1;
cout<<"enter the c1="<<endl;
cin>>c1;
cout<<"multipled value r2="<<endl;
cin>>r2;
cout<<" value for matrix c2="<<endl;
cin>>c2;
p=new int*[r1];
q=new int*[r2];
s=new int*[r1];
for(i=0;i<r1;i++)
{
p[i]=new int[c1];
}
for(i=0;i<r2;i++)
{
q[i]=new int[c2];
}
for(i=0;i<r1;i++)
{
s[i]=new int[c2];
}