POE - DATA STRUCTURES LAB - Explantion
POE - DATA STRUCTURES LAB - Explantion
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.
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
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!