VIDHYAA GIRI COLLEGE OF ARTS AND SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
OBJECT ORIENTED PROGRAMMING CONCEPTS USING C++P RACTICAL RECORD NOTEBOOK
Class: I BSC computer Science
Register Number: _______________
Name: _________________________
Subject Name: ______________________________ Subject Code:
________________________
Signature of the Incharge
PROGRAM 1: FUNCTION OVERLOADING
AIM:
To write a c++ program for simple calculation using function overloading.
PROGRAM:
#include <iostream.h>
#include <conio.h>
class funOverloader {
public:
void print(int a, int b) {
cout << "Sum: " << a + b << endl;
void print(double a, double b) {
cout << "Product: " << a * b << endl;
void print(char *str) {
cout << "String: " << str << endl;
};
int main() {
funOverloader obj;
obj.print(3, 5);
obj.print(2.5, 4.0);
obj.print("Hello, Overload!");
getch();
return 0;
}
OUTPUT:
Sum=8
Product=9.2
String=Hello World!
RESULT:
Thus the above program has been executed and verified.
PROGRAM 2: DEFAULT ARGUMENTS
AIM:
To write a c++ program using Default Argument.
PROGRAM:
#include <iostream.h>
#include <conio.h>
// Function with default arguments
void greetUser(const char *name = "Guest", int age = 0) {
cout << "Hello, " << name;
if (age > 0) {
cout << ". Age: " << age;
cout << endl;
int main() {
// Call the function with default arguments
greetUser();
greetUser("Alice");
greetUser("Bob", 25);
getch();
return 0;
OUTPUT:
Hello,Guest
Hello,Alice
Hello,Bob.age:25
PROGRAM 3: INLINE FUNCTION
Aim:
To write a c++ program for finding square number using Inline Function.
PROGRAM:
#include <iostream.h>
#include<conio.h>
inlineint Max(int x, int y)
return (x > y)? x : y;
void main() {
cout<<"Max (20,10): " << Max(20,10) <<endl;
cout<< "Max (0,200): " << Max(0,200) <<endl;
cout<<"Max (100,1010): " << Max(100,1010) <<endl;
getch();
OUTPUT:
Max(20,10): 20
Max(0,200): 200
Max(100,1010): 1010
RESULT:
Thus the above program has been executed and verified.
PROGRAM 4: CLASS AND OBJECT
Aim:
To write a c++ program for using Class and Objects.
Program:
#include <iostream.h>
class Rectangle {
private:
int length;
int width;
public:
Rectangle(int l, int w) {
length = l;
width = w;
}
int calculateArea() {
return length * width;
}
int calculatePerimeter() {
return 2 * (length + width);
}
};
int main() {
Rectangle rect1(5, 4);
Rectangle rect2(3, 7);
cout << "Area of rect1: " << rect1.calculateArea() << endl;
cout << "Perimeter of rect1: " << rect1.calculatePerimeter() << endl;
cout << "Area of rect2: " << rect2.calculateArea() << endl;
cout << "Perimeter of rect2: " << rect2.calculatePerimeter() << endl;
return 0;
}
Output:
Area of rect1: 20
Perimeter of rect1: 18
Area of rect2: 21
Perimeter of rect2: 20
PROGRAM 5: PASSING OBJECTS TO FUNCTIONS
Aim:
To write a c++ program for finding complex number using the concept of Passing Object Functions.
Program:
#include<iostream.h>
#include<conio.h>
class complex{
int a;
int b;
public:
void setData(int v1, int v2){
a = v1;
b = v2;
void setDataBySum(complex o1, complex o2){
a = o1.a + o2.a;
b = o1.b + o2.b;
void printNumber(){
cout<<"Your complex number is "<<a<<" + "<<b<<"i"<<endl;
};
int main(){
complex c1, c2, c3;
c1.setData(1, 2);
c1.printNumber();
c2.setData(3, 4);
c2.printNumber();
c3.setDataBySum(c1, c2);
c3.printNumber();
getch();
clrscr();
return 0;
Output:
Your complex number is 1+2i
Your complex number is 3+4i
Your complex number is 4+6i
Program 6: FRIEND FUNCTION
Aim:
To write a c++ program to calculate the length of given box using friend function.
Program:
#include<iostream.h>
#include<conio.h>
class Box
private:
int length;
public:
Box():length(0){}
friend int printLength(Box);
};
int printLength(Box b)
b.length+=10;
return b.length;
void main()
Box b;
cout<<"Length of box="<<printLength(b)<<endl;
getch();
Output:
Length of box=10
Program 7: “this “ POINTER
Aim:
TO write a c++ program using “this” pointer.
Program:
#include<iostream.h>
#include<conio.h>
class Test
private:
int x;
public:
void setX(int x)
this->x=x;
void print()
cout<<"x="<<x<<endl;
};
int main()
Test obj;
int x=20;
obj.setX(x);
obj.print();
getch();
return 0;
}
Output:
X=20
Result:
Program 8: CONSTRUCTOR AND DESTRUCTOR
Aim:
To write a c++ program to calculate simple function using the concepts of constructor and destructor.
Program:
#include <iostream.h>
#include<conio.h>
class Calculator
private:
int num1;
int num2;
public:
Calculator(int a, int b)
num1 = a;
num2 = b;
cout << "Calculator object created with numbers: " << num1 << " and " << num2 << endl;
~Calculator()
cout << "Calculator object destroyed" << endl;
int add()
return num1+num2;
}
int subtract()
return num1-num2;
int multiply()
return num1*num2;
};
int main() {
Calculator calc(10, 5);
cout << "Addition result: " << calc.add() << endl;
cout << "Subtraction result: " << calc.subtract() << endl;
cout << "Multiplication result: " << calc.multiply() << endl;
getch();
return 0;
Output:
Calculator object destroyed
Calculator object created with numbers:10 and 5
Addition result:15
Subtraction result:5
Multiplication result:50
Program 9: UNARY OPERATOR OVERLOADING
Aim:
To write a c++ program Unary prefix & postfix increment operator overloading (++).
Program:
#include <iostream.h>
#include<conio.h>
class Counter {
private:
int count;
public:
Counter() : count(0) {}
Counter& operator++() {
++count;
return *this;
Counter operator++(int) {
Counter temp = *this;
++(*this);
return temp;
void display() const {
cout << "Count: " << count << endl;
};
int main() {
Counter c;
++c;
c.display();
c++;
c.display();
getch();
clrscr();
return 0;
Output:
Count : 1
Count : 2
Program 10: BINARY OPERATOR OVERLOADING
Aim:
To write a c++ program for Add two complex numbers using binary '+' operator overloading
Program:
#include <iostream.h>
#include<conio.h>
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) {
Complex temp;
temp.real = real + other.real;
temp.imag = imag + other.imag;
return temp;
void display() {
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c1(2.5, 3.5);
Complex c2(1.6, 2.7);
Complex result;
result = c1 + c2;
cout << "Result of addition: ";
result.display();
getch();
clrscr();
return 0;
Output:
Result of addition : 4.1+ 6.2i
Program 11: SINGLE INHERITANCE
Aim:
To write a c++ program to find product of two numbers using single inheritance.
Program:
#include <iostream.h>
#include<conio.h>
class base //single base class
public:
int x;
void getdata()
cout << "Enter the value of x = "; cin >> x;
};
class derive : public base //single derived class
private:
int y;
public:
void readdata()
cout << "Enter the value of y = "; cin >> y;
void product()
cout << "Product = " << x * y;
};
int main()
derive a;
a.getdata();
a.readdata();
a.product();
getch();
clrscr();
return 0;
Output:
Enter the value of x=5
Enter the value of y=10
Product= 50
Program 12: MULTILEVEL INHERITANCE
Aim:
To write a c++ program for student marklist using multilevel inheritance.
Program:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class Student
protected:
char *name;
int rollNo;
public:
void getStudent() {
cout << "Enter student name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
void displayStudent() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNo << endl;
};
class Marks : public Student
protected:
int marks[3];
public:
void getMarks()
{
cout<<"Enter marks for three subjects:"<<endl;
for(int i=0;i<3;i++)
cout<<"Subject "<<i+1<<": ";
cin >>marks[i];
void displayMarks()
cout << "Marks obtained:" << endl;
for (int i = 0; i < 3; i++) {
cout << "Subject " << i + 1 << ": " << marks[i] << endl;
};
class Result : public Marks
public:
void calculateTotal()
int total = 0;
for (int i = 0; i < 3; i++)
total += marks[i];
cout << "Total marks: " << total << endl;
}
};
int main()
Result student;
student.getStudent();
student.getMarks();
cout << endl << "-------------------------" << endl;
student.displayStudent();
student.displayMarks();
student.calculateTotal();
getch();
return 0;
Output:
Enter student name: anu
Enter roll number:11
Enter marks for three subjects:
Subject 1: 55
Subject 2: 67
Subject 3: 89
_______________________________________________
Name: anu
Roll number:11
Marks obtained:
Subject 1: 55
Subject 2: 67
Subject 3: 89
Total marks: 211
Program 13: MULTIPLE INHERITANCE
Aim:
To write a c++ program for employee salary management using multiple inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class Employee
protected:
char *name;
int empId;
public:
void getEmployee()
cout << "Enter employee name: ";
cin >> name;
cout << "Enter employee ID: ";
cin >> empId;
};
class Salary1
protected:
int basicSal;
int allowance;
public:
void getSalary()
cout << "Enter basic salary: ";
cin >> basicSal;
cout << "Enter allowance: ";
cin >> allowance;
};
class SalaryManagement : public Employee, public Salary1
public:
void displaySalary()
cout << "\nEmployee Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
double totalSalary = basicSal + allowance;
cout << "Total Salary: " << totalSalary << endl;
};
int main()
SalaryManagement emp;
emp.getEmployee();
emp.getSalary();
emp.displaySalary();
getch();
return 0;
}
Output:
Enter employee name: kiya
Enter employee ID: 12
Enter basic salary:7000
Enter allowance:500
___________________________________________
Employee name : kiya
Employee ID:12
Total salary:7500
Program 14: HIERARCHICAL INHERITANCE
Aim:
To write a c++ program for Hostel Management system using hierarchical inheritance
Program:
#include <iostream.h>
#include <string.h>
#include<conio.h>
class Person {
protected:
char *name;
int age;
public:
void setData(char *n, int a) {
name = n;
age = a;
void displayData() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
};
class Student : public Person {
protected:
int rollNo;
public:
void setRollNo(int r) {
rollNo = r;
void displayRollNo() {
cout << "Roll No: " << rollNo << endl;
}
};
class HostelStudent : public Student {
protected:
char *hostelName;
int roomNo;
public:
void setHostelData(char *h, int r) {
hostelName = h;
roomNo = r;
void displayHostel() {
cout << "Hostel Name: " << hostelName << endl;
cout << "Room No: " << roomNo << endl;
};
int main() {
HostelStudent hs;
hs.setData("John", 20);
hs.setRollNo(101);
hs.setHostelData("XYZ Hostel", 301);
cout << "Student Details:" << endl;
hs.displayData();
hs.displayRollNo();
hs.displayHostel();
getch();
return 0;
}
Output:
Student Details:
Name:John
Age:20
Roll No:101
Hostel Name:XYZ Hostel
Room no:301
Program 15: HYBRID INHERITANCE
Aim:
To write a c++ program for displaying messages using hybrid inheritance.
Program:
#include <iostream.h>
#include<conio.h>
class Base1
public:
void display() {
cout << "Base class display" << endl;
};
class Derived1
public:
void display() {
cout << "Derived1 class display" << endl;
};
class Derived2 : public Base1, public Derived1
public:
void display() {
cout << "Derived2 class display" << endl;
};
class Derived3 : public Derived2
{
public:
void display() {
cout << "Derived3 class display" << endl;
};
int main()
Derived3 obj;
obj.Base1::display();
obj.Derived1::display();
obj.Derived2::display();
obj.display();
getch();
return 0;
Output:
Base class display
Derived1 class display
Derived2 class display
Derived3 class display
Program 16: VIRTUAL FUNCTION
Aim:
To write a c++ program for Book shop management using virtual function.
Program:
#include <iostream.h>
#include <string.h>
#include<conio.h>
class Book
protected:
char *title;
char *author;
float price;
public:
// Constructor
Book(char *t, char *a, float p) : title(t), author(a), price(p) {}
// Virtual function to display book details
virtual void display() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: $" << price << endl;
};
class FictionBook : public Book {
private:
char *genre;
public:
FictionBook(char *t,char *a, float p, char *g) : Book(t, a, p), genre(g) {}
void display(){
Book::display();
cout << "Genre: " << genre << endl;
};
// Derived class
class NonFictionBook : public Book {
private:
char *subject;
public:
// Constructor
NonFictionBook(char *t, char *a, float p, char *s) : Book(t, a, p), subject(s) {}
void display(){
Book::display();
cout << "Subject: " << subject << endl;
};
int main() {
FictionBook fictionBook("To Kill a Mockingbird", "Harper Lee", 10.99, "Classic");
NonFictionBook nonFictionBook("Sapiens: A Brief History of Humankind", "Yuval Noah Harari", 15.99,
"Anthropology");
cout << "Fiction Book Details:" << endl;
fictionBook.display();
cout << "\nNon-Fiction Book Details:" << endl;
nonFictionBook.display();
getch();
return 0;
Output:
Fiction Book Details:
Title: To kill a mockingbird
Author: Harper Lee
Price:$10.99
Genre:Classic
Non-Fiction Book Details:
Title:Sapiens:A Brief History of Humankind
Author: Yuval noah harari
Price:$15.99
Genre:Anthropology
Program 17: TEXT FILE
Aim:
To write a c++ program to create and manipulate a Text File using Text file.
Program:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
ofstream outputFile("example.txt");
if(outputFile){
outputFile<<"Hello,this is a text file."<<endl;
outputFile<<"it contains some text written form a C++ program."<<endl;
outputFile.close();
cout<<"Textfile'example.txt' created successfully."<<endl;
else{
return 1;
ifstream inputFile("example.txt");
char line[255];
if(inputFile){
cout<<"contents of 'example.txt':"<<endl;
while(inputFile.getline(line,255)){
cout<<line<<endl;
inputFile.close();
}else
cout<<"Unable to open file!"<<endl;
return 1;
getch();
return 0;
Output:
Textfile ‘example.txt’ created successfully.
Contents of ‘example.txt’:
Hello,this is a text file.
It contains some text written from a c++ program.
Program 18: FILE I/O OPERATIONS
Aim:
To write a c++ program for performing a sequential I/O(input/output) operations on a File.
Program:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main(){
clrscr();
ofstream outFile("output.txt");
if(!outFile){
cerr<<"Failed to open the output file."<<endl;
return 1;
outFile<<"Hello,World!"<<endl;
outFile<<"This is a sequential output operation."<<endl;
outFile<<"Writing data to a file using c++."<<endl;
outFile.close();
ifstream inFile("output.txt");
if(!inFile){
cerr<<"Failed to open the input file."<<endl;
return 1;
char line[255];
cout<<"contents of the file:"<<endl;
while(inFile){
inFile.getline(line,255);
cout<<line<<endl;
inFile.close();
getch();
return 0;
Output:
Textfile' example.txt' created successfully.
contents of 'example.txt':
Hello, this is a text file.
it contains some text written form a C++ program.
Result:
Program 19: COMMAND LINE ARGUMENT
Aim:
To write a c++ program to find the Biggest of three numbers using Command Line Arguments.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
if(argc !=4)
cout<<"Usage;"<<argv[0]<<"num1 num2 num3"<<endl;
return 1;
int num1=atoi(argv[1]);
int num2=atoi(argv[2]);
int num3=atoi(argv[3]);
int biggest = num1;
if(num2 > biggest)
biggest=num2;
if(num3 > biggest)
biggest=num3;
cout<<"The biggest number is:"<<biggest<<endl;
getch();
return 0;
Output:
C:\TURBOC3\BIN>CD..
C:\TURBOC3<CD SOURCE
C:\TURBOC3\SOURCE>commandargu.exe 12 22 15
The biggest of number is: 22
Program 20: CLASS TEMPLATE
Aim:
To write a c++ program for printing various type of data using one class template.
Program:
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class Pair{
private:
T1 first;
T2 second;
public:
Pair(T1 f,T2 s){
first =f;
second =s;
T1 getFirst() const{
return first;
T2 getSecond() const{
return second;
void setFirst(T1 f){
first=f;
void setSecond(T2 s){
second=s;
void display() const{
cout<<"("<<first<<","<<second<<")"<<endl;
}
};
int main(){
Pair<int,int>intPair(10,20);
cout<<"integer Pair:";
intPair.display();
Pair<double,char>doubleCharPair(3.14,'A');
cout<<"Double-char Pair:";
doubleCharPair.display();
getch();
clrscr();
return 0;
Output:
Integer pair(10,20)
Double-char pair(3.14, A)
Program 21: FUNCTION TEMPLATE
Aim:
To write a c++ program for finding maximum of different type of data using one Function Template.
Program:
#include<iostream.h>
#include<conio.h>
template<class T>
T max(T a,T b){
return (a>b)?a:b;
int main(){
int num1 = 10,num2 = 25;
cout<<"Max of"<<num1<<"and"<<num2<<"is;"<<max(num1,num2)<<endl;
double num3=15.5,num4=25.5;
cout<<"Max of"<<num3<<"and"<<num4<<"is;"<<max(num3,num4)<<endl;
char char1='a',char2 ='s';
cout<<"Max of"<<char1<<"and"<<char2<<"is;"<<max(char1,char2)<<endl;
getch();
return 0;
Output:
Max of 10 and 25 is: 25
Max of 15.5 and 25.5 is: 25.5
Max of a and s is: s
Program 22: EXCEPTION HANDLING
Aim:
To write a c++ program for finding exception(divided by zero) using exception handling.
Program:
#include <iostream.h>
int main() {
try {
int numerator, denominator, result;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
if (denominator == 0) {
throw runtime_error("Division by zero error");
result = numerator / denominator;
cout << "Result of division: " << result << endl;
catch (const exception& e) {
cerr << "Exception caught: " << e.what() << endl;
return 0;
}
Output:
Enter numerator: 15
Enter denominator: 0
Exception Caught:Division by zero error