Practicals CPP F
Practicals CPP F
Practicals CPP F
int main() {
int num;
cout << "Enter number : ";
cin >> num;
if(num % 2 == 0) {
cout << num << " is Even number ";
} else {
cout << num << " is Odd number ";
}
return 0;
}
/********************OUTPUT********************
Enter number : 8
8 is Even number
Enter number : 7
7 is Odd number
**********************************************/
int main() {
int num,i,sum = 0,avg;
cout << "Enter number : ";
cin >> num;
return 0;
}
/********************OUTPUT********************
Enter number : 5
Sum is : 15
Average is : 3
Enter number : 8
Sum is : 36
Average is : 4
**********************************************/
3. Prime or Composite
#include<iostream>
using namespace std;
int main() {
int num,i,count = 0;
cout << "Enter number : ";
cin >> num;
Enter number : 8
8 is Composite number
**********************************************/
4. Sum, Difference, Product, and Quotient of Two Integers
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
return 0;
}
/********************OUTPUT********************
Enter first number: 20
Enter second number: 10
Sum : 30
Difference : 10
Product : 200
Quotient : 2
***********************************************/
Practical No. 2 : Write a program to demonstrate use of Function overloading.
#include<iostream>
using namespace std;
int volume(int a) {
return (a * a * a);
}
int main() {
cout << "The volume of Cube of side 3 is : " << volume(3) << endl;
cout << "The volume of Cylinder of radius 3 and height 4 is : " << volume(3, 4) << endl;
cout << "The volume of cuboid of 3, 4 and 5 is : " << volume(3, 4, 5) << endl;
return 0;
}
/*************************OUTPUT*************************
The volume of Cube of side 3 is : 27
The volume of Cylinder of radius 3 and height 4 is : 113
The volume of cuboid of 3, 4 and 5 is : 60
********************************************************/
Practical No. 3 : Write a program to demonstrate encapsulation using of class.
#include <iostream>
using namespace std;
// Encapsulation
class Encap
{
int age;
void show()
{
cout << "Hello " << endl;
}
public:
string name;
void setVal(int a)
{
age = a;
cout << "Age is : " << age << endl;
}
};
int main()
{
Encap E;
E.name = "Alex";
E.setVal(19);
cout << "Name is : " << E.name;
return 0;
}
/********************OUTPU********************
Age is : 19
Name is : Alex
**********************************************/
Practical No. 4 : Write a program to demonstrate use constructors and Destructor.
#include <iostream>
using namespace std;
class number
{
int num;
public:
// Constructor
number(void);
// Destructor
~number();
void printNumber()
{
cout << "Your number is " << num << endl;
}
};
number :: number(void)
{
cout << "In Constructor " << endl;
num = 10;
}
number :: ~number() {
cout << "In Destructor " << endl;
}
int main()
{
number num;
num.printNumber();
return 0;
}
/********************OUTPU********************
In Constructor
Your number is 10
In Destructor
**********************************************/
Practical No. 5 : Write a program to demonstrate single inheritance.
#include <iostream>
using namespace std;
class student
{
public:
int sRollNo;
string sName;
void getData()
{
cout << "Enter Name : ";
cin >> sName;
void displyInfo() {
total = m1 + m2 + m3;
cout << "RollNo.\tName\tMarks1\tMarks2\tMarks3\tTotal " << endl;
cout << " "<< sRollNo<<"\t"<<sName<<" \t "<<m1<<" \t "<<m2<<" \t "<<m3<<" \t "<< total;
}
};
int main()
{
marks std;
std.getData();
std.getMarks();
std.displyInfo();
return 0;
}
/********************OUTPU********************
Enter Name : Alex
Enter Roll No. : 25
Enter marks 1 : 85
Enter marks 2 : 86
Enter marks 3 : 87
RollNo. Name Marks1 Marks2 Marks3 Total
25 Alex 85 86 87 258
**********************************************/
Practical No. 6 : . Write a program to demonstrate multiple inheritances.
#include <iostream>
using namespace std;
class a
{
protected:
int a;
public:
void getA(int x)
{
a = x;
}
};
class b
{
protected:
int b;
public:
void getB(int y)
{
b = y;
}
};
int main()
{
addition add;
add.getA(10);
add.getB(20);
add.displayAdd();
return 0;
}
/********************OUTPU********************
a = 10
b = 20
a+b = 30
**********************************************/
Practical No. 7 : Write a program to demonstrate use of operator overloading using friend function.
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
int inch;
public:
Distance() {
feet = 0;
inch = 0;
}
Distance(int f, int i) {
feet = f;
inch = i;
}
void display() {
cout << feet << " feets and " << inch << " inch" << endl;
}
int main() {
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
d3 = d1 + d2;
d3.display();
return 0;
}
/********************OUTPU********************
**********************************************/
Practical No. 8 : Write a program to demonstrate use of friend function.
#include<iostream>
class demo {
int a,b;
public:
void getData() {
};
return (add.a+add.b);
int main()
demo add;
add.getData();
return 0;
/********************OUTPU********************
Addition is : 40
**********************************************/
Practical No. 9 : Write a program to demonstrate use of Virtual functions.
#include<iostream>
using namespace std;
class A {
public:
virtual void show() {
cout << "Show Base "<< endl;
}
void display() {
cout << "Display Base "<< endl;
}
};
class B : public A {
public:
void show() {
cout << "Show Derived "<< endl;
}
void display() {
cout << "Display Derived "<< endl;
}
};
int main(){
A a;
B b;
A *bptr;
return 0;
}
/********************OUTPU********************
Display Base
Show Base
Display Base
Show Derived
**********************************************/
Practical No. 10 : Write a program to demonstrate use of pointer.
#include <iostream>
using namespace std;
int main() {
int *a, i;
i = 10;
a = &i;
/********************OUTPU********************
i = 10
a = 0x61ff08
*a = 10
**********************************************/
#include<iostream>
int main() {
int a,*ptr,**pptr;
a = 100;
ptr = &a;
pptr = &ptr;
cout << "The address of ptr : " << ptr << endl;
cout << "The address of pptr : " << pptr << endl;
return 0;
/********************OUTPU********************
**********************************************/
b. Write a program to demonstrate use of pointer to objects.
#include<iostream>
using namespace std;
class A {
int feet,inch;
public:
void getData(){
cout << "feet " << feet <<" and "<< "inch " << inch << endl;
}
int main()
{
A a;
A *ptr = &a;
// arrow operator
ptr -> setData(5,6);
ptr -> getData();
//dot operator
(*ptr).setData(5,4);
(*ptr).getData();
return 0;
}
/********************OUTPU********************
feet 5 and inch 6
feet 5 and inch 4
**********************************************/
#include<iostream>
using namespace std;
return 0;
}
/********************OUTPU********************
The addition is : 30
**********************************************/
Practical No. 11 : Write a program to demonstrate use of Exception Handling.
#include<iostream>
using namespace std;
int main()
{
int a,b,div;
try
{
if(b!=0) {
div=a/b;
cout << "Division is : " << div << endl;
} else {
throw(b);
}
}
catch(int b)
{
cout << "Divide by zero Exception "<< endl;
}
return 0;
}
/********************OUTPU********************
Run 1 :
Division is : 2
Run 2 :
**********************************************/