0% found this document useful (0 votes)
19 views22 pages

OOP Practicacal

Uploaded by

shravanisd2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views22 pages

OOP Practicacal

Uploaded by

shravanisd2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Q1…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

#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

};

// Default Constructor

Complex::Complex() {

real = 0;

img = 0;

// Overloading input stream (>>)

istream & operator >> (istream &in, Complex &i) {

in >> i.real >> i.img;

return in;
}

// Overloading output stream (<<)

ostream & operator << (ostream &out, const Complex &d) {

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 Complex::operator + (Complex c1) {

Complex temp;

temp.real = real + c1.real;

temp.img = img + c1.img;

return temp;

// Overloading * operator (Multiplication of Complex numbers)

Complex Complex::operator * (Complex c2) {

Complex tmp;

tmp.real = real * c2.real - img * c2.img;

tmp.img = real * c2.img + img * c2.real;

return tmp;

int main() {

Complex C1, C2, C3, C4;

char b; // To check if the user wants to perform another operation

bool continueProgram = true; // Controls the main program loop


while (continueProgram) {

// Input Complex Numbers

cout << "Enter Real and Imaginary part of the Complex Number 1 : \n";

cin >> C1;

cout << "Enter Real and Imaginary part of the Complex Number 2 : \n";

cin >> C2;

bool continueMenu = true; // Controls the menu loop

while (continueMenu) {

// Display Complex Numbers and Menu

cout << "Complex Number 1 : " << C1 << endl;

cout << "Complex Number 2 : " << C2 << endl;

cout << "**********MENU**********" << endl;

cout << "1. Addition of Complex Numbers" << endl;

cout << "2. Multiplication of Complex Numbers" << endl;

cout << "3. Exit\n";

int choice;

cout << "Enter your choice from above MENU (1 to 3) : ";

cin >> choice;

// Perform the selected operation

if (choice == 1) {

C3 = C1 + C2;

cout << "Addition : " << C3 << endl;

else if (choice == 2) {

C4 = C1 * C2;

cout << "Multiplication : " << C4 << endl;

else {
cout << "Thanks for using this program!!\n";

continueProgram = false;

continueMenu = false;

// Ask user if they want to perform another operation

cout << "Do you want to perform another operation (y/n)? ";

cin >> b;

if (b != 'y' && b != 'Y') {

continueMenu = false;

cout << "Thanks for using this program!!\n";

return 0;

Short

#include<iostream>

using namespace std;

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 operator + (complex & c)

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();

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 as well as exception handling.
#include<iostream>

#include<string>

using namespace std;

class StudData;

class Student {

string name;

int roll_no;

string cls;

string division;

string dob;

string bloodgroup;

static int count;

public:

Student() : roll_no(0), division(""), dob("dd/mm/yyyy"), bloodgroup("") {}

~Student() {}

static int getCount() {

return count;

void getData(StudData*);

void dispData(StudData*) const;

};

class StudData {

string caddress;

long int telno;

long int dlno;


public:

StudData() : telno(0), dlno(0) {}

~StudData() {}

void getStudData() {

cout << "Enter Contact Address: ";

cin.ignore();

getline(cin, caddress);

cout << "Enter Telephone Number: ";

cin >> telno;

cout << "Enter Driving License Number: ";

cin >> dlno;

void dispStudData() const {

cout << "Contact Address: " << caddress << endl;

cout << "Telephone Number: " << telno << endl;

cout << "Driving License Number: " << dlno << endl;

};

int Student::count = 0;

void Student::getData(StudData* st) {

cout << "Enter Student Name: ";

cin.ignore();

getline(cin, name);

cout << "Enter Roll Number: ";

cin >> roll_no;

cout << "Enter Class: ";


cin >> cls;

cout << "Enter Division: ";

cin >> division;

cout << "Enter Date of Birth: ";

cin.ignore(); // Clear any leftover newline

getline(cin, dob);

cout << "Enter Blood Group: ";

cin >> bloodgroup;

st->getStudData();

count++;

void Student::dispData(StudData* st1) const {

cout << "Student Name: " << name << endl;

cout << "Roll Number: " << roll_no << endl;

cout << "Class: " << cls << endl;

cout << "Division: " << division << endl;

cout << "Date of Birth: " << dob << endl;

cout << "Blood Group: " << bloodgroup << endl;

st1->dispStudData();

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++;

cout << "Do you want to add another student (y/n): ";

cin >> ch;

} while (ch == 'y' || ch == 'Y');

for (int i = 0; i < n; i++) {

cout << "---------------------------------------------------------------" << endl;

stud1[i]->dispData(stud2[i]);

cout << "---------------------------------------------------------------" << endl;

cout << "Total Students: " << Student::getCount() << endl;

cout << "---------------------------------------------------------------" << endl;

for (int i = 0; i < n; 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>

using namespace std;

class Publication

string title;

float price;

public:

Publication(string t = "", float p = 0.0)

title = t;

price = p;

void setTitle(const string & t)

title = t;

void setPrice(float p)

price = p;

void display() const

cout << "Title: " << title << "\nPrice: " << price << endl;
}

};

class Book : public Publication

int pageCount;

public:

Book(int pc = 0)

pageCount = pc;

void setPageCount(int pc)

pageCount = pc;

void display() const

Publication::display();

cout << "Page Count: " << pageCount << endl;

};

class Tape : public Publication

float playingTime;
public:

Tape(float pt = 0.0)

playingTime = pt;

void setPlayingTime(float pt)

playingTime = pt;

void display() const

Publication::display();

cout << "Playing Time: " << playingTime << " minutes" << endl;

};

void inputPublication(Publication & pub, const string & type)

string title;

float price;

cout << "Enter " << type << " Title: ";

cin >> title;

pub.setTitle(title);

cout << "Enter " << type << " Price: ";

cin >> price;

pub.setPrice(price);
}

int main() {

Book book;

Tape tape;

try {

inputPublication(book, "Book");

int pageCount;

cout << "Enter Book Page Count: ";

cin >> pageCount;

book.setPageCount(pageCount);

inputPublication(tape, "Tape");

float playingTime;

cout << "Enter Tape Playing Time: ";

cin >> playingTime;

tape.setPlayingTime(playingTime);

cout << "\nBook Information:\n";

book.display();

cout << "\nTape Information:\n";

tape.display();

catch(const char *e)

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>

using namespace std;

int main() {

ofstream outFile("example.txt");

if (outFile)

outFile << "this test file.\nFile handling in C++ is fun!";

outFile.close();

else

cerr << "Error opening file for writing.\n";

return 1;

ifstream inFile("example.txt");

if (inFile)

string line;

while (getline(inFile, line))

cout << line << endl;

inFile.close();

}
else

cerr << "Error opening file for reading.\n";

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>

using namespace std;

#define SIZE 10

template<class T>

void sel(T A[], int n) {

int i, j, min;

T temp;

for (i = 0; i < n - 1; i++) {

min = i;

for (j = i + 1; j < n; j++) {

if (A[j] < A[min]) min = j;

temp = A[i];

A[i] = A[min];

A[min] = temp;
}

// Print sorted array

cout << "\nSorted array:";

for (i = 0; i < n; i++) {

cout << " " << A[i];

cout << endl;

int main() {

int A[SIZE], n, ch;

float B[SIZE];

do {

cout << "\n* * * * SELECTION SORT SYSTEM * * * * *";

cout << "\n1. Integer Values";

cout << "\n2. Float Values";

cout << "\n3. Exit";

cout << "\nEnter your choice: ";

cin >> ch;

switch (ch) {

case 1:

cout << "\nEnter number of integer elements: ";

cin >> n;

cout << "\nEnter integer elements:\n";

for (int i = 0; i < n; i++) {

cin >> A[i];

sel(A, n);
break;

case 2:

cout << "\nEnter number of float elements: ";

cin >> n;

cout << "\nEnter float elements:\n";

for (int i = 0; i < n; i++) {

cin >> B[i];

sel(B, n);

break;

case 3:

cout << "Exiting program.\n";

break;

default:

cout << "Invalid choice, try again.\n";

} while (ch != 3);

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>

using namespace std;

class student {

public:

int rollno;

string name;

char dob[15];

bool operator==(const student &s) { return rollno == s.rollno; }

bool operator<(const student &s) { return rollno < s.rollno; }

friend ostream& operator<<(ostream &out, const student &s);

friend istream& operator>>(istream &in, student &s);

};

ostream& operator<<(ostream &out, const student &s) {

out << "\n\t" << s.rollno << "\t" << s.name << "\t" << s.dob;

return out;

istream& operator>>(istream &in, student &s) {

cout << "\nEnter Roll No: ";

in >> s.rollno;

cout << "Enter Name: ";

in >> s.name;

cout << "Enter DOB: ";

in >> s.dob;

return in;
}

void print(const vector<student> &students) {

cout << "\n\tRoll No\tName\tDOB";

for (const auto &s : students) cout << s;

int main() {

vector<student> students;

int choice;

do {

cout << "\n1. Create\n2. Display\n3. Insert\n4. Delete\n5. Search\n6. Sort\n7. Quit\nChoice: ";

cin >> choice;

switch(choice) {

case 1: {

int n;

cout << "Enter number of students: ";

cin >> n;

students.resize(n);

for (int i = 0; i < n; ++i) cin >> students[i];

break;

case 2:

print(students);

break;

case 3: {

student newStudent;

cin >> newStudent;

students.push_back(newStudent);
break;

case 4: {

int rollno;

cout << "Enter Roll No to delete: ";

cin >> rollno;

auto it = find_if(students.begin(), students.end(), [rollno](const student &s) {

return s.rollno == rollno;

});

if (it != students.end()) students.erase(it);

else cout << "Not found.\n";

break;

case 5: {

int rollno;

cout << "Enter Roll No to search: ";

cin >> rollno;

auto it = find_if(students.begin(), students.end(), [rollno](const student &s) {

return s.rollno == rollno;

});

if (it != students.end()) cout << *it;

else cout << "Not found.\n";

break;

case 6:

sort(students.begin(), students.end());

print(students);

break;

case 7:

cout << "Exiting program.\n";

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>

using namespace std;

int main()

typedef map<string,int> mapType;

mapType populationMap;

populationMap.insert(pair<string, int>("Maharashtra", 7026357));

populationMap.insert(pair<string, int>("Rajasthan", 6578936));

populationMap.insert(pair<string, int>("Karanataka", 6678993));

populationMap.insert(pair<string, int>("Punjab", 5789032));

populationMap.insert(pair<string, int>("West Bengal", 6676291));

mapType::iterator iter;

cout<<"========Population of states in India==========\n";

cout<<"\n Size of populationMap"<<populationMap.size()<<"\n";


string state_name;

cout<<"\n Enter name of the state :";

cin>>state_name;

iter = populationMap.find(state_name);

if( iter!= populationMap.end() )

cout<<state_name<<" 's population is "

<<iter->second ;

else

cout<<"Key is not populationMap"<<"\n";

populationMap.clear();

You might also like