OOP Practicacal
OOP Practicacal
Implement the
following
1. Constructor (including a default constructor which creates the complex number 0+0i).
4. Overload operators << and >> to print and read Complex Numbers
#include<iostream>
class Complex {
double real;
double img;
public:
friend istream & operator >> (istream &, Complex &); // Input
friend ostream & operator << (ostream &, const Complex &); // Output
};
// Default Constructor
Complex::Complex() {
real = 0;
img = 0;
return in;
}
if (d.img >= 0)
out << d.real << " + " << d.img << "i" << endl;
else
out << d.real << " - " << -d.img << "i" << endl; // Handle negative imaginary part
return out;
Complex temp;
return temp;
Complex tmp;
return tmp;
int main() {
cout << "Enter Real and Imaginary part of the Complex Number 1 : \n";
cout << "Enter Real and Imaginary part of the Complex Number 2 : \n";
while (continueMenu) {
int choice;
if (choice == 1) {
C3 = C1 + C2;
else if (choice == 2) {
C4 = C1 * C2;
else {
cout << "Thanks for using this program!!\n";
continueProgram = false;
continueMenu = false;
cout << "Do you want to perform another operation (y/n)? ";
cin >> b;
continueMenu = false;
return 0;
Short
#include<iostream>
class complex
int real;
int img;
public:
complex()
};
complex(int real,int img)
this->real=real;
this->img=img;
void display()
cout<<real<<"+ i"<<img<<endl;
complex ans;
ans.real=real + c.real;
ans.img=img + c.img;
return ans;
};
int main()
complex c1(3,4);
complex c2(4,5);
complex c3 = c1+c2;
c3.display();
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 as well as exception handling.
#include<iostream>
#include<string>
class StudData;
class Student {
string name;
int roll_no;
string cls;
string division;
string dob;
string bloodgroup;
public:
~Student() {}
return count;
void getData(StudData*);
};
class StudData {
string caddress;
~StudData() {}
void getStudData() {
cin.ignore();
getline(cin, caddress);
cout << "Driving License Number: " << dlno << endl;
};
int Student::count = 0;
cin.ignore();
getline(cin, name);
getline(cin, dob);
st->getStudData();
count++;
st1->dispStudData();
int main() {
Student* stud1[100];
StudData* stud2[100];
int n = 0;
char ch;
do {
stud1[n]->getData(stud2[n]);
n++;
cout << "Do you want to add another student (y/n): ";
stud1[i]->dispData(stud2[i]);
delete stud1[i];
delete stud2[i];
return 0;
3)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.
#include <iostream>
#include <string>
class Publication
string title;
float price;
public:
title = t;
price = p;
title = t;
void setPrice(float p)
price = p;
cout << "Title: " << title << "\nPrice: " << price << endl;
}
};
int pageCount;
public:
Book(int pc = 0)
pageCount = pc;
pageCount = pc;
Publication::display();
};
float playingTime;
public:
Tape(float pt = 0.0)
playingTime = pt;
playingTime = pt;
Publication::display();
cout << "Playing Time: " << playingTime << " minutes" << endl;
};
string title;
float price;
cout << "Enter " << type << " Title: ";
pub.setTitle(title);
cout << "Enter " << type << " Price: ";
pub.setPrice(price);
}
int main() {
Book book;
Tape tape;
try {
inputPublication(book, "Book");
int pageCount;
book.setPageCount(pageCount);
inputPublication(tape, "Tape");
float playingTime;
tape.setPlayingTime(playingTime);
book.display();
tape.display();
cout<<"exception occure"<<e<<endl;
}
return 0;
4)Write a C++ program that creates an output file, writes information to it, closes the file, open it
again as an input file and read the information from the file.
#include <iostream>
#include <fstream>
int main() {
ofstream outFile("example.txt");
if (outFile)
outFile.close();
else
return 1;
ifstream inFile("example.txt");
if (inFile)
string line;
inFile.close();
}
else
return 1;
return 0;
5)Write a function template for selection sort that inputs, sorts and outputs an integer array and a
float array.
#include<iostream>
#define SIZE 10
template<class T>
int i, j, min;
T temp;
min = i;
temp = A[i];
A[i] = A[min];
A[min] = temp;
}
int main() {
float B[SIZE];
do {
switch (ch) {
case 1:
cin >> n;
sel(A, n);
break;
case 2:
cin >> n;
sel(B, n);
break;
case 3:
break;
default:
return 0;
6)Write C++ program using STL for sorting and searching user defined records such as personal
records (Name, DOB, Telephone number etc) using vector container.
#include <iostream>
#include <vector>
#include <algorithm>
class student {
public:
int rollno;
string name;
char dob[15];
};
out << "\n\t" << s.rollno << "\t" << s.name << "\t" << s.dob;
return out;
in >> s.rollno;
in >> s.name;
in >> s.dob;
return in;
}
int main() {
vector<student> students;
int choice;
do {
cout << "\n1. Create\n2. Display\n3. Insert\n4. Delete\n5. Search\n6. Sort\n7. Quit\nChoice: ";
switch(choice) {
case 1: {
int n;
cin >> n;
students.resize(n);
break;
case 2:
print(students);
break;
case 3: {
student newStudent;
students.push_back(newStudent);
break;
case 4: {
int rollno;
});
break;
case 5: {
int rollno;
});
break;
case 6:
sort(students.begin(), students.end());
print(students);
break;
case 7:
break;
}
} while(choice != 7);
return 0;
7) 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
#include<iostream>
#include<map>
#include<string>
int main()
mapType populationMap;
mapType::iterator iter;
cin>>state_name;
iter = populationMap.find(state_name);
<<iter->second ;
else
populationMap.clear();