C++ Assignment

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

KAZI NAZRUL UNIVERSITY 2020-

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.

I am really thankful to them.

Secondly, I would also like to thank my parents for supporting me a


lot.

Last but not the least, I am thankful to my friends who helped me a


lot in finishing this assignment within the limited.

It helped me to increase my knowledge and skills.

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>

using namespace std;

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(){}

friend int Bus::compare(Truck);


};

int Bus:: compare(Truck t){

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";

else cout<<"Same Speed\n";

return 0;

PROGRAM 3: PROGRAM TO DEMONSTRATE FRIEND CLASS

#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;
}

//now to overload operator '+'


complex operator + (complex ob){
complex res;
res.real=real+ob.real;
res.imag=imag+ob.imag;
return res;
}

void print(){
cout<<real<<" + i"<<imag<<"\n";
}
};

int main(){
complex c1(10, 5), c2(2, 4);
complex c3=c1+c2;
c3.print();
return 0;
}

PROGRAM 5: TO OVERLOAD INSERTION/EXCRETION OPERATORS


//To overload ‘<<’ and ‘>>’ operators and to take input and output the
geometrical coordinates
#include<iostream>
using namespace std;
class Point
{
public:
double m_dX, m_dY, m_dZ;

Point(double dX=0.0, double dY=0.0, double dZ=0.0){


m_dX = dX;
m_dY = dY;
m_dZ = dZ;
Page 6 of 15
}
friend ostream& operator<< (ostream& out, const Point& cPoint);
friend istream& operator>> (istream& in, Point& cPoint);
double GetX() { return m_dX; }
double GetY() { return m_dY; }
double GetZ() { return m_dZ; }
};
ostream& operator<< (ostream& out, Point& cPoint){
out << "(" << cPoint.m_dX << ", " <<cPoint.m_dY << ", " <<cPoint.m_dZ <<
")";
return out;
}

istream& operator>> (istream& in, Point& cPoint) {


in >> cPoint.m_dX;
in >> cPoint.m_dY;
in >> cPoint.m_dZ;
return in;
}
int main()
{
cout << "Enter a point: " << endl;
Point cPoint;
cin >> cPoint;
cout << "You entered: " << cPoint << endl;
return 0;
}

PROGRAM 6: PROGRAM TO SHOW METHOD OVERLOADING


#include<bits/stdc++.h>
using namespace std;
class Sum
{
public:
void add(float x,float y)
{
cout<<"\n1st method with float & float\n";
float s;
s=x+y;
cout<<s;
}
void add(int x,int y)
{
cout<<"\n2nd method with int & int\n";
float s;
s=x+y;
cout<<s;
}
void add(float x,int y)
Page 7 of 15
{
cout<<"\n3rd method with float & int\n";
float s;
s=x+y;
cout<<s;
}

};
int main(){
Sum s;
s.add(12.5f,20);
s.add(5.5f,2.2f);
s.add(10,20);
cout<<"\n";
return 0;
}

PROGRAM 7: PROGRAM TO SHOW METHOD OVERRIDING


#include<bits/stdc++.h>
using namespace std;
class AAA{
public:
void show()
{
cout<<"\nMethod of Base class\n";
}
};
class BBB:public AAA{
public:
void show()
{
cout<<"\nMethod of Derive class\n";
}
};
int main(){
AAA obj1;

Page 8 of 15
BBB obj2;
obj1.show();
obj2.show();
return 0;
}

PROGRAM 8: PROGRAM TO DEMONSTRATE RUNTIME


POLYMORPHISM
#include <bits/stdc++.h>
using namespace std;

class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }

void show ()
{ cout<< "show base class" <<endl; }
};

class derived:public base


{
public:
void print () //print () is already virtual function in derived class, we
could also declared as virtual void print () explicitly
{ cout<< "print derived class" <<endl; }

void show ()
{ cout<< "show derived class" <<endl; }
Page 9 of 15
};

//main function
int main()
{
base *bptr;
derived d;
bptr = &d;

//virtual function, binded at runtime (Runtime polymorphism)


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
return 0;
}

PROGRAM 9: PROGRAM TO SHOW MULTIPLE-INHERITANCE


#include<bits/stdc++.h>
using namespace std;
class A{
public:
A(){cout<<"Constructor of A\n";}
~A(){cout<<"Destructor of A\n";}
};
class B{
public:
B(){cout<<"Constructor of B\n";}
~B(){cout<<"Destructor of B\n";}
};
Page 10 of 15
class C:public B,public A{
public:
C(){cout<<"Constructor of C\n";}
~C(){cout<<"Destructor of C\n";}
};

int main(){
C obj;
return 0;
}

PROGRAM 10: TO IMPLEMENT TEMPLATE FUNCTION FOR


BUBBLE SORT ALGORITHM
#include<bits/stdc++.h>
using namespace std;
template <class T>
void bubbleSort(T *p,int n){
int i,j;
T tmp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(p[j]>p[j+1])
{
tmp=p[j];
p[j]=p[j+1];
p[j+1]=tmp;

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;
}

PROGRAM 11: PROGRAM TO IMPLEMENTING QUEUE


#include<bits/stdc++.h>
using namespace std;
#define MAX 5
int s[MAX],top=-1;
void push(int x){
if(top==MAX-1)
{
printf("Stack is Full\n");
return;

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;

double division(int a, int b) {


if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}

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

You might also like