Practicals CPP F

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

SMT G.G.

KHADSE COLLEGE MUKTAINAGAR


Semester-3
BCA 306 Lab on C++ Programming
INDEX

Ex. Title Page Date Remark


NO. No.

1 Demonstrate the use of if……else and control structure.


1. even or odd
2. sum and average
3. prime or composite
4. sum, difference, product and quotient of two integers
2
Write a program to demonstrate use of Function overloading.
3
Write a program to demonstrate encapsulation using of class.
4 Write a program to demonstrate use constructors and
Destructor.
5
Write a program to demonstrate single inheritance.
6
Write a program to demonstrate multiple inheritances.
7 Write a program to demonstrate use of operator overloading
using friend function.
8
Write a program to demonstrate use of friend function.
9 Write a program to demonstrate use of Virtual functions.
10 Write a program to demonstrate use of pointer.
a. Write a program to demonstrate use of pointer to pointer.
b. Write a program to demonstrate use of pointer to objects.
c. Write a program to demonstrate use of pointer to function.
11
Write a program to demonstrate use of Exception Handling

Batch In-Charge Head of Department


Practical No. 1 : Demonstrate the use of if……else and control structure.
1. even or odd
#include<iostream>
using namespace std;

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
**********************************************/

2. Sum and Average


#include<iostream>
using namespace std;

int main() {
int num,i,sum = 0,avg;
cout << "Enter number : ";
cin >> num;

for(i = 1; i <= num; i++) {


sum += i;
}
cout << " Sum is : " << sum;

avg = sum / num;


cout << "\n Average is : " << avg;

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;

for(i = 1; i <= num; i++) {


if(num % i == 0) {
count++;
}
}
if(count == 1) {
cout << "None";
}
else if(count == 2) {
cout << num << " is Prime number ";
}
else {
cout << num << " is Composite number ";
}
return 0;
}
/********************OUTPUT********************
Enter number : 5
5 is Prime number

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;

int sum = num1 + num2;


int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;

cout << " Sum : " << sum;


cout << "\n Difference : " << difference;
cout << "\n Product : " << product;
cout << "\n Quotient : " << quotient;

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 volume(double r, int h) {


return (3.14 * r * r * h);
}

int volume(int l, int b, int h) {


return (l * b * h);
}

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;

cout << "Enter Roll No. : ";


cin >> sRollNo;
}
};

class marks : public student


{
public:
int m1, m2, m3, total;
void getMarks() {
cout << "Enter marks 1 : ";
cin >> m1;

cout << "Enter marks 2 : ";


cin >> m2;

cout << "Enter marks 3 : ";


cin >> m3;
}

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

class addition : public a, public b


{
public:
void displayAdd()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a+b = " << a + b;
}
};

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

friend Distance operator+(Distance, Distance);


};

Distance operator+(Distance d1, Distance d2) {


Distance result;
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
return result;
}

int main() {
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;

d3 = d1 + d2;
d3.display();

return 0;
}

/********************OUTPU********************

18 feets and 11 inch

**********************************************/
Practical No. 8 : Write a program to demonstrate use of friend function.

#include<iostream>

using namespace std;

class demo {

int a,b;

public:

void getData() {

cout << "Enter two numbers : ";

cin >> a >> b;

friend int sum(demo);

};

int sum(demo add){

return (add.a+add.b);

int main()

demo add;

add.getData();

cout << "Addition is : "<< sum(add);

return 0;

/********************OUTPU********************

Enter two numbers : 20 20

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;

cout << "Points to Base class" << endl;


bptr = &a;
bptr -> display();
bptr -> show();

cout << "\nPoints to Derived class" << endl;


bptr = &b;
bptr -> display();
bptr -> show();

return 0;
}

/********************OUTPU********************

Points to Base class

Display Base

Show Base

Points to Derived class

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;

cout << "\n i = "<< i; // value


cout << "\n a = "<< a; // Reference
cout << "\n *a = "<< *a; // value
return 0;
}

/********************OUTPU********************

i = 10
a = 0x61ff08
*a = 10

**********************************************/

a. Write a program to demonstrate use of pointer to pointer

#include<iostream>

using namespace std;

int main() {

int a,*ptr,**pptr;

a = 100;

ptr = &a;

pptr = &ptr;

cout << "The value of a : " << a << endl;

cout << "The address of a : " << &a << endl;

cout << "The address of ptr : " << ptr << endl;

cout << "The address of pptr : " << pptr << endl;

return 0;

/********************OUTPU********************

The value of a : 100

The address of a : 0x61ff08

The address of ptr : 0x61ff08

The address of pptr : 0x61ff04

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

void setData(int f, int i){


feet = f;
inch = i;
}
};

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
**********************************************/

c. Write a program to demonstrate use of pointer to function.

#include<iostream>
using namespace std;

int addition(int x, int y) {


return x+y;
}
int main() {
int a,b,sum;
int (*ptr) (int, int);
ptr = addition;
a = 10,
b = 20;
sum = (*ptr) (a, b);

cout << "The addition is : " << sum;

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;

cout << "Enter number for a : ";


cin >> a;

cout << "Enter number for b : ";


cin >> b;

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 :

Enter number for a : 10

Enter number for b : 5

Division is : 2

Run 2 :

Enter number for a : 10

Enter number for b : 0

Divide by zero Exception

**********************************************/

You might also like