0% found this document useful (0 votes)
8 views29 pages

OOPs Practical Manual

Uploaded by

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

OOPs Practical Manual

Uploaded by

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

SHRI SHANKARACHARYA TECHNICAL CAMPUS

LAB MANUAL

Object-Oriented Programming with C++

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 1


SHRI SHANKARACHARYA TECHNICAL CAMPUS

List of Experiments

1. Write a Program to check whether number is prime or not.


2. Write a Program to read number and to display the largest value between: (a)
Two number, (b)Three Numbers, (c) Four numbers by using switch-case
statements.
3. Write a Program to find sum of first natural numbers: sum= 1+2+3+4+…….
100 by using(a) for loop, (b)while loop, (c) dowhile loop
4. Write a Program to find sum of the following series using function
declaration: Sum= x- (x)3/3!+(x)5/5! ............... (x)n/n!
5. Write a Program to read the element of the given two matrixes & to perform
the matrix Multiplications
6. Write a Program to exchange the contents of two variables by using (a) Call
by value, (b) Call by reference.
7. Write a Program to perform the following arithmetic operations of a complex
number using astructure: (a) Addition of two complex numbers, (b) Subtraction
of two complex numbers, (c) Multiplication of two complex numbers, (d)
Division of two complex numbers.
8. Write an object oriented program (OOP) using C++ to exchange the private
data members of two different functions using friend Functions.
9. Write an OOP using C++ to count how many times a particular member
function of a class is called by: (a) A particular object, (b) Any objects
10. Write an OOP using C++ to define a constructor for a “Date” class that
initializes the Date objects with initial values. In case initial values are not
provided, it should initialize the objects with default values.

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 2


SHRI SHANKARACHARYA TECHNICAL CAMPUS

1. Write a Program to check whether number is prime or not.


#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
Explanation
• The function isPrime(int num) checks if the input number is prime.
• If the number is less than or equal to 1, it returns false because numbers 0 and 1 are
not prime.
• It then iterates from 2 to the square root of the number (for efficiency) and checks for
divisibility. If any divisor is found, it returns false.
• If no divisors are found, the function returns true, indicating that the number is prime.

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 3


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Output :
Input: Enter a number: 17
Output: 17 is a prime number.

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 4


SHRI SHANKARACHARYA TECHNICAL CAMPUS

2. Write a Program to read number and to display the largest value between: (a) Two
number, (b)Three Numbers, (c) Four numbers by using switch-case statements.
#include <iostream>
using namespace std;
void largestTwo(int a, int b) {
cout << "Largest between two numbers: " << max(a, b) << endl;
}
void largestThree(int a, int b, int c) {
cout << "Largest between three numbers: " << max({a, b, c}) << endl;
}
void largestFour(int a, int b, int c, int d) {
cout << "Largest between four numbers: " << max({a, b, c, d}) << endl;
}
int main() {
int choice;
cout << "Enter choice: 1 for two numbers, 2 for three numbers, 3 for four numbers: ";
cin >> choice;
switch (choice) {
case 1: {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
largestTwo(a, b);
break;
}
case 2: {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
largestThree(a, b, c);

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 5


SHRI SHANKARACHARYA TECHNICAL CAMPUS

break;
}
case 3: {
int a, b, c, d;
cout << "Enter four numbers: ";
cin >> a >> b >> c >> d;
largestFour(a, b, c, d);
break;
}
default:
cout << "Invalid choice" << endl;
}
return 0;
}
Explanation
1. The program prompts the user to choose one of three options:
o Option 1: Find the largest between two numbers.
o Option 2: Find the largest between three numbers.
o Option 3: Find the largest between four numbers.
2. Using a switch-case, it reads the appropriate number of inputs for the selected option
and calculates the largest using conditional expressions.
3. The result is printed for each case, and an error message is displayed if the user enters
an invalid choice.

Output :
(a) Two Numbers
Input:
Enter choice: 1
Enter two numbers: 5 8
Output: Largest between two numbers: 8

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 6


SHRI SHANKARACHARYA TECHNICAL CAMPUS

(b) Three Numbers


Input:
Enter choice: 2
Enter three numbers: 3 15 9
Output: Largest between three numbers: 15
(c) Four Numbers
Input:
Enter choice: 3
Enter four numbers: 10 6 22 17
Output: Largest between four numbers: 22

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 7


SHRI SHANKARACHARYA TECHNICAL CAMPUS

3. Write a Program to find sum of first natural numbers: sum= 1+2+3+4+……. 100 by
using(a) for loop, (b)while loop, (c) dowhile loop
#include <iostream>
using namespace std;

int main() {
int sum = 0;

// (a) Using for loop


sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
cout << "Sum of first 100 natural numbers (for loop): " << sum << endl;

// (b) Using while loop


sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i++;
}
cout << "Sum of first 100 natural numbers (while loop): " << sum << endl;

// (c) Using do-while loop


sum = 0;
i = 1;
do {
sum += i;
i++;

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 8


SHRI SHANKARACHARYA TECHNICAL CAMPUS

} while (i <= 100);


cout << "Sum of first 100 natural numbers (do-while loop): " << sum << endl;

return 0;
}
Explanation
1. For loop: Initializes i to 1 and iterates up to 100, adding i to sum in each iteration.
2. While loop: Uses a while loop to accumulate sum while i is incremented from 1 to
100.
3. Do-while loop: Similar to while, but ensures the loop executes at least once.
Each method will yield the sum of the first 100 natural numbers, which is 5050.

Output :
(a) Using for Loop
Output: Sum of first 100 natural numbers (for loop): 5050
(b) Using while Loop
Output: Sum of first 100 natural numbers (while loop): 5050
(c) Using do-while Loop
Output: Sum of first 100 natural numbers (do-while loop): 5050

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 9


SHRI SHANKARACHARYA TECHNICAL CAMPUS

4. Write a Program to find sum of the following series using function declaration: Sum=
x- (x)3/3!+(x)5/5! ............... (x)n/n!
#include <iostream>
#include <cmath>
using namespace std;
double factorial(int n) {
double fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
double seriesSum(double x, int n) {
double sum = 0;
for (int i = 1; i <= n; i += 2) {
double term = pow(x, i) / factorial(i);
sum += (i % 4 == 1) ? term : -term;
}
return sum;
}
int main() {
double x;
int n;
cout << "Enter value of x and n: ";
cin >> x >> n;
cout << "Sum of the series: " << seriesSum(x, n) << endl;
return 0;
}

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 10


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Explanation
1. factorial: A function to calculate the factorial of a number. This is used for the
denominator of each term.
2. calculateSeriesSum: A function that calculates the series sum up to the specified
highest odd power n.
o It iterates through odd powers only (1, 3, 5, ...).
o Each term alternates in sign, which is handled by the variable sign.
3. main: Reads x and n from the user, then calls calculateSeriesSum to display the result.

Output :
Input:
Enter value of x and n: 2 5
Output: Sum of the series: 1.933333

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 11


SHRI SHANKARACHARYA TECHNICAL CAMPUS

5. Write a Program to read the element of the given two matrixes & to perform the
matrix Multiplication.
#include <iostream>
using namespace std;

int main() {
int r1, c1, r2, c2;

// Read dimensions of the first matrix


cout << "Enter rows and columns of the first matrix: ";
cin >> r1 >> c1;

// Read dimensions of the second matrix


cout << "Enter rows and columns of the second matrix: ";
cin >> r2 >> c2;

// Check if multiplication is possible


if (c1 != r2) {
cout << "Matrix multiplication not possible. Columns of first matrix must be equal to
rows of second matrix." << endl;
return 1;
}

// Declare matrices
int mat1[r1][c1], mat2[r2][c2], result[r1][c2] = {0};

// Read elements of the first matrix


cout << "Enter elements of the first matrix:" << endl;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
cin >> mat1[i][j];

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 12


SHRI SHANKARACHARYA TECHNICAL CAMPUS

}
}

// Read elements of the second matrix


cout << "Enter elements of the second matrix:" << endl;
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
cin >> mat2[i][j];
}
}

// Perform matrix multiplication


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

// Display the result


cout << "Product of the matrices:" << endl;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 13


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Explanation
1. Input Validation: The program first checks if matrix multiplication is possible by
verifying that the number of columns in the first matrix (c1) matches the number of
rows in the second matrix (r2).
2. Matrix Initialization: Reads elements for both matrices, mat1 and mat2.
3. Multiplication Logic: Uses three nested loops:
o The outer two loops iterate over rows of mat1 and columns of mat2.
o The innermost loop iterates over columns of mat1 (or rows of mat2),
calculating the product and summing it for each element of the resulting
matrix.
4. Display Result: Prints the resulting matrix product.

Output :
1.
Input:
Enter rows and columns of the matrices: 2 2
Enter elements of first matrix:
12
34
Enter elements of second matrix:
20
12
Output:
Product of matrices:
44
10 8
2.
Enter rows and columns of the first matrix: 2 3
Enter rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
123

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 14


SHRI SHANKARACHARYA TECHNICAL CAMPUS

456
Enter elements of the second matrix:
78
9 10
11 12
Product of the matrices:
58 64
139 154

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 15


SHRI SHANKARACHARYA TECHNICAL CAMPUS

6. Write a Program to exchange the contents of two variables by using (a) Call by value,
(b) Call by reference.
#include <iostream>
using namespace std;

// Function to swap using call by value


void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "Inside swapByValue (Call by Value): a = " << a << ", b = " << b << endl;
}

// Function to swap using call by reference


void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int x = 10, y = 20;

cout << "Before swapping: x = " << x << ", y = " << y << endl;

// Swap by value (this will not affect x and y in main)


swapByValue(x, y);
cout << "After swapByValue (Call by Value): x = " << x << ", y = " << y << endl;

// Swap by reference (this will affect x and y in main)

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 16


SHRI SHANKARACHARYA TECHNICAL CAMPUS

swapByReference(x, y);
cout << "After swapByReference (Call by Reference): x = " << x << ", y = " << y << endl;

return 0;
}
Explanation and Output
1. swapByValue: This function swaps the values of a and b locally within the function.
Since it's call by value, the original x and y in main remain unchanged.
2. swapByReference: This function swaps the values of a and b by reference. Changes
made to a and b here will directly reflect in x and y in main.

Output :
Before swapping: x = 10, y = 20
Inside swapByValue (Call by Value): a = 20, b = 10
After swapByValue (Call by Value): x = 10, y = 20
After swapByReference (Call by Reference): x = 20, y = 10

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 17


SHRI SHANKARACHARYA TECHNICAL CAMPUS

7. Write a Program to perform the following arithmetic operations of a complex


number using a structure: (a) Addition of two complex numbers, (b) Subtraction of two
complex numbers, (c) Multiplication of two complex numbers, (d) Division of two
complex numbers.
#include <iostream>
using namespace std;
// Structure to represent a complex number
struct Complex {
float real;
float imag;
};

// Function to add two complex numbers


Complex add(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}

// Function to subtract two complex numbers


Complex subtract(Complex c1, Complex c2) {
Complex result;
result.real = c1.real - c2.real;
result.imag = c1.imag - c2.imag;
return result;
}

// Function to multiply two complex numbers


Complex multiply(Complex c1, Complex c2) {
Complex result;

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 18


SHRI SHANKARACHARYA TECHNICAL CAMPUS

result.real = (c1.real * c2.real) - (c1.imag * c2.imag);


result.imag = (c1.real * c2.imag) + (c1.imag * c2.real);
return result;
}

// Function to divide two complex numbers


Complex divide(Complex c1, Complex c2) {
Complex result;
float denominator = (c2.real * c2.real) + (c2.imag * c2.imag);

if (denominator == 0) {
cout << "Error: Division by zero." << endl;
result.real = result.imag = 0;
return result;
}

result.real = ((c1.real * c2.real) + (c1.imag * c2.imag)) / denominator;


result.imag = ((c1.imag * c2.real) - (c1.real * c2.imag)) / denominator;
return result;
}

// Function to display a complex number


void display(Complex c) {
if (c.imag >= 0)
cout << c.real << " + " << c.imag << "i" << endl;
else
cout << c.real << " - " << -c.imag << "i" << endl;
}

int main() {

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 19


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Complex c1, c2, result;

// Input for first complex number


cout << "Enter the real and imaginary parts of the first complex number (c1): ";
cin >> c1.real >> c1.imag;

// Input for second complex number


cout << "Enter the real and imaginary parts of the second complex number (c2): ";
cin >> c2.real >> c2.imag;

// Addition
result = add(c1, c2);
cout << "Addition: ";
display(result);

// Subtraction
result = subtract(c1, c2);
cout << "Subtraction: ";
display(result);

// Multiplication
result = multiply(c1, c2);
cout << "Multiplication: ";
display(result);

// Division
result = divide(c1, c2);
cout << "Division: ";
display(result);

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 20


SHRI SHANKARACHARYA TECHNICAL CAMPUS

return 0;
}
Explanation:
1. Structure Definition:
o The Complex structure represents a complex number with two fields: real and
imag (for real and imaginary parts).
2. Arithmetic Functions:
o Addition: Adds the real and imaginary parts of two complex numbers
separately.
o Subtraction: Subtracts the real and imaginary parts of two complex numbers
separately.
o Multiplication: Uses the formula for multiplying complex numbers:
(a+bi)×(c+di)=(ac−bd)+(ad+bc)i(a + bi) \times (c + di) = (ac - bd) + (ad +
bc)i(a+bi)×(c+di)=(ac−bd)+(ad+bc)i
o Division: Uses the formula for dividing complex numbers:
(a+bi)(c+di)=(ac+bd)(c2+d2)+(bc−ad)(c2+d2)i\frac{(a + bi)}{(c + di)} =
\frac{(ac + bd)}{(c^2 + d^2)} + \frac{(bc - ad)}{(c^2 + d^2)}i(c+di)(a+bi)
=(c2+d2)(ac+bd)+(c2+d2)(bc−ad)i It also checks for division by zero (i.e.,
when the denominator is zero).
3. Display Function:
o The display function prints the complex number in the format a + bi or a - bi
based on the sign of the imaginary part.
4. Main Function:
o The program reads two complex numbers from the user, performs addition,
subtraction, multiplication, and division, and displays the results.

Output :
Enter the real and imaginary parts of the first complex number (c1): 3 2
Enter the real and imaginary parts of the second complex number (c2): 1 4
Addition: 4 + 6i
Subtraction: 2 - 2i
Multiplication: -5 + 14i
Division: 0.7 - 0.4i

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 21


SHRI SHANKARACHARYA TECHNICAL CAMPUS

8. Write an object oriented program (OOP) using C++ to exchange the private data
members of two different functions using friend Functions.
#include <iostream>
using namespace std;

// Class definition
class Box {
private:
int length;
int width;

public:
// Constructor to initialize the values
Box(int l, int w) : length(l), width(w) {}

// Friend function declaration


friend void exchange(Box& b1, Box& b2);

// Function to display the values of length and width


void display() const {
cout << "Length: " << length << ", Width: " << width << endl;
}
};

// Friend function to exchange the private data members of two objects


void exchange(Box& b1, Box& b2) {
int tempLength = b1.length;
int tempWidth = b1.width;

// Exchange the values

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 22


SHRI SHANKARACHARYA TECHNICAL CAMPUS

b1.length = b2.length;
b1.width = b2.width;

b2.length = tempLength;
b2.width = tempWidth;
}

int main() {
// Creating two objects of class Box
Box box1(10, 5);
Box box2(20, 15);

// Display original values


cout << "Before exchange:" << endl;
cout << "Box 1: ";
box1.display();
cout << "Box 2: ";
box2.display();

// Exchange the data members using the friend function


exchange(box1, box2);

// Display exchanged values


cout << "\nAfter exchange:" << endl;
cout << "Box 1: ";
box1.display();
cout << "Box 2: ";
box2.display();

return 0;

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Explanation:
1. Box Class:
o It has two private data members: length and width, which represent the
dimensions of the box.
o A constructor initializes these values.
o A display function is provided to show the values of length and width.
2. Friend Function:
o exchange is a friend function of the Box class. This means exchange can
access and modify the private members (length and width) of the Box class
directly.
o The function takes two Box objects as arguments and exchanges their length
and width values.
3. Main Function:
o Two Box objects, box1 and box2, are created and initialized with different
values.
o Before calling the friend function exchange, the values of the boxes are
displayed.
o After calling exchange, the data members of the two boxes are swapped, and
the new values are displayed.

Output :
Before exchange:
Box 1: Length: 10, Width: 5
Box 2: Length: 20, Width: 15

After exchange:
Box 1: Length: 20, Width: 15
Box 2: Length: 10, Width: 5

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 24


SHRI SHANKARACHARYA TECHNICAL CAMPUS

9. Write an OOP using C++ to count how many times a particular member function of a
class is called by: (a) A particular object, (b) Any objects
#include <iostream>
using namespace std;

class MyClass {
private:
int individualCount; // Count calls for a particular object
static int globalCount; // Static count, shared by all objects

public:
// Constructor to initialize the individual count
MyClass() {
individualCount = 0;
}

// Static function to get the global count


static int getGlobalCount() {
return globalCount;
}

// Member function that increments both individual and global count


void functionCalled() {
individualCount++; // Increment count for this particular object
globalCount++; // Increment global count (shared by all objects)
}

// Function to display individual count and global count


void display() {
cout << "Function called " << individualCount << " times by this object." << endl;

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 25


SHRI SHANKARACHARYA TECHNICAL CAMPUS

static void displayGlobalCount() {


cout << "Function called " << globalCount << " times in total by all objects." << endl;
}
};

// Initialize static member variable


int MyClass::globalCount = 0;

int main() {
// Create two objects of MyClass
MyClass obj1, obj2;

// Call the function for obj1


obj1.functionCalled();
obj1.functionCalled();
obj1.display(); // Display individual count for obj1

// Call the function for obj2


obj2.functionCalled();
obj2.display(); // Display individual count for obj2

// Display global count (how many times the function was called across all objects)
MyClass::displayGlobalCount();

return 0;
}
Explanation:
1. Class Definition:

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 26


SHRI SHANKARACHARYA TECHNICAL CAMPUS

o The class MyClass contains two data members:


▪ individualCount: Keeps track of how many times the function is called
for a particular object.
▪ static int globalCount: A static member variable shared by all objects
of the class. It keeps track of how many times the function has been
called in total by any object.
2. Constructor:
o The constructor initializes the individualCount to 0 for each new object.
3. Member Function (functionCalled):
o This function is called each time we want to track the function call.
o It increments both the individualCount for the current object and the
globalCount for all objects.
4. Display Functions:
o display(): Displays the individual count of function calls for the current object.
o displayGlobalCount(): Displays the global count of function calls across all
objects, accessed through the class name because it's static.
5. Main Function:
o Two objects obj1 and obj2 are created.
o The function functionCalled is called multiple times on both objects.
o The counts are displayed using display() for individual counts and
displayGlobalCount() for the global count.

Output :
Function called 2 times by this object.
Function called 1 times by this object.
Function called 3 times in total by all objects.

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 27


SHRI SHANKARACHARYA TECHNICAL CAMPUS

10. Write an OOP using C++ to define a constructor for a “Date” class that initializes
the Date objects with initial values. In case initial values are not provided, it should
initialize the objects with default values.
#include <iostream>
using namespace std;
class Date {
private:
int day;
int month;
int year;
public:
// Constructor with default values
Date(int d = 1, int m = 1, int y = 2000) : day(d), month(m), year(y) {}

void displayDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
};
int main() {
Date date1(15, 8, 2023); // Initializing with custom values
Date date2; // Initializing with default values

cout << "Custom initialized date: ";


date1.displayDate();
cout << "Default initialized date: ";
date2.displayDate();
return 0;
}

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 28


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Explanation:
1. Class Definition:
o The Date class has three private data members: day, month, and year.
2. Constructor:
o The constructor is designed to take three parameters: d (day), m (month), and
y (year).
o If no arguments are provided when creating an object, default values are
assigned: day = 1, month = 1, and year = 2000.
o If some arguments are provided, the constructor initializes the data members
with those values.
3. Display Function:
o The display function prints the date in the format day/month/year.
4. Main Function:
o The program creates three Date objects:
▪ date1 is created using the default constructor, which initializes the date
as 1/1/2000.
▪ date2 is created with custom values 15/8/2023.
▪ date3 is created with only the day and month specified (10/11), with
the year defaulting to 2000.

Output :
Custom initialized date: Date: 15/8/2023
Default initialized date: Date: 1/1/2000

SOMESH KUMAR DEWANGAN DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 29

You might also like