際際滷

際際滷Share a Scribd company logo
Program 1: Employee Class
cpp
Copy code
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
int empNo;
string empName;
float basic;
float da;
float it;
float netSalary;
public:
void getData() {
cout << "Enter Employee Number: ";
cin >> empNo;
cout << "Enter Employee Name: ";
cin.ignore();
getline(cin, empName);
cout << "Enter Basic Salary: ";
cin >> basic;
da = 0.52 * basic;
it = 0.3 * (basic + da);
netSalary = basic + da - it;
}
void displayData() {
cout << "nEmployee Details:n";
cout << "Employee Number: " << empNo << endl;
cout << "Employee Name: " << empName << endl;
cout << "Basic Salary: " << basic << endl;
cout << "DA: " << da << endl;
cout << "Income Tax: " << it << endl;
cout << "Net Salary: " << netSalary << endl;
}
};
int main() {
Employee emp;
emp.getData();
emp.displayData();
return 0;
}
Explanation:
1. #include <iostream> and #include <string>:
o Includes libraries for input/output operations (cin, cout) and
string handling (string).
2. using namespace std;:
o Avoids the need to prefix standard functions (e.g., std::cin).
3. class Employee:
o Defines a class Employee with private and public members.
o Private Members:
 empNo: Employee number.
 empName: Employee name.
 basic, da, it, netSalary: Basic salary, Dearness Allowance,
Income Tax, and Net Salary, respectively.
o Public Members:
 void getData():
 Prompts the user to input employee details.
 Calculates:
 da as 52% of basic.
 it as 30% of the sum of basic and da.
 netSalary as basic + da - it.
 void displayData():
 Displays the employee details, including computed
values (da, it, netSalary).
4. int main():
o Creates an object emp of the Employee class.
o Calls getData() to input details and displayData() to display the
results.
Program 2: Constructor Overloading
cpp
Copy code
#include <iostream>
using namespace std;
class Box {
private:
double length, width, height;
public:
Box() {
length = width = height = 0;
}
Box(double l) {
length = width = height = l;
}
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double volume() {
return length * width * height;
}
void display() {
cout << "Volume: " << volume() << endl;
}
};
int main() {
Box b1;
Box b2(10);
Box b3(10, 20, 30);
cout << "Default Constructor: ";
b1.display();
cout << "Single Parameter: ";
b2.display();
cout << "Three Parameters: ";
b3.display();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the library for input/output operations and uses the
standard namespace.
2. class Box:
o Defines a class to represent a 3D box with private data
members (length, width, height) and public methods.
o Constructors:
 Default Constructor:
 Initializes all dimensions (length, width, height) to
zero.
 Single Parameter Constructor:
 Initializes all dimensions to the same value (l).
 Parameterized Constructor:
 Initializes dimensions to specified values (l, w, h).
o Public Methods:
 double volume():
 Returns the volume of the box (length * width *
height).
 void display():
 Prints the calculated volume of the box.
3. int main():
o Creates three objects of Box:
 b1 using the default constructor.
 b2 using the single-parameter constructor.
 b3 using the three-parameter constructor.
o Calls the display() method for each object to show the volume.
Program 3: Multilevel Inheritance
cpp
Copy code
#include <iostream>
using namespace std;
class Animal {
protected:
string species;
public:
void setSpecies(string s) {
species = s;
}
};
class Mammal : public Animal {
protected:
int legs;
public:
void setLegs(int l) {
legs = l;
}
};
class Dog : public Mammal {
private:
string breed;
public:
void setBreed(string b) {
breed = b;
}
void display() {
cout << "Species: " << species << endl;
cout << "Legs: " << legs << endl;
cout << "Breed: " << breed << endl;
}
};
int main() {
Dog dog;
dog.setSpecies("Canis");
dog.setLegs(4);
dog.setBreed("Labrador");
dog.display();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the library for input/output operations and uses the
standard namespace.
2. Multilevel Inheritance:
o class Animal:
 The base class with a protected member (species) and a
public method to set its value (setSpecies).
o class Mammal : public Animal:
 Inherits Animal and adds a protected member (legs) and a
method to set its value (setLegs).
o class Dog : public Mammal:
 Inherits Mammal and adds a private member (breed) and
a method to set its value (setBreed).
 void display():
 Displays the species, legs, and breed values.
3. int main():
o Creates an object dog of class Dog.
o Calls setSpecies, setLegs, and setBreed to set values for species,
number of legs, and breed.
o Calls display() to print the details.
Program 4: Array of Class Objects
cpp
Copy code
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int rollNo;
char grade;
public:
void getData() {
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Grade: ";
cin >> grade;
}
void display() {
cout << "nName: " << name;
cout << "nRoll Number: " << rollNo;
cout << "nGrade: " << grade << endl;
}
};
int main() {
Student students[3];
for(int i = 0; i < 3; i++) {
cout << "nEnter details for student " << i+1 <<
":n";
students[i].getData();
}
cout << "nStudent Details:n";
for(int i = 0; i < 3; i++) {
students[i].display();
}
return 0;
}
Explanation:
1. #include <iostream> and #include <string>:
o Includes libraries for input/output operations and string
handling.
2. class Student:
o Represents a student with private data members (name, rollNo,
grade) and public methods.
o Public Methods:
 void getData():
 Prompts the user to input a students details and
stores them in private members.
 void display():
 Prints the stored student details.
3. int main():
o Creates an array students of size 3 to store details of three
students.
o Input Loop:
 Iterates over the array, calling getData() for each student.
o Output Loop:
 Iterates over the array, calling display() for each student
to print their details.
Program 5: Function Overloading for Shapes
cpp
Copy code
#include <iostream>
using namespace std;
class Shape {
public:
double perimeter(double radius) {
return 2 * 3.14159 * radius;
}
double perimeter(double length, double width) {
return 2 * (length + width);
}
double perimeter(double a, double b, double c) {
return a + b + c;
}
};
int main() {
Shape shape;
cout << "Circle Perimeter: " << shape.perimeter(5) <<
endl;
cout << "Rectangle Perimeter: " << shape.perimeter(4, 6)
<< endl;
cout << "Triangle Perimeter: " << shape.perimeter(3, 4, 5)
<< endl;
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the library for input/output operations and uses the
standard namespace.
2. class Shape:
o Represents a shape and contains three overloaded methods for
calculating the perimeter of different shapes.
o Method Overloading:
 perimeter(double radius): Calculates the perimeter of a
circle (2 *  * radius).
 perimeter(double length, double width): Calculates the
perimeter of a rectangle (2 * (length + width)).
 perimeter(double a, double b, double c): Calculates the
perimeter of a triangle (a + b + c).
3. int main():
o Creates an object shape of the Shape class.
o Calls each of the overloaded perimeter() methods to calculate
and display the perimeter of a circle, rectangle, and triangle.
Program 6: Constructor with Default Arguments
cpp
Copy code
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double l = 1, double w = 1) {
length = l;
width = w;
}
void display() {
cout << "Length: " << length << ", Width: " <<
width << endl;
cout << "Area: " << length * width << endl;
}
};
int main() {
Rectangle r1;
Rectangle r2(5);
Rectangle r3(5, 10);
cout << "Default values: ";
r1.display();
cout << "One argument: ";
r2.display();
cout << "Two arguments: ";
r3.display();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the input/output library for cin and cout operations.
2. class Rectangle:
o Represents a rectangle with private data members (length,
width).
o Constructor:
 The constructor accepts two arguments (l and w) with
default values (1), meaning if no arguments are passed,
both length and width will be initialized to 1.
o void display():
 Displays the dimensions (length, width) and the area
(length * width) of the rectangle.
3. int main():
o Creates three objects of the Rectangle class:
 r1 uses the default constructor with no arguments.
 r2 uses the constructor with one argument (5 for both
length and width).
 r3 uses the constructor with two arguments (5 and 10).
o Calls display() for each object to show their details.
Program 7: Bank Account Class
cpp
Copy code
#include <iostream>
using namespace std;
class Account {
private:
double balance;
double interestRate;
public:
Account(double initial = 0, double rate = 0.05) {
balance = initial;
interestRate = rate;
}
void deposit(double amount) {
if(amount > 0) {
balance += amount;
cout << "Deposited: " << amount << endl;
}
}
void withdraw(double amount) {
if(amount <= balance) {
balance -= amount;
cout << "Withdrawn: " << amount << endl;
} else {
cout << "Insufficient funds" << endl;
}
}
void computeInterest() {
double interest = balance * interestRate;
balance += interest;
cout << "Interest added: " << interest << endl;
}
void showBalance() {
cout << "Current balance: " << balance << endl;
}
};
int main() {
Account acc(1000);
acc.showBalance();
acc.deposit(500);
acc.withdraw(200);
acc.computeInterest();
acc.showBalance();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes input/output functionality and the standard
namespace.
2. class Account:
o Represents a bank account with private members (balance and
interestRate).
o Constructor:
 Initializes balance with a default value of 0 and
interestRate with 0.05 (5%).
o Methods:
 void deposit(double amount):
 Deposits a positive amount into the account and
displays the deposited amount.
 void withdraw(double amount):
 Withdraws an amount if sufficient balance is
available. Displays an error message if insufficient
funds.
 void computeInterest():
 Computes and adds interest to the balance based
on interestRate.
 void showBalance():
 Displays the current balance.
3. int main():
o Creates an Account object acc with an initial balance of 1000.
o Calls methods to deposit, withdraw, compute interest, and
display the balance.
Program 8: Virtual Functions
cpp
Copy code
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base class display" << endl;
}
virtual void show() {
cout << "Base class show" << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "Derived class display" << endl;
}
void show() override {
cout << "Derived class show" << endl;
}
};
int main() {
Base* base;
Derived derived;
base = &derived;
base->display();
base->show();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the standard library for input/output operations.
2. class Base:
o Defines a base class with two virtual methods:
 virtual void display():
 A virtual method that can be overridden by derived
classes.
 virtual void show():
 Another virtual method.
3. class Derived : public Base:
o Inherits from Base and overrides both virtual methods.
o void display():
 Overridden method in the derived class.
o void show():
 Overridden method in the derived class.
4. int main():
o Creates a pointer base of type Base and an object derived of
type Derived.
o base = &derived;:
 The base pointer points to the derived object,
demonstrating polymorphism.
o Calls base->display() and base->show(), which invoke the
overridden methods in the Derived class due to virtual
functions.
5. Polymorphism:
o The use of virtual functions allows the derived class methods to
be called through a base class pointer, ensuring dynamic
dispatch.
Program 9: Friend Function
cpp
Copy code
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) {
real = r;
imag = i;
}
friend Complex addComplex(Complex c1, Complex c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
Complex addComplex(Complex c1, Complex c2) {
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main() {
Complex c1(3, 4), c2(5, 6);
Complex sum = addComplex(c1, c2);
cout << "First complex number: ";
c1.display();
cout << "Second complex number: ";
c2.display();
cout << "Sum: ";
sum.display();
return 0;
}
Explanation:
1. #include <iostream> and using namespace std;:
o Includes the necessary libraries for input/output operations.
2. class Complex:
o Represents a complex number with private members real and
imag.
o Constructor:
 Initializes the complex number with default values (0 for
both real and imag).
o Friend Function:
 friend Complex addComplex(Complex c1, Complex c2):
 This function is declared as a friend of the Complex
class, allowing it to access private members (real
and imag) directly.
 void display():
 Displays the complex number in the form real +
imag i.
3. Complex addComplex(Complex c1, Complex c2):
o Adds two complex numbers by adding their real and imaginary
parts separately.
4. int main():
o Creates two complex numbers c1 and c2.
o Uses the addComplex function to compute their sum and
displays the results.
Program 10: File Handling
cpp
Copy code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream outFile("sample.txt");
if(outFile.is_open()) {
outFile << "This is a line written to the file.n";
outFile << "This is another line.n";
outFile << "Adding some numbers: " << 42 << endl;
outFile.close();
cout << "File created and content written
successfully." << endl;
} else {
cout << "Unable to open file." << endl;
}
ifstream inFile("sample.txt");
string line;
cout << "nReading file contents:n";
if(inFile.is_open()) {
while(getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
return 0;
}
Explanation:
1. #include <iostream>, #include <fstream>, and #include <string>:
o Includes the libraries for input/output operations, file handling
(fstream), and string manipulation.
2. ofstream outFile("sample.txt");:
o Creates an ofstream object to write data to a file named
sample.txt.
o if(outFile.is_open()):
 Checks if the file is successfully opened. If true, writes
lines to the file and closes it.
3. ifstream inFile("sample.txt");:
o Creates an ifstream object to read data from the file sample.txt.
o while(getline(inFile, line)):
 Reads the file line by line and prints each line.
4. File Operations:
o If the file cannot be opened, an error message is printed.
o After reading, the file is closed.

More Related Content

Similar to OOP_EXPLAINED_example_of_cod_and_explainations.pdf (20)

Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptxUNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
C++ Topic 1.pdf from Yangon Technological University
C++ Topic 1.pdf  from Yangon Technological UniversityC++ Topic 1.pdf  from Yangon Technological University
C++ Topic 1.pdf from Yangon Technological University
ShweEainLinn2
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
SushmaGavaraskar
Oop1
Oop1Oop1
Oop1
Vaibhav Bajaj
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
Sivakumar M
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
Ayesha Bhatti
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
Rounak Samdadia
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
Envision Computer Training Institute
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptxUNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
C++ Topic 1.pdf from Yangon Technological University
C++ Topic 1.pdf  from Yangon Technological UniversityC++ Topic 1.pdf  from Yangon Technological University
C++ Topic 1.pdf from Yangon Technological University
ShweEainLinn2
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
Sivakumar M
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti

Recently uploaded (20)

Lecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptxLecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptx
Aqsa162589
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
IObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 FreeIObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 Free
mohsinrazakpa95
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
The Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio ManagementThe Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio Management
OnePlan Solutions
The Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original VersionThe Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
Java and AI with LangChain4j: Jakarta EE gets AI
Java and AI with LangChain4j: Jakarta EE gets AIJava and AI with LangChain4j: Jakarta EE gets AI
Java and AI with LangChain4j: Jakarta EE gets AI
Edward Burns
Jotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and BenefitsJotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and Benefits
Jotform
Software Architecture and Design-Ch-1.v6
Software Architecture and Design-Ch-1.v6Software Architecture and Design-Ch-1.v6
Software Architecture and Design-Ch-1.v6
Salahaddin University-Erbil, University of Kurdistan Hewler
Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025
bibi39322
Movavi Video Editor Crack + Activation Key [2025]
Movavi Video Editor Crack + Activation Key [2025]Movavi Video Editor Crack + Activation Key [2025]
Movavi Video Editor Crack + Activation Key [2025]
l07307095
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odooTour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
AxisTechnolabs
AR/VR Company in India - Simulanis.com
AR/VR Company in India  -  Simulanis.comAR/VR Company in India  -  Simulanis.com
AR/VR Company in India - Simulanis.com
mdashraf329911
Parallels Desktop Crack [Latest] 2025 free
Parallels Desktop Crack [Latest] 2025 freeParallels Desktop Crack [Latest] 2025 free
Parallels Desktop Crack [Latest] 2025 free
mohsinrazakpa96
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: OverviewChoreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
WSO2
Autodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free DownloadAutodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free Download
blouch52kp
Wondershare Democreator 8.3.3 Crack Activated Free Download
Wondershare Democreator 8.3.3 Crack Activated Free DownloadWondershare Democreator 8.3.3 Crack Activated Free Download
Wondershare Democreator 8.3.3 Crack Activated Free Download
adanataj41
Building-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-RequiredBuilding-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-Required
Ozias Rondon
Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025
mohsinrazakpa28
TVersity Pro Media Server Free CRACK Download
TVersity Pro Media Server Free CRACK DownloadTVersity Pro Media Server Free CRACK Download
TVersity Pro Media Server Free CRACK Download
mohsinrazakpa43
Lecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptxLecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptx
Aqsa162589
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
IObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 FreeIObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 Free
mohsinrazakpa95
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
The Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio ManagementThe Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio Management
OnePlan Solutions
The Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original VersionThe Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
Java and AI with LangChain4j: Jakarta EE gets AI
Java and AI with LangChain4j: Jakarta EE gets AIJava and AI with LangChain4j: Jakarta EE gets AI
Java and AI with LangChain4j: Jakarta EE gets AI
Edward Burns
Jotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and BenefitsJotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and Benefits
Jotform
Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025
bibi39322
Movavi Video Editor Crack + Activation Key [2025]
Movavi Video Editor Crack + Activation Key [2025]Movavi Video Editor Crack + Activation Key [2025]
Movavi Video Editor Crack + Activation Key [2025]
l07307095
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odooTour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
AxisTechnolabs
AR/VR Company in India - Simulanis.com
AR/VR Company in India  -  Simulanis.comAR/VR Company in India  -  Simulanis.com
AR/VR Company in India - Simulanis.com
mdashraf329911
Parallels Desktop Crack [Latest] 2025 free
Parallels Desktop Crack [Latest] 2025 freeParallels Desktop Crack [Latest] 2025 free
Parallels Desktop Crack [Latest] 2025 free
mohsinrazakpa96
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: OverviewChoreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
WSO2
Autodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free DownloadAutodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free Download
blouch52kp
Wondershare Democreator 8.3.3 Crack Activated Free Download
Wondershare Democreator 8.3.3 Crack Activated Free DownloadWondershare Democreator 8.3.3 Crack Activated Free Download
Wondershare Democreator 8.3.3 Crack Activated Free Download
adanataj41
Building-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-RequiredBuilding-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-Required
Ozias Rondon
Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025Wondershare Dr.Fone Crack Free Download 2025
Wondershare Dr.Fone Crack Free Download 2025
mohsinrazakpa28
TVersity Pro Media Server Free CRACK Download
TVersity Pro Media Server Free CRACK DownloadTVersity Pro Media Server Free CRACK Download
TVersity Pro Media Server Free CRACK Download
mohsinrazakpa43

OOP_EXPLAINED_example_of_cod_and_explainations.pdf

  • 1. Program 1: Employee Class cpp Copy code #include <iostream> #include <string> using namespace std; class Employee { private: int empNo; string empName; float basic; float da; float it; float netSalary; public: void getData() { cout << "Enter Employee Number: "; cin >> empNo; cout << "Enter Employee Name: "; cin.ignore(); getline(cin, empName); cout << "Enter Basic Salary: "; cin >> basic; da = 0.52 * basic; it = 0.3 * (basic + da); netSalary = basic + da - it; } void displayData() { cout << "nEmployee Details:n"; cout << "Employee Number: " << empNo << endl; cout << "Employee Name: " << empName << endl; cout << "Basic Salary: " << basic << endl; cout << "DA: " << da << endl; cout << "Income Tax: " << it << endl; cout << "Net Salary: " << netSalary << endl; }
  • 2. }; int main() { Employee emp; emp.getData(); emp.displayData(); return 0; } Explanation: 1. #include <iostream> and #include <string>: o Includes libraries for input/output operations (cin, cout) and string handling (string). 2. using namespace std;: o Avoids the need to prefix standard functions (e.g., std::cin). 3. class Employee: o Defines a class Employee with private and public members. o Private Members: empNo: Employee number. empName: Employee name. basic, da, it, netSalary: Basic salary, Dearness Allowance, Income Tax, and Net Salary, respectively. o Public Members: void getData(): Prompts the user to input employee details. Calculates: da as 52% of basic. it as 30% of the sum of basic and da. netSalary as basic + da - it. void displayData(): Displays the employee details, including computed values (da, it, netSalary). 4. int main(): o Creates an object emp of the Employee class. o Calls getData() to input details and displayData() to display the results. Program 2: Constructor Overloading cpp Copy code #include <iostream>
  • 3. using namespace std; class Box { private: double length, width, height; public: Box() { length = width = height = 0; } Box(double l) { length = width = height = l; } Box(double l, double w, double h) { length = l; width = w; height = h; } double volume() { return length * width * height; } void display() { cout << "Volume: " << volume() << endl; } }; int main() { Box b1; Box b2(10); Box b3(10, 20, 30); cout << "Default Constructor: "; b1.display(); cout << "Single Parameter: "; b2.display();
  • 4. cout << "Three Parameters: "; b3.display(); return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes the library for input/output operations and uses the standard namespace. 2. class Box: o Defines a class to represent a 3D box with private data members (length, width, height) and public methods. o Constructors: Default Constructor: Initializes all dimensions (length, width, height) to zero. Single Parameter Constructor: Initializes all dimensions to the same value (l). Parameterized Constructor: Initializes dimensions to specified values (l, w, h). o Public Methods: double volume(): Returns the volume of the box (length * width * height). void display(): Prints the calculated volume of the box. 3. int main(): o Creates three objects of Box: b1 using the default constructor. b2 using the single-parameter constructor. b3 using the three-parameter constructor. o Calls the display() method for each object to show the volume. Program 3: Multilevel Inheritance cpp Copy code #include <iostream> using namespace std; class Animal {
  • 5. protected: string species; public: void setSpecies(string s) { species = s; } }; class Mammal : public Animal { protected: int legs; public: void setLegs(int l) { legs = l; } }; class Dog : public Mammal { private: string breed; public: void setBreed(string b) { breed = b; } void display() { cout << "Species: " << species << endl; cout << "Legs: " << legs << endl; cout << "Breed: " << breed << endl; } }; int main() { Dog dog; dog.setSpecies("Canis"); dog.setLegs(4);
  • 6. dog.setBreed("Labrador"); dog.display(); return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes the library for input/output operations and uses the standard namespace. 2. Multilevel Inheritance: o class Animal: The base class with a protected member (species) and a public method to set its value (setSpecies). o class Mammal : public Animal: Inherits Animal and adds a protected member (legs) and a method to set its value (setLegs). o class Dog : public Mammal: Inherits Mammal and adds a private member (breed) and a method to set its value (setBreed). void display(): Displays the species, legs, and breed values. 3. int main(): o Creates an object dog of class Dog. o Calls setSpecies, setLegs, and setBreed to set values for species, number of legs, and breed. o Calls display() to print the details. Program 4: Array of Class Objects cpp Copy code #include <iostream> #include <string> using namespace std; class Student { private: string name; int rollNo; char grade; public: void getData() {
  • 7. cout << "Enter Name: "; cin.ignore(); getline(cin, name); cout << "Enter Roll Number: "; cin >> rollNo; cout << "Enter Grade: "; cin >> grade; } void display() { cout << "nName: " << name; cout << "nRoll Number: " << rollNo; cout << "nGrade: " << grade << endl; } }; int main() { Student students[3]; for(int i = 0; i < 3; i++) { cout << "nEnter details for student " << i+1 << ":n"; students[i].getData(); } cout << "nStudent Details:n"; for(int i = 0; i < 3; i++) { students[i].display(); } return 0; } Explanation: 1. #include <iostream> and #include <string>: o Includes libraries for input/output operations and string handling. 2. class Student: o Represents a student with private data members (name, rollNo, grade) and public methods. o Public Methods:
  • 8. void getData(): Prompts the user to input a students details and stores them in private members. void display(): Prints the stored student details. 3. int main(): o Creates an array students of size 3 to store details of three students. o Input Loop: Iterates over the array, calling getData() for each student. o Output Loop: Iterates over the array, calling display() for each student to print their details. Program 5: Function Overloading for Shapes cpp Copy code #include <iostream> using namespace std; class Shape { public: double perimeter(double radius) { return 2 * 3.14159 * radius; } double perimeter(double length, double width) { return 2 * (length + width); } double perimeter(double a, double b, double c) { return a + b + c; } }; int main() { Shape shape; cout << "Circle Perimeter: " << shape.perimeter(5) << endl; cout << "Rectangle Perimeter: " << shape.perimeter(4, 6) << endl;
  • 9. cout << "Triangle Perimeter: " << shape.perimeter(3, 4, 5) << endl; return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes the library for input/output operations and uses the standard namespace. 2. class Shape: o Represents a shape and contains three overloaded methods for calculating the perimeter of different shapes. o Method Overloading: perimeter(double radius): Calculates the perimeter of a circle (2 * * radius). perimeter(double length, double width): Calculates the perimeter of a rectangle (2 * (length + width)). perimeter(double a, double b, double c): Calculates the perimeter of a triangle (a + b + c). 3. int main(): o Creates an object shape of the Shape class. o Calls each of the overloaded perimeter() methods to calculate and display the perimeter of a circle, rectangle, and triangle. Program 6: Constructor with Default Arguments cpp Copy code #include <iostream> using namespace std; class Rectangle { private: double length; double width; public: Rectangle(double l = 1, double w = 1) { length = l; width = w; } void display() {
  • 10. cout << "Length: " << length << ", Width: " << width << endl; cout << "Area: " << length * width << endl; } }; int main() { Rectangle r1; Rectangle r2(5); Rectangle r3(5, 10); cout << "Default values: "; r1.display(); cout << "One argument: "; r2.display(); cout << "Two arguments: "; r3.display(); return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes the input/output library for cin and cout operations. 2. class Rectangle: o Represents a rectangle with private data members (length, width). o Constructor: The constructor accepts two arguments (l and w) with default values (1), meaning if no arguments are passed, both length and width will be initialized to 1. o void display(): Displays the dimensions (length, width) and the area (length * width) of the rectangle. 3. int main(): o Creates three objects of the Rectangle class: r1 uses the default constructor with no arguments. r2 uses the constructor with one argument (5 for both length and width). r3 uses the constructor with two arguments (5 and 10). o Calls display() for each object to show their details.
  • 11. Program 7: Bank Account Class cpp Copy code #include <iostream> using namespace std; class Account { private: double balance; double interestRate; public: Account(double initial = 0, double rate = 0.05) { balance = initial; interestRate = rate; } void deposit(double amount) { if(amount > 0) { balance += amount; cout << "Deposited: " << amount << endl; } } void withdraw(double amount) { if(amount <= balance) { balance -= amount; cout << "Withdrawn: " << amount << endl; } else { cout << "Insufficient funds" << endl; } } void computeInterest() { double interest = balance * interestRate; balance += interest; cout << "Interest added: " << interest << endl; }
  • 12. void showBalance() { cout << "Current balance: " << balance << endl; } }; int main() { Account acc(1000); acc.showBalance(); acc.deposit(500); acc.withdraw(200); acc.computeInterest(); acc.showBalance(); return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes input/output functionality and the standard namespace. 2. class Account: o Represents a bank account with private members (balance and interestRate). o Constructor: Initializes balance with a default value of 0 and interestRate with 0.05 (5%). o Methods: void deposit(double amount): Deposits a positive amount into the account and displays the deposited amount. void withdraw(double amount): Withdraws an amount if sufficient balance is available. Displays an error message if insufficient funds. void computeInterest(): Computes and adds interest to the balance based on interestRate. void showBalance(): Displays the current balance. 3. int main(): o Creates an Account object acc with an initial balance of 1000. o Calls methods to deposit, withdraw, compute interest, and display the balance.
  • 13. Program 8: Virtual Functions cpp Copy code #include <iostream> using namespace std; class Base { public: virtual void display() { cout << "Base class display" << endl; } virtual void show() { cout << "Base class show" << endl; } }; class Derived : public Base { public: void display() override { cout << "Derived class display" << endl; } void show() override { cout << "Derived class show" << endl; } }; int main() { Base* base; Derived derived; base = &derived; base->display(); base->show(); return 0; } Explanation:
  • 14. 1. #include <iostream> and using namespace std;: o Includes the standard library for input/output operations. 2. class Base: o Defines a base class with two virtual methods: virtual void display(): A virtual method that can be overridden by derived classes. virtual void show(): Another virtual method. 3. class Derived : public Base: o Inherits from Base and overrides both virtual methods. o void display(): Overridden method in the derived class. o void show(): Overridden method in the derived class. 4. int main(): o Creates a pointer base of type Base and an object derived of type Derived. o base = &derived;: The base pointer points to the derived object, demonstrating polymorphism. o Calls base->display() and base->show(), which invoke the overridden methods in the Derived class due to virtual functions. 5. Polymorphism: o The use of virtual functions allows the derived class methods to be called through a base class pointer, ensuring dynamic dispatch. Program 9: Friend Function cpp Copy code #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) {
  • 15. real = r; imag = i; } friend Complex addComplex(Complex c1, Complex c2); void display() { cout << real << " + " << imag << "i" << endl; } }; Complex addComplex(Complex c1, Complex c2) { Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } int main() { Complex c1(3, 4), c2(5, 6); Complex sum = addComplex(c1, c2); cout << "First complex number: "; c1.display(); cout << "Second complex number: "; c2.display(); cout << "Sum: "; sum.display(); return 0; } Explanation: 1. #include <iostream> and using namespace std;: o Includes the necessary libraries for input/output operations. 2. class Complex: o Represents a complex number with private members real and imag. o Constructor: Initializes the complex number with default values (0 for both real and imag).
  • 16. o Friend Function: friend Complex addComplex(Complex c1, Complex c2): This function is declared as a friend of the Complex class, allowing it to access private members (real and imag) directly. void display(): Displays the complex number in the form real + imag i. 3. Complex addComplex(Complex c1, Complex c2): o Adds two complex numbers by adding their real and imaginary parts separately. 4. int main(): o Creates two complex numbers c1 and c2. o Uses the addComplex function to compute their sum and displays the results. Program 10: File Handling cpp Copy code #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream outFile("sample.txt"); if(outFile.is_open()) { outFile << "This is a line written to the file.n"; outFile << "This is another line.n"; outFile << "Adding some numbers: " << 42 << endl; outFile.close(); cout << "File created and content written successfully." << endl; } else { cout << "Unable to open file." << endl; } ifstream inFile("sample.txt"); string line;
  • 17. cout << "nReading file contents:n"; if(inFile.is_open()) { while(getline(inFile, line)) { cout << line << endl; } inFile.close(); } return 0; } Explanation: 1. #include <iostream>, #include <fstream>, and #include <string>: o Includes the libraries for input/output operations, file handling (fstream), and string manipulation. 2. ofstream outFile("sample.txt");: o Creates an ofstream object to write data to a file named sample.txt. o if(outFile.is_open()): Checks if the file is successfully opened. If true, writes lines to the file and closes it. 3. ifstream inFile("sample.txt");: o Creates an ifstream object to read data from the file sample.txt. o while(getline(inFile, line)): Reads the file line by line and prints each line. 4. File Operations: o If the file cannot be opened, an error message is printed. o After reading, the file is closed.