OOP Corrected Practicals
OOP Corrected Practicals
Implement a class Complex which represents the Complex Number data type. Implement the following
Constructor (including a default constructor which creates the complex number 0+0i).
Overload operator+ to add two complex numbers.
Overload operator* to multiply two complex numbers.
Overload operators << and >> to print and read Complex Numbers
CODE:
#include <iostream>
using namespace std;
class Complex {
double real;
double img;
public:
Complex(); // Default Constructor
friend istream& operator >> (istream&, Complex&); // Input
friend ostream& operator << (ostream&, const Complex&); // Output
Complex operator + (Complex); // Addition
Complex operator * (Complex); // Multiplication
};
Complex::Complex() {
real = 0;
img = 0;
}
C3 = C1 + C2;
cout << "Addition: " << C3 << endl;
C4 = C1 * C2;
cout << "Multiplication: " << C4 << endl;
return 0;
}
Experiment No : 1
Output :
Experiment No : 2
/*
Experiment Number 2 : Develop a program in C++ to create a database of student’s information
system containing the following information: Name, Roll number, Class, Division, Date of Birth, Blood
group, Contactaddress, Telephone number, Driving license no. and other. Construct the database with
suitable member functions. Make use of constructor, default constructor, copy constructor, destructor,
static member functions, friend class, this pointer, inline code and dynamic memory allocation
operators-new and delete aswell as exception handling.
*/
CODE:
#include<iostream>
#include<string>
using namespace std;
class Student {
string name;
int roll_no;
string cls;
char* division;
string dob;
char* bloodgroup;
static int count;
public:
// Default Constructor
Student() {
name = "";
roll_no = 0;
cls = "";
division = new char[10]; // Allocate memory for division
dob = "dd/mm/yyyy";
bloodgroup = new char[4]; // Allocate memory for blood group
}
// Destructor
~Student() {
delete[] division; // Deallocate memory for division
delete[] bloodgroup; // Deallocate memory for blood group
}
class StudData {
string caddress;
long int* telno;
long int* dlno;
public:
// Default Constructor
StudData() {
caddress = "";
telno = new long; // Allocate memory for telephone number
dlno = new long; // Allocate memory for driving license number
}
// Destructor
~StudData() {
delete telno; // Deallocate memory for telephone number
delete dlno; // Deallocate memory for driving license number
}
int main() {
Student* stud1;
StudData* stud2;
return 0;
}
Experiment No : 2
Output :
Experiment No : 3
/*
Imagine a publishing company which does marketing for book and audiocassette versions. Create a
class publication that stores the title (a string) and price (type float) of a publication. From this class
derive two classes: book, which adds a page count(type int), and tape, which adds a playing time in
minutes(type float). Write a program that instantiates the book and tape classes, allows user to enter
data and displays the data members.If an exception is caught, replace all the data member values with
zero values.
*/
CODE
#include<iostream>
using namespace std;
public:
// Function to add publication details
void add() {
cout << "\nEnter the Publication information: " << endl;
cout << "Enter Title of the Publication: ";
cin >> title;
cout << "Enter Price of Publication: ";
cin >> price;
}
// Function to display publication details
void display() {
cout << "\nTitle of Publication: " << title;
cout << "\nPublication Price: " << price;
}
};
// Declaring class book which inherits class publication in public mode.
class book : public publication {
private:
int page_count;
public:
// Function to add book details
void add_book() {
try {
add();
cout << "Enter Page Count of Book: ";
cin >> page_count;
if (page_count <= 0) {
throw page_count;
}
}
catch (...) {
cout << "\nInvalid Page Count!!!";
page_count = 0;
}
}
public:
// Function to add tape details
void add_tape() {
try {
add();
cout << "Enter Play Duration of the Tape (in minutes): ";
cin >> play_time;
if (play_time <= 0) {
throw play_time;
}
}
catch (...) {
cout << "\nInvalid Play Time!!!";
play_time = 0;
}
}
int main() {
book b1; // Object of class book
tape t1; // Object of class tape
return 0;
}
Experiment No : 3
Output :
Experiment No : 4
/*
Write a C++ program that creates an output file, writes information to it, closes the file, open it again
as aninput file and read the information from the file.
*/
CODE:-
#include <iostream>
#include <fstream>
using namespace std;
class Employee {
string Name;
int ID;
double salary;
public:
// Function to accept employee details
void accept() {
cout << "\nName: ";
cin >> Name;
cout << "Id: ";
cin >> ID;
cout << "Salary: ";
cin >> salary;
}
int main() {
Employee o[5];
fstream file;
int i, n;
return 0;
}
Experiment No : 4
Output :
Experiment No : 5
/*
Write a function template for selection sort that inputs, sorts and outputs an integer array anda
float array.
*/
CODE:
#include<iostream>
using namespace std;
int main() {
int A[SIZE]; // Array to hold integers
float B[SIZE]; // Array to hold floats
int i;
return 0;
}
Experiment No : 5
Output :
Experiment No : 6
/*
Write C++ Program using STL for sorting and searching user defined records such as item records
using vector container.
*/
CODE:-
// Function declarations
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2);
int main() {
int ch;
do {
// Displaying menu
cout << "\n* * * * * Menu * * * * *";
cout << "\n1. Insert";
cout << "\n2. Display";
cout << "\n3. Search";
cout << "\n4. Sort";
cout << "\n5. Delete";
cout << "\n6. Exit";
cout << "\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort(o1.begin(), o1.end(), compare); // Sorting based on cost
cout << "\n\n Sorted on Cost: ";
display();
break;
case 5:
dlt();
break;
case 6:
exit(0);
}
} while (ch != 6);
return 0;
}
CODE:-
#include <iostream>
#include <map>
#include <string>
int main() {
// Using float for population values
typedef map<string, float> mapType;
mapType populationMap;
// Correcting the insertions
populationMap.insert(make_pair("Maharashtra", 125));
populationMap.insert(make_pair("Uttar Pradesh", 225));
populationMap.insert(make_pair("Bihar", 120));
populationMap.insert(make_pair("West Bengal", 100));
populationMap.insert(make_pair("Madhya Pradesh", 90));
populationMap.insert(make_pair("Tamil Nadu", 80));
populationMap.insert(make_pair("Rajasthan", 78));
populationMap.insert(make_pair("Andhra Pradesh", 53));
populationMap.insert(make_pair("Odisha", 47));
populationMap.insert(make_pair("Kerala", 38));
populationMap.insert(make_pair("Telangana", 37));
populationMap.insert(make_pair("Assam", 35));
populationMap.insert(make_pair("Jharkhand", 38));
populationMap.insert(make_pair("Karnataka", 68));
populationMap.insert(make_pair("Gujarat", 70));
populationMap.insert(make_pair("Punjab", 31));
populationMap.insert(make_pair("Chhattisgarh", 30));
populationMap.insert(make_pair("Haryana", 29));
populationMap.insert(make_pair("UT Delhi", 19));
populationMap.insert(make_pair("UT Jammu and Kashmir", 14));
populationMap.insert(make_pair("Uttarakhand", 12));
populationMap.insert(make_pair("Himachal Pradesh", 8));
populationMap.insert(make_pair("Tripura", 4));
populationMap.insert(make_pair("Meghalaya", 4));
populationMap.insert(make_pair("Manipur", 3));
populationMap.insert(make_pair("Nagaland", 2));
populationMap.insert(make_pair("Goa", 2));
populationMap.insert(make_pair("Arunachal Pradesh", 2));
populationMap.insert(make_pair("UT Puducherry", 2));
populationMap.insert(make_pair("Mizoram", 1));
populationMap.insert(make_pair("UT Chandigarh", 1));
populationMap.insert(make_pair("Sikkim", 1));
populationMap.insert(make_pair("UT Dadra and Nagar Haveli and Daman and Diu", 1));
populationMap.insert(make_pair("UT Andaman and Nicobar Islands", 1));
populationMap.insert(make_pair("UT Lakshadweep", 0.0003));
populationMap.insert(make_pair("UT Ladakh", 0.00006));
if (iter != populationMap.end()) {
cout << state << "'s population is " << iter->second << " million\n";
} else {
cout << "State is not in the populationMap\n";
}