Practical oop imp
Practical oop imp
Implement a class Complex which represents the Complex Number data type. Implement the following
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.
Code:
#include <iostream>
double real;
double img;
public:
};
// Default Constructor
Complex::Complex() {
real = 0;
img = 0;
return in;
out << d.real << " + " << d.img << "i";
return out;
// Overloading + operator
Complex temp;
return temp;
// Overloading * operator
Complex tmp;
return tmp;
int main() {
int flag = 1;
char b;
while (flag == 1) {
cout << "Enter Real and Imaginary part of the Complex Number 1: \n";
cout << "Enter Real and Imaginary part of the Complex Number 2: \n";
int f = 1;
while (f == 1) {
cout << "Complex Number 1: " << C1 << endl;
int a;
cout << "Enter your choice from above MENU (1 to 3): ";
cin >> a;
if (a == 1) {
C3 = C1 + C2;
cout << "Do you want to perform another operation (y/n): ";
cin >> b;
if (b == 'y' || b == 'Y') {
f = 1;
} else {
flag = 0;
f = 0;
} else if (a == 2) {
C4 = C1 * C2;
cout << "Do you want to perform another operation (y/n): ";
cin >> b;
if (b == 'y' || b == 'Y') {
f = 1;
} else {
flag = 0;
f = 0;
}
} else {
flag = 0;
f = 0;
return 0;
Output:
Practical
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, Contact address, 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 as well as exception handling.
Code:
#include<iostream>
#include<string.h>
class StudData;
class Student{
string name;
int roll_no;
string cls;
char* division;
string dob;
char* bloodgroup;
public:
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
delete division;
delete[] bloodgroup;
return count;
void getData(StudData*);
void dispData(StudData*);
};
class StudData{
string caddress;
public:
StudData()
caddress="";
telno=new long;
dlno=new long;
~StudData()
delete telno;
delete dlno;
}
void getStudData()
cin.get();
getline(cin,caddress);
cin>>*telno;
cin>>*dlno;
void dispStudData()
};
getline(cin,name);
cin>>roll_no;
cin.get();
getline(cin,cls);
cin>>division;
cin.get();
getline(cin,dob);
cin>>bloodgroup;
st->getStudData();
count++;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
st1->dispStudData();
int Student::count;
int main()
Student* stud1[100];
StudData* stud2[100];
int n=0;
char ch;
do
stud1[n]=new Student;
stud2[n]=new StudData;
stud1[n]->getData(stud2[n]);
n++;
cin.get();
for(int i=0;i<n;i++)
cout<<"---------------------------------------------------------------"<<endl;
stud1[i]->dispData(stud2[i]);
cout<<"---------------------------------------------------------------"<<endl;
cout<<endl<<"---------------------------------------------------------------"<<endl;
for(int i=0;i<n;i++)
delete stud1[i];
delete stud2[i];
return 0;
}
Output:
Practical
Imagine a publishing company which does marketing for book and audio cassette versions. Create a class
publication that stores the title (a string) and price (type float) of publications. 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 class, 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>
#include <stdio.h>
private:
string title;
float price;
public:
void add()
getline(cin, title);
void display()
};
class book : public publication // declaring class book which inherits class publication in public mode.
{
private:
int page_count;
public:
void add_book()
try
add();
if (page_count <= 0)
throw page_count;
catch (...)
page_count = 0;
void display_book()
display();
};
class tape : public publication // declaring class tape which inherits class publication in public mode
private:
float play_time;
public:
void add_tape()
try
add();
if (play_time <= 0)
throw play_time;
catch (...)
play_time = 0;
void display_tape()
display();
cout << "\nPlay Time : " << play_time << " min";
};
int main()
do
{
cout << "\n* * * * * PUBLICATION DATABASE SYSTEM * * * * *";
switch (ch)
case 1:
b1[b_count].add_book();
break;
case 2:
t1[t_count].add_tape();
break;
case 3:
b1[j].display_book();
break;
case 4:
t1[j].display_tape();
break;
case 5:
return 0;
Output:
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Code:
#include<iostream>
#include<fstream>
class test
public:
void writedata();
void readdata();
};
void test::writedata()
fstream fp;
char ch;
fp.open("it.txt", ios::out);
cout << "Enter characters to write to the file (end input with a dot '.'): ";
fp.put(ch);
fp.close();
void test::readdata()
fstream fp;
char ch;
fp.open("it.txt", ios::in);
if (!fp)
return;
// Read and display characters until the end of the file is reached
while (fp.get(ch))
fp.close();
int main()
test ob;
int ch;
do
switch(ch)
case 1:
ob.writedata();
break;
case 2:
ob.readdata();
break;
default:
break;
return 0;
Output:
1.Write
2.Read
Enter your choice= 1
abcd
.
1.Write
2.Read
Enter your choice= 2
abcd
1.Write
2.Read
Enter your choice= 3
Practical
Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array.
Code:
#include <iostream>
int minIndex = i;
minIndex = j;
if (minIndex != i)
T temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
// Template function to display an array
int main()
// Get the size of the integer array and input the values
cout << "Enter the number of elements in the integer array: ";
int intArray[n1];
cout << "Enter " << n1 << " integer values: ";
displayArray(intArray, n1);
displayArray(intArray, n1);
// Get the size of the float array and input the values
cout << "Enter the number of elements in the float array: ";
float floatArray[n2];
cout << "Enter " << n2 << " float values: ";
displayArray(floatArray, n2);
displayArray(floatArray, n2);
return 0;
Output
Practical
Write C++ program using STL for sorting and searching user defined records such as personal records
(Name, DOB, Telephone number etc) using vector container.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct PersonalRecord {
string name;
string phoneNumber;
};
cout << "Name: " << record.name << ", DOB: " << record.dob << ", Phone: " << record.phoneNumber <<
endl;
// Function to search for a record by name (using linear search for simplicity)
if (record.name == name) {
return &record;
int main() {
vector<PersonalRecord> records;
displayRecord(record);
// Sorting by Name
displayRecord(record);
}
// Sorting by DOB
displayRecord(record);
string searchName;
if (foundRecord != nullptr) {
displayRecord(*foundRecord);
} else {
return 0;
}
Output:
Practical
Write C++ program using STL for sorting and searching user defined records such as Item records (Item
code, name, cost, quantity etc) using vector container.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct ItemRecord {
int itemCode;
string itemName;
double itemCost;
int itemQuantity;
};
if (item.itemCode == itemCode) {
int main() {
vector<ItemRecord> items;
displayItem(item);
displayItem(item);
displayItem(item);
int searchCode;
if (foundItem != nullptr) {
displayItem(*foundItem);
} else {
cout << "\nItem with code " << searchCode << " not found." << endl;
return 0;
}
Output:
Practical
Write a program in C++ to use map associative container. The keys will be the names of states and the values
will be the populations of the states. When the program runs, the user is prompted to type the name of a
state. The program then looks in the map, using the state name as an index and returns the population of the
state.
Code:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// Create a map where key is state name (string) and value is population (int)
map<string, int> statePopulation;
// Search for the state in the map and display the population
map<string, int>::iterator it = statePopulation.find(state);
if (it != statePopulation.end()) {
// If the state is found in the map, print the population
cout << "The population of " << state << " is: " << it->second << endl;
} else {
// If the state is not found in the map
cout << "State not found in the map." << endl;
}
return 0;
}
Output: