0% found this document useful (0 votes)
6 views

c++ file

Uploaded by

kapilkumar00786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c++ file

Uploaded by

kapilkumar00786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Shah Satnam Ji Boys’ College,Sirsa

Practical File
Of
Programming In C++

Submitted to:- Submitted by:-


Mr.Kapil Kumar Anmol Kumar
Assistant professor B.Sc 2nd Year
Computer Department Roll No:-
INDEX

Sr.no. Program Date Signature


1. Write a Program to implement defining
member functions inside the class and outside
the class.
2. Write a program to implement array of objects.

3. Write a program to implement the friend


function.

4. Write a program to implement constructor and


destructor.

5. Write a program to implement function


overloading.

6. Write a program to implement unary operator


overloading.

7. Write a program to implement binary operator


overloading.

8. Write a program implement multiple


inheritance.

9. Write a program to implement function


template overloading.

10. Write a program to implement exception


handling.
PROGRAM 1
Write a Program to implement defining member functions inside the class and
outside the class.

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

for(int i=0; i<3; i++)


{
obj[i].input();
}

for(int j=0; j<3; j++)


{
obj[j].show();
}

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

int max(A ob1, B ob2)


{
if(ob1.a > ob2.b)
{
return ob1.a;
}
else
{
return ob2.b;
}
}
int main()
{
int result;
A obj_A;
B obj_B;

obj_A.inputs();
obj_B.getdata();
result = max(obj_A, obj_B);

cout<<"\nGreater number: "<<result<<endl;

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

test(int a, int b) //Parameterized Constructor


{
x = a;
y = b;
count++;
cout<<"\nObject created: "<<count;
}

test(test &obj) //Copy constructor


{
x = obj.x;
y = obj.y;
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;
}

void add(int x, int y)


{
a = x;
b = y;
cout<<"\nAddition of A and B: "<<a+b<<endl;
}

void add(int x, int y, int z)


{
a = x;
b = y;
c = z;
cout<<"\nAddition of A, B, and C: "<<a+b+c;
}
};

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

void operator -() //Overloading (-) Operator


{
a = -a;
b = -b;
c = -c;
}
};

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

test operator +(test obj) //Overloading (+) operator


{
test temp;
temp.x = x + obj.x;
temp.y = y + obj.y;
return temp;
}
};

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

class C: public A, public B


{
int c;

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;

template <class T>


void test(T x, T y)
{
cout<<"\nTemplate with 2 parameters executed\n";
}

template <class T>


void test(T x, T y, T z)
{
cout<<"\nTemplate with 3 parameters executed\n";
}

void test(float x, float y)


{
cout<<"\nNon-Template executed\n";
}

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;

cout<<"\nEnter 1st Number: ";


cin>>a;
cout<<"Enter 2nd number: ";
cin>>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

You might also like