C++ Assignment
C++ Assignment
C++ Assignment
2021
BANWARILAL BHALOTIA COLLEGE
ASANSOL
IN
C++ LANGUAGE
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to our
departmental executive teacher Mr. ARPAN TEWARY as well as
our principal DR. AMITAVA BASU who gave me the golden
opportunity to do this wonderful project on the topic C++
PROGRAMMING LANGUAGE, which also helped me in doing a lot
of research and I come to know about so many new things.
Soumyajit Mukherjee
Page 1 of 15
SL. C++ PROGRAMS PAGE
NO. NO.
1. 3
2. 4-5
3. 5
4. 6
5. 6-7
6. 7-8
7. 8-9
8. 9-10
9. 10-11
10. 11-12
11. 12-14
12. 15
Page 2 of 15
PROGRAM 1: PROGRAM TO DEMONSTRATE CLASS AND OBJECTS
#include<bits/stdc++.h>
using namespace std;
class Point{
private:
int x, y;
public:
Point(int x1, int y1){
x=x1, y=y1;
}
//copy constructor
Point(const Point &p2){
x=p2.x, y=p2.y;
}
int getX(){
return x;
}
int getY(){
return y;
}
};
int main(){
Point p1(10, 15);//Normal constructor
Point p2=p1;//Copy constructor called here
cout<<"p1.x= "<<p1.getX()<<", p1.y= "<<p1.getY()<<"\n";
cout<<"p2.x= "<<p2.getX()<<", p2.y= "<<p2.getY()<<"\n";
return 0;
}
Page 3 of 15
PROGRAM 2: PROGRAM TO DEMONSTRATE FRIEND FUNCTION
#include<bits/stdc++.h>
class Truck;
class Bus{
int s;
public:
Bus(int x){
s=x;
Bus(){
int compare(Truck);
};
class Truck{
int s;
public:
Truck(int x){
s=x;
}
Truck(){}
return (s-t.s);
int main(){
int a;
Bus b1(50);
Truck t1(145);
a=b1.compare(t1);
if(a>0)
cout<<"Bus is faster\n";
Page 4 of 15
else if(a<0)
cout<<"Truck is Faster\n";
return 0;
#include<bits/stdc++.h>
using namespace std;
class B;
class A{
private:
int a;
public:
A(){
a=5;
}
friend class B;//Friend class
};
class B{
private:
int b;
public:
void showA(A &x){
//since B is friend of A, it can access the private data memeber of A
cout<<"A::a= "<<x.a<<"\n";
}
};
int main(){
A a;
B b;
b.showA(a);
return 0;
}
Page 5 of 15
PROGRAM 4: PROGRAM TO SHOW OPERATOR OVERLOADING
//Program to overload ‘+’ operator to add two complex class objects
#include<iostream>
using namespace std;
class complex{
private:
int real, imag;
public:
complex(int r=0, int i=0){
real=r, imag=i;
}
void print(){
cout<<real<<" + i"<<imag<<"\n";
}
};
int main(){
complex c1(10, 5), c2(2, 4);
complex c3=c1+c2;
c3.print();
return 0;
}
};
int main(){
Sum s;
s.add(12.5f,20);
s.add(5.5f,2.2f);
s.add(10,20);
cout<<"\n";
return 0;
}
Page 8 of 15
BBB obj2;
obj1.show();
obj2.show();
return 0;
}
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
void show ()
{ cout<< "show derived class" <<endl; }
Page 9 of 15
};
//main function
int main()
{
base *bptr;
derived d;
bptr = &d;
int main(){
C obj;
return 0;
}
Page 11 of 15
}
}
}
}
int main(){
float f[100];
int n,i;
cout<<"How many elements ";
cin>>n;
cout<<"Enter "<<n<<" elements ";
for(i=0;i<n;i++)
cin>>f[i];
bubbleSort(f,n);
cout<<"The sorted array is ";
for(i=0;i<n;i++)
cout<<f[i]<<" ";
cout<<"\n";
return 0;
}
Page 12 of 15
}
top++;
s[top]=x;
}
int pop(){
int a;
if(top==-1)
{
printf("\nThe stack is Empty\n");
return -1;
}
else
{
a=s[top];
top--;
return a;
}
}
void display(){
int i;
if(top==-1)
printf("\nEmpty Stack. Nothing to display\n");
else
{
printf("The elements of the stack are ");
for(i=0;i<=top;i++)
printf("\t%d\t",s[i]);
}
}
void menu(){
printf("\t\t\t\tPROGRAM ON STACK\n\n");
printf("\t\t\t1. PUSH\n");
printf("\t\t\t2. POP\n");
printf("\t\t\t3. DISPLAY\n");
printf("\t\t\t4. EXIT\n");
printf("\t\tEnter your choice ");
}
int main(){
int ch,a;
while(1)
{
menu();
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number to push ");
scanf("%d",&a);
push(a);
printf("Press any key to continue......");
break;
case 2:
a=pop();
if(a!=-1)
printf("\nThe popped element is %d",a);
printf("Press any key to continue......");
break;
case 3:
display();
printf("Press any key to continue......");
Page 13 of 15
break;
case 4:
exit(0);
default:
printf("\nINVALID CHOICE\n");
printf("Press any key to continue......\n");
return 0;
break;
}
}
Page 14 of 15
PROGRAM 12: PROGRAM TO DEMONSTRATE EXCEPTION
HANDLING
#include <iostream>
using namespace std;
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Page 15 of 15