0% found this document useful (0 votes)
23 views17 pages

POE - DATA STRUCTURES LAB - Explantion

Am not sur why you criminals are changing ths goal post
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)
23 views17 pages

POE - DATA STRUCTURES LAB - Explantion

Am not sur why you criminals are changing ths goal post
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/ 17

Sure! I’ll explain each line of code for the provided C++ programs in detail.

1. Employee Class
cpp
Copy
#include <iostream>
#include <string>
• Includes the input-output stream library for standard input and
output operations and the string library for using the std::string type.
cpp
Copy
class Employee {
• Defines a class named Employee.
cpp
Copy
private:
• Specifies that the following members are private and can only be
accessed within the class.
cpp
Copy
int empNumber;
std::string empName;
float basic, da, it, netSalary;
• Declares private data members: empNumber (integer for employee
number), empName (string for employee name), basic (float for basic
salary), da (float for Dearness Allowance), it (float for Income Tax),
and netSalary (float for net salary).
cpp
Copy
public:
• Specifies that the following members are public and can be accessed
from outside the class.
cpp
Copy
Employee(int number, std::string name, float basicSalary) {
• Defines a constructor that initializes an Employee object with
parameters for employee number, name, and basic salary.
cpp
Copy
empNumber = number;
empName = name;
basic = basicSalary;
da = basic * 0.2; // Assuming DA is 20% of Basic
it = (basic + da) * 0.1; // Assuming IT is 10% of (Basic + DA)
netSalary = (basic + da) - it;
• Initializes the object's member variables and calculates the
allowances and net salary based on the specified formulas.
cpp
Copy
void print() {
• Declares a public member function named print to display employee
details.
cpp
Copy
std::cout << "Employee Number: " << empNumber << "\n";
std::cout << "Employee Name: " << empName << "\n";
std::cout << "Basic: " << basic << "\n";
std::cout << "DA: " << da << "\n";
std::cout << "IT: " << it << "\n";
std::cout << "Net Salary: " << netSalary << "\n";
• Prints the employee's details to the console.
cpp
Copy
int main() {
• Begins the main function, the entry point of the program.
cpp
Copy
Employee emp(101, "John Doe", 50000);
• Creates an Employee object named emp with an employee number of
101, name "John Doe", and a basic salary of 50000.
cpp
Copy
emp.print();
• Calls the print method of the emp object to display its details.
cpp
Copy
return 0;
}
• Returns 0 to indicate that the program ended successfully.

2. Constructor Overloading
cpp
Copy
#include <iostream>
• Includes the input-output stream library.
cpp
Copy
class Box {
• Defines a class named Box.
cpp
Copy
private:
• Specifies that the following members are private.
cpp
Copy
int length, breadth, height;
• Declares three private integer members: length, breadth, and height.
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
Box(int l) : length(l), breadth(l), height(l) {} // Cube
• Defines a constructor that initializes a cube (length, breadth,
and height are the same).
cpp
Copy
Box(int l, int b, int h) : length(l), breadth(b), height(h) {} // Rectangular
box
• Defines another constructor that initializes a rectangular box with
different dimensions.
cpp
Copy
void displayVolume() {
• Declares a member function to display the volume of the box.
cpp
Copy
std::cout << "Volume: " << length * breadth * height << std::endl;
• Calculates and prints the volume using the formula length * breadth *
height.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Box cube(5);
• Creates a Box object cube representing a cube with side length 5.
cpp
Copy
Box rectBox(5, 10, 15);
• Creates a Box object rectBox representing a rectangular box with
dimensions 5, 10, and 15.
cpp
Copy
cube.displayVolume(); // Volume of cube
• Calls the displayVolume method for cube to display its volume.
cpp
Copy
rectBox.displayVolume(); // Volume of rectangular box
• Calls the displayVolume method for rectBox to display its volume.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

3. Multilevel Inheritance
cpp
Copy
#include <iostream>
#include <string>
• Includes necessary libraries for input-output operations and string
handling.
cpp
Copy
class Person {
• Defines a base class named Person.
cpp
Copy
protected:
• Specifies that the following members are protected, allowing access
in derived classes.
cpp
Copy
std::string name;
• Declares a protected string member name.
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
Person(std::string personName) : name(personName) {}
• Defines a constructor that initializes the name member with the
provided parameter.
cpp
Copy
class Employee : public Person {
• Defines a derived class Employee that inherits from Person.
cpp
Copy
protected:
int empID;
• Declares a protected integer member empID.
cpp
Copy
public:
Employee(std::string personName, int id) : Person(personName), empID(id) {}
• Defines a constructor that initializes both the base class Person and the
derived class Employee members.
cpp
Copy
class Manager : public Employee {
• Defines a derived class Manager that inherits from Employee.
cpp
Copy
private:
std::string department;
• Declares a private string member department.
cpp
Copy
public:
Manager(std::string personName, int id, std::string dept) : Employee(personNam
e, id), department(dept) {}
• Defines a constructor that initializes the base class Employee and the
private member department.
cpp
Copy
void display() {
std::cout << "Manager Name: " << name << ", ID: " << empID << ", Departmen
t: " << department << std::endl;
}
• Declares a public member function to display the manager's details.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Manager mgr("Alice", 101, "HR");
• Creates a Manager object mgr with the name "Alice", ID 101, and
department "HR".
cpp
Copy
mgr.display();
• Calls the display method for mgr to print the manager's details.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

4. Array of Class Objects for Students


cpp
Copy
#include <iostream>
#include <string>
• Includes the necessary libraries for input-output operations and
string handling.
cpp
Copy
class Student {
• Defines a class named Student.
cpp
Copy
private:
• Specifies that the following members are private.
cpp
Copy
std::string name;
int rollNumber;
char grade;
• Declares private members: name (string), rollNumber (integer),
and grade (character).
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
void readData() {
• Declares a public member function to read student data.
cpp
Copy
std::cout << "Enter Name: ";
std::cin >> name;
std::cout << "Enter Roll Number: ";
std::cin >> rollNumber;
std::cout << "Enter Grade: ";
std::cin >> grade;
• Prompts the user to enter the student's name, roll number, and
grade, storing them in the respective member variables.
cpp
Copy
void displayData() {
• Declares a public member function to display student data.
cpp
Copy
std::cout << "Name: " << name << ", Roll Number: " << rollNumber << ", Gra
de: " << grade << std::endl;
}
• Prints the student's details to the console.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
const int numStudents = 3;
Student students[numStudents];
• Declares a constant for the number of students and creates an array
of Student objects.
cpp
Copy
for (int i = 0; i < numStudents; i++) {
• Starts a loop to read data for each student.
cpp
Copy
std::cout << "Enter details for student " << (i + 1) << ":\n";
students[i].readData();
• Prompts the user to enter details for each student and calls
the readData function.
cpp
Copy
std::cout << "\nStudents' Details:\n";
• Prints a header for the details section.
cpp
Copy
for (int i = 0; i < numStudents; i++) {
students[i].displayData();
}
• Loops through the students and calls the displayData function for each
to print their details.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

5. Shape Class with Overloaded Function


cpp
Copy
#include <iostream>
#include <cmath>
• Includes the necessary libraries for input-output operations and
mathematical functions.
cpp
Copy
class Shape {
• Defines a class named Shape.
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
float perimeter(float side) { // Square
return 4 * side;
}
• Defines an overloaded method to calculate the perimeter of a square.
It takes one parameter (side) and returns the perimeter.
cpp
Copy
float perimeter(float length, float breadth) { // Rectangle
return 2 * (length + breadth);
}
• Defines another overloaded method to calculate the perimeter of a
rectangle. It takes two parameters (length and breadth) and returns the
perimeter.
cpp
Copy
float perimeter(float radius, bool isCircle) { // Circle
return 2 * M_PI * radius;
}
• Defines another overloaded method to calculate the perimeter of a
circle. It takes a radius parameter and a boolean just to differentiate
from the rectangle method.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Shape shape;
• Creates an object of the Shape class.
cpp
Copy
std::cout << "Perimeter of Square: " << shape.perimeter(5) << std::endl;
• Calls the perimeter method for a square and prints the result.
cpp
Copy
std::cout << "Perimeter of Rectangle: " << shape.perimeter(5, 10) << std::endl
;
• Calls the perimeter method for a rectangle and prints the result.
cpp
Copy
std::cout << "Perimeter of Circle: " << shape.perimeter(5, true) << std::endl;
• Calls the perimeter method for a circle and prints the result.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

6. Constructor with Default Argument


cpp
Copy
#include <iostream>
• Includes the input-output stream library.
cpp
Copy
class Rectangle {
• Defines a class named Rectangle.
cpp
Copy
private:
• Specifies that the following members are private.
cpp
Copy
int length, breadth;
• Declares private members: length and breadth.
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
Rectangle(int l = 1, int b = 1) : length(l), breadth(b) {}
• Defines a constructor with default arguments. If no values are
provided, it initializes length and breadth to 1.
cpp
Copy
void displayArea() {
• Declares a public member function to display the area of the
rectangle.
cpp
Copy
std::cout << "Area: " << length * breadth << std::endl;
}
• Calculates and prints the area using the formula length * breadth.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Rectangle rect1; // Uses default arguments
• Creates a Rectangle object rect1 using default values (1, 1).
cpp
Copy
Rectangle rect2(5, 10); // Custom dimensions
• Creates a Rectangle object rect2 with specified dimensions 5 and 10.
cpp
Copy
rect1.displayArea();
• Calls the displayArea method for rect1 to display its area.
cpp
Copy
rect2.displayArea();
• Calls the displayArea method for rect2 to display its area.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

7. Account Class
cpp
Copy
#include <iostream>
• Includes the input-output stream library.
cpp
Copy
class Account {
• Defines a class named Account.
cpp
Copy
private:
• Specifies that the following members are private.
cpp
Copy
float balance;
• Declares a private float member balance to store the account balance.
cpp
Copy
public:
• Specifies that the following members are public.
cpp
Copy
Account() : balance(0) {}
• Defines a constructor that initializes balance to 0 when
an Account object is created.
cpp
Copy
void deposit(float amount) {
balance += amount;
std::cout << "Deposited: " << amount << "\n";
}
• Declares a member function to deposit an amount into the account.
It adds the amount to balance and prints the deposited amount.
cpp
Copy
void withdraw(float amount) {
if (amount > balance) {
std::cout << "Insufficient balance!\n";
} else {
balance -= amount;
std::cout << "Withdrawn: " << amount << "\n";
}
}
• Declares a member function to withdraw an amount. It checks if
the amount is greater than balance. If so, it prints an error message;
otherwise, it deducts the amount from the balance.
cpp
Copy
void showBalance() {
std::cout << "Balance: " << balance << "\n";
}
• Declares a member function to display the current balance.
cpp
Copy
void computeInterest(float rate) {
float interest = balance * rate / 100;
std::cout << "Interest: " << interest << "\n";
}
• Declares a member function to compute and display interest based
on the current balance and given rate.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Account acc;
• Creates an Account object acc, which initializes balance to 0.
cpp
Copy
acc.deposit(1000);
• Calls the deposit function to add 1000 to the account balance.
cpp
Copy
acc.showBalance();
• Calls the showBalance function to display the current balance.
cpp
Copy
acc.computeInterest(5);
• Calls the computeInterest function with a rate of 5% to compute and
display the interest.
cpp
Copy
acc.withdraw(200);
• Calls the withdraw function to withdraw 200 from the account.
cpp
Copy
acc.showBalance();
• Calls the showBalance function again to display the updated balance.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

8. Virtual Functions
cpp
Copy
#include <iostream>
• Includes the input-output stream library.
cpp
Copy
class Base {
• Defines a base class named Base.
cpp
Copy
public:
virtual void show() {
std::cout << "Base class show function called." << std::endl;
}
};
• Declares a virtual function show in the base class. This allows derived
classes to override it.
cpp
Copy
class Derived : public Base {
• Defines a derived class named Derived that inherits from Base.
cpp
Copy
public:
void show() override {
std::cout << "Derived class show function called." << std::endl;
}
};
• Overrides the show function from the base class to provide specific
functionality for the derived class.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
Base* b; // Base class pointer
• Declares a pointer to the base class Base.
cpp
Copy
Derived d; // Derived class object
• Creates an object of the derived class Derived.
cpp
Copy
b = &d; // Base pointer pointing to derived object
• Points the base class pointer b to the derived class object d.
cpp
Copy
b->show(); // Calls derived class show function
• Calls the show function using the base pointer. Due to polymorphism,
this will invoke the show method in the Derived class.
cpp
Copy
return 0;
}
• Returns 0 to indicate successful execution.

9. Friend Function
cpp
Copy
#include <iostream>
• Includes the input-output stream library.
cpp
Copy
class Box {
• Defines a class named Box.
cpp
Copy
private:
int width;
• Declares a private integer member width.
cpp
Copy
public:
Box(int w) : width(w) {}
• Defines a constructor that initializes width with the provided value.
cpp
Copy
friend void printWidth(Box b);
};
• Declares a friend function printWidth that can access private members
of Box.
cpp
Copy
void printWidth(Box b) {
• Defines the friend function printWidth, which takes a Box object as a
parameter.
cpp
Copy
std::cout << "Width: " << b.width << std::endl;
}
• Prints the width of the Box object passed to it

9. Friend Function (continued)


cpp
Copy
int main() {
• Begins the main function, which serves as the entry point of the
program.
cpp
Copy
Box box(10);
• Creates a Box object named box and initializes its width to 10 using the
constructor.
cpp
Copy
printWidth(box); // Accessing private member width
• Calls the printWidth function, passing the box object. This function can
access the private member width of the Box class due to its friendship
with the Box class.
cpp
Copy
return 0;
}
• Returns 0 to indicate that the program completed successfully.

10. File Creation and Writing


cpp
Copy
#include <iostream>
#include <fstream>
• Includes necessary libraries: the input-output stream library
(<iostream>) for standard I/O operations and the file stream library
(<fstream>) for file operations.
cpp
Copy
int main() {
• Begins the main function.
cpp
Copy
std::ofstream outfile("example.txt");
• Creates an output file stream object named outfile and opens a file
named example.txt. If the file does not exist, it will be created.
cpp
Copy
if (outfile.is_open()) {
• Checks if the file was successfully opened. The is_open() function
returns true if the file stream is open.
cpp
Copy
outfile << "Hello, this is a test file.\n";
outfile << "Writing some content to the file.\n";
• Writes text to the file using the output stream. Each line is added to
the file. The \n character creates a new line.
cpp
Copy
outfile.close(); // Close the file
• Closes the file stream to ensure all data is flushed to the file and
resources are freed.
cpp
Copy
std::cout << "File created and content written successfully." << std::endl
;
• Prints a success message to the console indicating that the file was
created and written to successfully.
cpp
Copy
} else {
std::cout << "Unable to open the file." << std::endl;
}
• If the file could not be opened, this block executes, printing an error
message to the console.
cpp
Copy
return 0;
}
• Returns 0 to indicate that the program completed successfully.

Summary
The provided C++ programs illustrate various OOP concepts, including
classes, constructors, inheritance, polymorphism, friend functions, and file
operations. Each program is designed to show specific features of C++, and
the line-by-line explanations help clarify how these features work and
interact with one another.
If you have any specific programs you'd like to dive deeper into or need
further clarification, feel free to ask!

You might also like