OOPS_Lab_Programs_2024-25
OOPS_Lab_Programs_2024-25
1. Simple Classes and Objects: Create an EMPLOYEE class with following members:
Employee Number, Employee Name, Basic, DA, IT, Net Salary. Member functions: to
read the data, to calculate Net Salary and to print data members. Develop C++
program to demonstrate reading N employees information and compute Net Salary of
each employee. (Consider Dearness Allowance (DA) = 52% of Basic and Income Tax
(IT) = 30% of the gross salary. Net Salary = Basic + DA - IT).
#include<iostream>
using namespace std;
class Employee
{
char emp_name[30];
int emp_number;
float basic, da, it, gross_salary, net_salary;
public:
void read_emp_details(int count)
{
cout<<"\n\n*** Enter Employee "<<count<<" Details ***";
cout<<"\nEmployee Number: ";
cin>>emp_number;
cout<<"Employee Name: ";
cin>>emp_name;
cout<<"Basic Salary: ";
cin>>basic;
cout<<"\n---- Employee "<<count<<" Datails are saved ----\n\n";
}
float find_net_salary()
{
da = basic * 0.52;
gross_salary = basic + da;
it = gross_salary * 0.30;
net_salary = (basic + da) - it;
return net_salary;
}
int main()
{
Employee emp[10];
int no_of_emp, i;
return 0;
}
#include <iostream>
using namespace std;
class Students
{
string usn, name;
int marks[6][3];
float average[6];
public:
void input();
void display();
};
void Students::input()
{
cout << "USN:";
cin >> usn;
void Students::display()
{
cout << usn << "\t" << name << "\t";
for(float i : average) // This is a range-based for loop in C++, which iterates over
//each element i in the average array.
{
cout << i << " ";
}
cout << endl;
}
int main()
{
int n;
cout << "Enter the number of Students:" << endl;
cin >> n;
Students std[n];
for (int i = 0; i < n; i++)
{
cout << "\nEnter the details of Student-" << i+1 << endl;
std[i].input();
}
cout << "\nDetails of Students:" << endl << "USN\tName\tAverage Marks in 6
Subjects" << endl;
for (int i = 0; i < n; i++)
{
std[i].display();
}
}
3. Polymorphism: Develop a C++ program to create a class called COMPLEX with the
following overloading functions ADD that return a COMPLEX number.
i. ADD (a, s2) – where a is an integer (real part) and s2 is a complex number.
ii. ADD (s1, s2) – where s1 and s2 are complex numbers.
#include <iostream>
using namespace std;
class COMPLEX
{
private: int real, imag;
void COMPLEX::getdata()
{
cout << "Enter the real part and imaginary part: ";
cin >> real >> imag;
}
void COMPLEX::display(COMPLEX s)
{
cout <<s.real<<"+i"<<s.imag<<endl;
}
int main()
{
COMPLEX s1, s2, s3, s4;
int r;
4. Friend function: Create two classes, Class RollsRoyce and Class Lamborghini with
appropriate member variables (Model name, year, price and mileage) and member
functions (read and print). Demonstrate the comparison of two cars w.r.t price and
mileage using friend function.
#include <iostream>
using namespace std;
#include <string>
class RollsRoyce
{
private:
string modelName;
int year;
double price;
double mileage;
public:
void read()
{
cout << "Enter RollsRoyce Model Name: ";
cin >> modelName;
cout << "Enter Year: ";
cin >> year;
cout << "Enter Price: ";
cin >> price;
cout << "Enter Mileage: ";
cin >> mileage;
}
void print()
{
cout << "RollsRoyce Model: " << modelName
<< ", Year: " << year
<< ", Price: $" << price
<< ", Mileage: " << mileage << " MPG" << endl;
}
friend void compareCars(RollsRoyce rr, Lamborghini lambo);
};
class Lamborghini
{
private:
string modelName;
int year;
double price;
double mileage;
public:
void read()
{
cout << "Enter Lamborghini Model Name: ";
cin >> modelName;
cout << "Enter Year: ";
cin >> year;
cout << "Enter Price: ";
cin >> price;
cout << "Enter Mileage: ";
cin >> mileage;
}
void print()
{
cout << "Lamborghini Model: " << modelName
<< ", Year: " << year
<< ", Price: $" << price
<< ", Mileage: " << mileage << " MPG" << endl;
}
friend void compareCars(RollsRoyce rr, Lamborghini lambo);
};
int main()
{
RollsRoyce rr;
Lamborghini lambo;
rr.print();
lambo.print();
compareCars(rr, lambo);
return 0;
}
5. Static and non-static members: Create a vehicle having a non-static data member
registration number and a static data member count. Non-static member functions
setregno() and getregno() are used to get and set the registration number. A static
member function getVehiclecount() is used to return the number of vehicles in the
garage. Use a constructor to increment the vehicle count when a vehicle is created and
the destructor to decrement the count when the vehicle is destroyed.
#include <iostream>
using namespace std;
class Vehicle
{
private:
int regNo; // Non-static data member for registration number
static int count; // Static data member to track vehicle count
public:
// Constructor
Vehicle()
{
count++; // Increment count when a vehicle is created
cout<< "Vehicle created." << endl;
}
// Destructor
~Vehicle()
{
count--; // Decrement count when a vehicle is destroyed
cout << "Vehicle with Registration Number " << regNo << "
destroyed." << endl;
}
int main()
{
// Create vehicles
Vehicle v1;
v1.setRegNo(1001);
Vehicle v2;
v2.setRegNo(1002);
cout << "Vehicle 1 Registration Number: " << v1.getRegNo() << endl;
cout << "Vehicle 2 Registration Number: " << v2.getRegNo() << endl;
cout << "Total Vehicles in Garage: " << Vehicle::getVehicleCount() << endl;
{
Vehicle v3;
v3.setRegNo(1003);
cout << "Vehicle 3 Registration Number: " << v3.getRegNo() << endl;
cout << "Total Vehicles in Garage (after v3 created): " <<
Vehicle::getVehicleCount() << endl;
} // v3 goes out of scope and is destroyed here
return 0;
}
#include<iostream>
using namespace std;
class matrix
{
private:
int m[5][5];
int row;int col;
public:
void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
void display();
};
int main()
{
matrix m1,m2,m3,m4;
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<"the result is\n";
m3.display();
cout<<"subtraction of matrices\n";
cout<<"The result is \n";
m4.display();
}
else
{
cout<<"order of the input matrices is not identical\n";
}
return 0;
}
# include <iostream>
# include <string>
using namespace std;
class PersonData
{
private:
string firstName;
string lastName;
string address;
string city;
string state;
int zip;
string phone;
public:
// constructors
PersonData()
{
firstName = "";
lastName = "";
address = "";
city = "";
state = "";
zip = 0;
phone = "";
}
PersonData(string fn, string ln, string add, string c, string s, int z, string
ph)
{
firstName = fn;
lastName = ln;
address = add;
city = c;
state = s;
zip = z;
phone = ph;
}
// mutator functions
void setFirstName(string fn)
{
firstName = fn;
}
void setState(string s)
{
state = s;
}
void setZip(int z)
{
zip = z;
}
// accessor functions
string getFirstName() const
{
return firstName;
}
// display functions
void displayPersonData() const
{
cout << "First Name:\t\t" << getFirstName() << endl;
cout << "Last Name:\t\t" << getLastName() << endl;
cout << "Address:\t\t" << getAddress() << endl;
cout << "City:\t\t\t" << getCity() << endl;
cout << "State:\t\t\t" << getState() << endl;
cout << "Zip:\t\t\t" << getZip() << endl;
cout << "Phone:\t\t\t" << getPhone() << endl;
}
};
// mutator functions
void setCustomerNumber(int num)
{
customerNumber = num;
}
// accessor functions
int getCustomerNumber() const
{
return customerNumber;
}
void setMailid(string mail)
{
mailid = mail;
}
// display functions
void displayCustomerData() const
{
cout << "Customer Number:\t" << getCustomerNumber() <<
endl;
cout << "Mailid:\t\t" << getMailid() << endl;
displayPersonData();
}
};
int main()
{
CustomerData C1(237,"[email protected]","Santhosh", "Kumar", "31 M.G
Road", "Bengaluru", "Karnataka", 560023, "2563672586");
C1.displayCustomerData();
cout<<"\n\n";
return 0;
}
Student
Test Sports
Result
#include <iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number= a;
}
void put_number(void)
{
cout << "Roll No: “ << roll_number << "\n";
}
};
class test : public student
{
protected:
float cie1, cie2;
public:
void get_marks (float x, float y)
{
cie1 = x;
cie2 = y;
}
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
int main()
{
result student_1;
student_1.get_number (1234);
student_1.get_marks (27.5, 33.0);
student_1.get_score (6.0);
student_1.display();
return 0;
}
9. Pure Virtual Function: Create a Class Shape with member variables dimension and
member function: getdimenstion() to intialize the member variable and pure virtual
function calculateArea(). Create Square class which inherits the shape class and
calculates the area of square. Create Circle class which inherits the shape class and
calculates the area of circle. Demonstrate runtime polymorphism in the main function
to calculate area of square and circle.
#include <iostream>
using namespace std;
class Shape
{
protected:
double dimension;
public:
void getDimension(double dim)
{
dimension = dim;
}
virtual double calculateArea() = 0;
};
int main()
{
Square square;
Circle circle;
square.getDimension(5.0);
cout << "Area of Square: " << square.calculateArea() << endl;
circle.getDimension(3.0);
cout << "Area of Circle: " << circle.calculateArea() << endl;
return 0;
}
10. Templates:
a) Illustrate class templates in a C++ program for the following operations:
Adding two arrays, finding the max and min in an array.
b) Develop a C++ program to sort using bubble sort by applying function
templates.
#include <iostream>
using namespace std;
T findMax()
{
T maxVal = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > maxVal)
{
maxVal = arr[i];
}
}
return maxVal;
}
T findMin()
{
T minVal = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] < minVal)
{
minVal = arr[i];
}
}
return minVal;
}
void display()
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
};
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {5, 4, 3, 2, 1};
int size = 5;
int size1=5;
ArrayOp<int> ar1(arr1, size);
ArrayOp<int> ar2(arr2, size1);
if (size == size1)
{
ArrayOp<int> result = ar1.add(ar2);
cout << "Sum of arrays: ";
result.display();
}
else
{
cout << "Arrays must be of the same size to add." << endl;
}
cout << "Maximum value in Array 1: " << ar1.findMax() << endl;
cout << "Minimum value in Array 1: " << ar1.findMin() << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int intArray[] = {64, 34, 25, 12, 22, 11, 90};
int intSize = sizeof(intArray) / sizeof(intArray[0]);
bubbleSort(intArray, intSize);
bubbleSort(doubleArray, doubleSize);
return 0;
}