DAV CENTENARY COLLEGE,
NIT-3 FARIDABAD
PRACTICAL FILE
Object- Oriented Programming
Using C++
(24BCA403DS02)
SESSION: 2025-2026
SUBMITTED BY: SUBMITTED TO :
Name: Surya Pratap Singh Mrs. Rajwinder Kaur
Class: BCA IIIrd (A)
Roll No. : 1241182034
INDEX
[Link] PROGRAM REMARKS
1. Create a C++ program to take two numbers as input
and display their sum.
2. Write a C++ program to find maximum number of array
element.
3. Write a program to find factorial of a number.
4. Write a program to define a class called Rectangle with
attributes length and width and display the area of the
rectangle.
5. Create a C++ program to define a class Student with
attributes name, roll no and marks of 5 subjects. Use
member functions to input and display student details
with total marks (display record of 3 students using
array of object).
6. Write a C++ program to demonstrate function
overloading by defining multiple functions with the
same name but different parameters.
7. WAP to demonstrate the working of friend function.
8. WAP to implement the concept of constructor and
destructor.
9. WAP to overload unary operators ++ and -- using
member function of class.
10. WAP to overload binary operators +.
11. Implement a program to demonstrate the concept of
inheritance by creating a base class 'Shape' and derived
class 'Rectangle'. Display area of rectangle using
inheritance.
12. Create a program to define a class Student with
attributes name and roll number. Use member
functions to input and display student details.
13. WAP to demonstrate the use of access specifiers
(Private, Public and Protected) in a class. Use concept
of multiple inheritance.
14. WAP to implement arithmetic operations on pointers.
15. WAP to demonstrate the use of pointers to objects.
Define a class Book with attributes 'title' and 'author'
and use pointers to access and display book details.
16. WAP to demonstrate dynamic polymorphism using
virtual function.
17. WAP to handle exceptions using a try catch block.
18. WAP to implement simple template functions to find
largest among two numbers. Test function with
different data types.
19. WAP to perform file input and output operations
including opening, reading, writing and closing files.
Que-1: Create a C++ program to take two numbers as input and
display their sum.
Ans-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, sum;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
sum = a + b;
cout << "Sum = " << sum;
getch();
Output :
Que-2: Write a C++ program to find maximum number of array
element.
Ans-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, max;
cout << "Enter size of array: ";
cin >> n; int arr[50]; // limit array size to 50
cout << "Enter " << n << " elements: ";
for(i = 0; i < n; i++)
cin >> arr[i];
max = arr[0]; // assume first element is maximum
for(i = 1; i < n; i++)
if(arr[i] > max)
max = arr[i];
}
cout << "Maximum element = " << max;
getch();
}
Output:-
Que-3: Write a program to find factorial of a number.
Ans-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i;
long fact = 1;
cout << "Enter a number: ";
cin >> n;
for(i = 1; i <= n; i++)
{
fact = fact * i;
cout << "Factorial of " << n << " = " << fact; getch();
Output:
Que-4: Write a program to define a class called Rectangle with
attributes length and width and display the area of the rectangle.
Ans-
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int length, width;
public:
void input()
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
}
void displayArea()
int area = length * width;
cout <<"Area of Rectangle = " << area;
};
void main()
{
clrscr();
Rectangle r;
[Link]();
[Link]();
getch();
}
Output:
Que-5: Create a C++ program to define a class Student with attributes
name, roll no and marks of 5 subjects. Use member functions to input
and display student details with total marks (display record of 3
students using array of object)
Ans-
#include<iostream.h>
#include<conio.h>
class Student
{
char name[30];
int rollno;
int marks[5];
int total;
public:
void input()
cout << "Enter marks of 5 subjects: ";
int i; // declare outside
for(i = 0; i < 5; i++)
{
cin >> marks[i];
total += marks[i];
}
void display()
cout << "\n-------------------------------\n";
cout << "Name: " << name;
cout << "\nRoll No: " << rollno;
cout << "\nMarks: ";
int i; // declare outside
for(i = 0; i < 5; i++)
cout << marks[i] << " ";
cout << "\nTotal Marks = " << total << "\n";
};
void main()
{
clrscr();
Student s[3]; // array of 3 students
cout << "Enter details of 3 students:\n";
int i;
for(i = 0; i < 3; i++)
{
cout << "\n--- Student " << (i+1) << " ---\n";
s[i].input();
}
cout << "\nDisplaying Student Records:\n";
for(i = 0; i < 3; i++)
s[i].display();
getch();
}
Output:
Que-6: Write a C++ program to demonstrate function overloading by
defining multiple functions with the same name but different
parameters
Ans-
#include <iostream.h>
#include <conio.h>
class Demo
{
public:
void display(int a)
cout << "Displaying Integer: " << a << endl;
void display(float b)
cout << "Displaying Float: " << b << endl;
void display(char c)
cout << "Displaying Character: " << c << endl;
};
void main()
{
clrscr();
Demo obj;
[Link](10);
[Link](3.14f);
[Link]('A');
getch();
}
Output:
Que-7: WAP to demonstrate the working of friend function
Ans-
#include<iostream.h>
#include<conio.h>
class xyz
{
int x,y;
public:
void get()
{
cout<<"Enter values for x&y";
cin>>x>>y;
void put()
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
friend void sum (xyz ob);
};
void sum (xyz ob)
cout<<ob.x+ob.y;
void main()
{
clrscr();
xyz obj;
[Link]();
[Link]();
sum(obj);
getch();
}
Output:
Que-8: WAP to implement the concept of constructor and destructor.
Ans-
#include <iostream.h>
#include <conio.h> class
Student
{
int roll;
char name[20];
public:
Student()
cout << "\nConstructor called!";
roll = 0;
name[0] = '\0';
}
void input()
cout << "\nEnter Roll No: ";
cin >> roll;
cout << "Enter Name: ";
cin >> name;
void display()
cout << "\nRoll No: " << roll;
cout << "\nName: " << name;
}
~Student()
cout << "\nDestructor called!";
};
void main()
{
clrscr();
Student s;
[Link]();
[Link]();
getch();
}
Output:
Que-9: WAP to overload unary operators ++ and -- using member
function of class
Ans-
#include <iostream.h>
#include <conio.h>
class sample
{
int x, y;
public:
void get(int a, int b)
{
x = a;
y = b;
}
void put()
cout << "x = " << x << endl;
cout << "y = " << y << endl << endl;
}
void operator ++();
void operator --();
void operator -();
};
void sample::operator ++()
{
x = x + 1;
y = y + 1;
}
void sample::operator --()
x = x - 1;
y = y - 1;
}
void sample::operator -()
{
x = -x;
y = -y;
}
void main()
{
clrscr();
sample ob1;
[Link](10, 20);
++ob1;
[Link]();
--ob1;
[Link]();
-ob1;
[Link]();
getch();
}
Output:
Que-10: WAP to overload binary operators +.
Ans-
#include <iostream.h>
#include <conio.h>
class Sample
{
int x;
public:
void get(int a)
{
x =a;
}
void put()
cout << "Value = " << x << endl;
Sample operator +(Sample s)
Sample temp;
temp.x = x + s.x;
return temp;
}
};
void main()
{
clrscr();
Sample ob1, ob2, ob3;
[Link](10);
[Link](20);
ob3 = ob1 + ob2;
cout << "Object 1: ";
[Link]();
cout << "Object 2: ";
[Link]();
cout << "Result (ob1 + ob2): ";
[Link]();
getch();
}
Output:
Que-11: Implement a program to demonstrate the concept of
inheritance by creating a base class 'Shape' and derived class
'Rectangle'. Display area of rectangle using inheritance.
Ans-
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
int length,breadth;
public:
void getdata()
cout<<"Enter length and breadth";
cin>>length>>breadth;
}
void put()
cout<<"length="<<length<<endl;
cout<<"breadth="<<breadth<<endl;
}
};
class rectangle:public shape
{
int area;
public:
void calculate()
{
area=length*breadth;
}
void display()
cout<<"area="<<length*breadth;
}
};
void main()
{
clrscr();
rectangle ob;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
Output:
Que-12: Create a program to define a class Student with attributes
name and roll number. Use member functions to input and display
student details
Ans-
#include<iostream.h>
#include<conio.h>
class student
{
private:
char name[10];
int roll;
public:
void getdata()
cout<<"Enter name & roll of student";
cin>>name>>roll;
}
void putdata()
{
cout<<"name="<<name<<endl;
cout<<"roll="<<roll;
}
};
void main()
{
clrscr();
student s1;
[Link]();
[Link]();
getch();
}
Output:
Que-13: WAP to demonstrate the use of access specifiers (Private,
Public and Protected) in a class. Use concept of multiple inheritance.
Ans-
#include<iostream.h>
#include<conio.h>
class Student
{
private:
int rollNo;
protected:
int marks;
public:
int age;
void setData()
rollNo=1;
marks=50;
age=18;
}
void showData()
cout<<"Student Data:\n";
cout<<"Roll No(private):"<<rollNo<<endl;
cout<<"Marks(protected):"<<marks<<endl;
cout<<"Age(public):"<<age<<endl;
}
};
class Teacher
protected:
int experience;
public:
void setExperience()
experience=5;
}
};
class School: public Student, public Teacher
{
public:
void showSchoolData()
{
marks=60;
age=19;
experience=6;
cout<<"\nschool Data:\n";
cout<<"Marks(protected):"<<marks<<endl;
cout<<"Age(public):"<<age<<endl;
cout<<"Experience(protected):"<<experience<<endl;
}
};
void main()
{
clrscr();
School obj;
[Link]();
[Link]();
[Link]();
[Link]();
cout<<"\nAccess public member from main():"<<[Link]<<endl;
getch();
Output:
Que-14: WAP to implement arithmetic operations on pointers.
Ans-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3]={10,20,30};
int *p=a;
cout<<"Value at p:"<<*p<<"\n";
p++;
cout<<"After p++:"<<*p<<"\n";
p--;
cout<<"After p--:"<<*p<<"\n"; getch();
Output:-
Que-15: WAP to demonstrate the use of pointers to objects. Define a
class Book with attributes 'title' and 'author' and use pointers to
access and display book details.
Ans-
#include<iostream.h>
#include<conio.h>
class book
char title[20],author[20];
public:
void get()
{
cout<<"Enter title and author of book : ";
cin>>title>>author;
}
void put()
cout<<"title = "<<title<<endl;
cout<<"author = "<<author<<endl;
}
};
void main()
clrscr();
book ob;
book *ptr;
ptr=&ob;
ptr->get();
ptr->put();
getch();
Output:
Que-16: WAP to demonstrate dynamic polymorphism using virtual
function.
Ans-
#include<iostream.h>
#include<conio.h>
class Animal
{
Public:
virtual void sound()
{
cout<<"Animals make sounds"<<endl;
};
class Dog: public Animal
public:
void sound()
{
cout<<"Dog barks"<<endl;
};
class Cat: public Animal
{
Public:
void sound()
{
cout<<"Cat meows"<<endl;
};
void main()
clrscr();
Animal *a; //base class pointer
Dog d;
Cat c;
a=&d;
a->sound(); //calls Dog's sound()
a=&c;
a->sound(); //calls Cat's sound()
getch();
}
Output:
Que-17: WAP to handle exceptions using a try-catch block.
Ans-
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
try {
if (b == 0) {
throw "Division by zero error!";
float result = (float)a / b;
cout << "Result = " << result << endl;
catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
cout << "Program continues after exception handling..." << endl;
return 0;
}
Output:
Que-18: WAP to implement simple template functions to find largest
among two numbers. Test function with different data types.
Ans-
#include <iostream.h>
#include <conio.h>
// Template function
template <class T>
T largest(T a, T b)
{
if(a > b)
return a;
else
return b;
}
void main()
{
clrscr();
cout << "Largest among two integers: " << largest(10, 20) << endl;
cout << "Largest among two floats: " << largest(15.5, 7.8) << endl;
cout << "Largest among two characters: " << largest('A', 'Z') << endl;
getch();
}
Output:
Que-19: Write a program to handle exceptions using multiple catch
blocks.
Ans-
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
try {
if (b == 0)
throw 0; // integer type exception
if (a < 0 || b < 0)
throw "Negative number found!"; // string type exception
float result = (float)a / b;
cout << "Division = " << result << endl;
}
catch (int) {
cout << "Error: Division by zero!" << endl;
}
catch (const char* msg) {
cout << "Error: " << msg << endl;
}
cout << "Program executed successfully!" << endl;
return 0;
}
Output:
Sample output 1:
Sample output 2:
Que-20: WAP to perform file input and output operations including
opening, reading, writing and closing files.
Ans-
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream outf("item");
outf<<"Book";
outf<<100<<endl;
int cost;
char name[20];
cout<<"Enter name and cost of item";
cin>>name>>cost;
outf<<name;
outf<<cost;
[Link]();
ifstream inf("item");
inf>>name;
inf>>cost;
cout<<"Name of item= "<<name<<endl;
cout<<"Cost= "<<cost<<endl;
[Link]();
getch();
}
Output: