c++ file
c++ file
Practical File
Of
Programming In C++
#include <iostream.h>
#include<conio.h>
class addition
{
int a, b;
public:
int inputs()
{
cout<<endl<<"---Addition---"<<endl;
cout<<"Enter value of A: ";
cin>>a;
cout<<"Enter value of B: ";
cin>>b;
}
void result()
{
cout<<"Addition of A and B: "<<a + b<<endl;
}
};
class multiply
{
int x, y;
public:
void inputs();
void result();
};
void multiply::inputs()
{
cout<<endl<<"---Mulitplication---"<<endl;
cout<<"Enter value of X: ";
cin>>x;
cout<<"Enter value of Y: ";
cin>>y;
}
void multiply::result()
{
cout<<"Multiplication of M and N: "<<x * y<<endl;
}
int main()
{
addition ob1;
ob1.inputs();
ob1.result();
multiply ob2;
ob2.inputs();
ob2.result();
getch();
return 0;
}
OUTPUT
PROGRAM 2
Write a program to implement array of objects.
#include <iostream.h>
#include<conio.h>
class Employee
{
int id, salary;
char name[30];
public:
void input()
{
cout<<"\nEnter Employee record (Id / Name / Salary): ";
cin>>id>>name>>salary;
}
void show()
{
cout<<"\nEmployee Id: " <<id<<endl;
cout<<"Employee Name: "<<name<<endl;
cout<<"Employee Salary: "<<salary<<endl;
}
};
int main()
{
Employee obj[3];
getch();
return 0;
}
OUTPUT
PROGRAM 3
Write a program to implement the friend function.
#include <iostream>
#include<conio.h>
using namespace std;
class A;
class B
{
int b;
public:
void getdata()
{
cout<<"Enter the value of B: ";
cin>>b;
}
friend int max(A,B);
};
class A
{
int a;
public:
void inputs()
{
cout<<"\nEnter the value of A: ";
cin>>a;
}
friend int max(A,B);
};
obj_A.inputs();
obj_B.getdata();
result = max(obj_A, obj_B);
getch();
return 0;
}
OUTPUT
PROGRAM 4
Write a program to implement constructor and destructor.
#include <iostream>
#include<conio.h>
int count = 0;
class test
{
int x,y;
public:
test() //Default constructor
{
x = 10;
y = 20;
count++;
cout<<"\nObject created: "<<count;
}
void display()
{
cout<<"\nValue of X: "<<x<<endl;
cout<<"Value of Y: "<<y<<endl;
}
~test()
{
cout<<"\nObject destroyed: "<<count;
--count;
}
};
int main()
{
test ob1;
ob1.display();
test ob2(100,200);
ob2.display();
test ob3(ob1);
ob3.display();
getch();
return 0;
}
OUTPUT
PROGRAM 5
Write a program to implement function overloading.
#include <iostream>
#include<conio.h>
class sum
{
int a=10, b=20, c=30;
public:
void add()
{
cout<<"\nAddition of A, B, and C: "<<a+b+c<<endl;
}
int main()
{
sum ob;
ob.add();
ob.add(100,200);
ob.add(20,40,60);
getch();
return 0;
}
OUTPUT
PROGRAM 6
Write a program to implement unary operator overloading.
#include<iostream>
#include<conio.h>
class test
{
int a,b,c;
public:
void setInput(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
void show()
{
cout<<"\nValue of A: "<<a<<endl;
cout<<"Value of B: "<<b<<endl;
cout<<"Value of C: "<<c<<endl;
}
int main()
{
test ob;
ob.setInput(100,200,300);
ob.show();
-ob; //Initialize overloaded operator (-)
ob.show();
getch();
return 0;
}
OUTPUT
PROGRAM 7
Write a program to implement binary operator overloading.
#include<iostream>
#include<conio.h>
class test
{
int x,y;
public:
test()
{
x = 0;
y = 0;
}
test(int a, int b)
{
x = a;
y = b;
}
void show()
{
cout<<"Value of X: "<<x<<endl;
cout<<"Value of Y: "<<y<<endl;
}
int main()
{
test ob1(10,20);
test ob2(30,40);
test ob3;
cout<<"\n--Object 1--\n";
ob1.show();
cout<<"\n--Object 2--\n";
ob2.show();
cout<<"\n--Object 3--\n";
ob3 = ob1 + ob2; //Initialize overloaded operator (+)
ob3.show();
getch();
return 0;
}
OUTPUT
PROGRAM 8
Write a program implement multiple inheritance.
#include<iostream>
#include<conio.h>
class A
{
protected:
int a;
public:
void getA()
{
cout<<"\nEnter the value of A: ";
cin>>a;
}
};
class B
{
protected:
int b;
public:
void getB()
{
cout<<"Enter the value of B: ";
cin>>b;
}
};
public:
void getC()
{
cout<<"Enter the value of C: ";
cin>>c;
}
void total()
{
cout<<"\nSum of A, B, and C: "<<a + b + c<<endl;
}
};
int main()
{
C obj;
obj.getA();
obj.getB();
obj.getC();
obj.total();
getch();
return 0;
}
OUTPUT
PROGRAM 9
Write a program to implement function template overloading.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
test(7.1,4.2);
test(‘a’,’b’
,’c’);
test(1,1.5);
getch();
return 0;
}
OUTPUT
PROGRAM 10
Write a program to implement exception handling.
#include<iostream>
#include<conio.h>
int main()
{
float a,b;
try
{
if(b!=0)
{
cout<<"\nDivision: "<<a/b;
}
else
{
throw b;
}
}
catch(float e)
{
cout<<"\nDivide by "<<e<<" error\n";
}
getch();
return 0;
}
OUTPUT