Oop Practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

//Name:prasad kakade

// Class: SE B
// Roll No: COSB 67
// Name: OOPassign1.cpp
// Title: Arithmetic operations on complex numbers using operator
overloading
INPUT:

#include <iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex()
{
x=0;
y=0;
}
complex operator+(complex);
complex operator*(complex);
friend istream &operator >>(istream &input,complex &t)
{
cout<<"Enter the real part = ";
input>>t.x;
cout<<"Enter the imaginary part = ";
input>>t.y;
}
friend ostream &operator <<(ostream &output,complex &t)
{
output<<t.x<<"+"<<t.y<<"i\n";
}
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
complex complex::operator*(complex c)
{
complex temp2;
temp2.x=(x*c.x)-(y*c.y);
temp2.y=(y*c.x)+(x*c.y);
return (temp2);
}
int main()
{
complex c1,c2,c3,c4;
cout<<"Default constructor value = \n";
cout<<c1;
cout<<"\n(Enter the 1st number)\n";
cin>>c1;
cout<<"\n(Enter the 2nd number)\n";
cin>>c2;
c3=c1+c2;
c4=c1*c2;
cout<<"\nThe first number is: ";
cout<<c1;
cout<<"\nThe second number is: ";
cout<<c2;
cout<<"\nThe addition is: ";
cout<<c3;
cout<<"\nThe multiplication is: ";
cout<<c4;
return 0;
}
OUTPUT:

Default constructor value =


0+0i

(Enter the 1st number)


Enter the real part = 2
Enter the imaginary part = 4

(Enter the 2nd number)


Enter the real part = 4
Enter the imaginary part = 8

The first number is: 2+4i

The second number is: 4+8i

The addition is: 6+12i

The multiplication is: -24+32i


//Name:Prasad kakade
// Class: SE B
// Roll No: COSB 67
// Name: OOPassign2.cpp
// Title: Personnel information system using constructor, destructor, static
member functions, friend class, this pointer, inline code and dynamic memory
allocation
INPUT:

#include <iostream>
#include <cstring>
using namespace std;
class StudData;
class Student
{
string name;
string dob;
string bloodgroup;
int height;
int weight;
string insurancePolicyNumber;
static int count;
char* division;
public:
Student():
name(" "), dob("dd/mm/yyyy"), bloodgroup(" "), height(0),
weight(0), insurancePolicyNumber(" ")
{
division = new char[10];
}
Student(const Student& s1):
name(s1.name), dob(s1.dob), bloodgroup(s1.bloodgroup),
height(s1.height),
weight(s1.weight),insurancePolicyNumber(s1.insurancePolicyNumb
er)
{
division = new char[strlen(s1.division) + 1];
strcpy(division, s1.division);
}
~Student()
{
delete[] division;
}
static int getCount()
{
return count;
}
void getData(StudData* st);
void dispData(StudData* st1);
};
class StudData
{
string caddress;
long int* telno;
long int* dlno;
friend class Student;
public:
StudData():
caddress(" ")
{
telno = new long;
dlno = new long;
}
StudData(const StudData& s2):
caddress(s2.caddress) {
telno = new long(*s2.telno);
dlno = new long(*s2.dlno);
}
~StudData() {
delete telno;
delete dlno;
}
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()
{
cout << "Contact Address : " << caddress << endl;
cout << "Telephone Number : " << *telno << endl;
cout << "Driving License Number : " << *dlno << endl;
}
};
inline void Student::getData(StudData* st)
{
cout << "Enter Student Name : ";
cin.ignore();
getline(cin, name);
cout << "Enter Date of Birth : ";
cin >> dob;
cout << "Enter Blood Group : ";
cin >> bloodgroup;
cout << "Enter Height (in cm): ";
cin >> height;
cout << "Enter Weight (in kg): ";
cin >> weight;
cout << "Enter Insurance Policy Number : ";
cin >> insurancePolicyNumber;
delete[] division;
division = new char[10];
cout << "Enter Division : ";
cin >> division;
st->getStudData();
count++;
}
inline void Student::dispData(StudData* st1)
{
cout << "Student Name : " << name << endl;
cout << "Date of Birth : " << dob << endl;
cout << "Blood Group : " << bloodgroup << endl;
cout << "Height: " << height << " cm" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "Insurance Policy Number: " << insurancePolicyNumber <<
endl;
st1->dispStudData();
}
int Student::count = 0;
int main()
{
Student* stud1[100];
StudData* stud2[100];
int n = 0;
char ch;
do
{
stud1[n] = new Student;
stud2[n] = new StudData;
cout << "Enter details of Student " << n + 1 << endl;
stud1[n]->getData(stud2[n]);
cout << "-------------------------------------------------------------" << endl;
stud1[n]->dispData(stud2[n]);
cout << "-------------------------------------------------------------" << endl;
cout << "Do you want to add another student (Y/N) : ";
cin >> ch;
cin.ignore();
n++;
}
while (ch == 'y' || ch == 'Y');
cout << "-------------------------------------------------------------" << endl;
for (int i = 0; i < n; i++) {
stud1[i]->dispData(stud2[i]);
cout << "-------------------------------------------------------------" << endl;
}
cout << "Total Students : " << Student::getCount() << endl;
cout << "-------------------------------------------------------------" << endl;
Student s;
stud1[n] = new Student(*stud1[0]);
stud2[n] = new StudData(*stud2[0]);
cout << "Use of copy constructor Student " << n + 1 << endl;
stud1[n]->dispData(stud2[n]);
for (int i = 0; i <= n; i++)
{
delete stud1[i];
delete stud2[i];
}
return 0;
}
OUTPUT:

Enter details of Student 1


Enter Student Name : JESSICA JOSEPH
Enter Date of Birth : 22/10/2008
Enter Blood Group : A+
Enter Height (in cm): 145
Enter Weight (in kg): 45
Enter Insurance Policy Number : POL123456
Enter Division : B
Enter Contact Address : 456 Elm Street
Enter Telephone Number : 7891234
Enter Driving License Number : 101112
-------------------------------------------------------------
Student Name : JESSICA JOSEPH
Date of Birth : 22/10/2008
Blood Group : A+
Height: 145 cm
Weight: 45 kg
Insurance Policy Number: POL123456
Contact Address : 456 Elm Street
Telephone Number : 7891234
Driving License Number : 101112
-------------------------------------------------------------
Do you want to add another student (Y/N) : Y
Enter details of Student 2
Enter Student Name : JOHN DOE
Enter Date of Birth : 15/05/2009
Enter Blood Group : B-
Enter Height (in cm): 150
Enter Weight (in kg): 50
Enter Insurance Policy Number : POL654321
Enter Division : A
Enter Contact Address : 789 Maple Avenue
Enter Telephone Number : 4567891
Enter Driving License Number : 131415
-------------------------------------------------------------
Student Name : JOHN DOE
Date of Birth : 15/05/2009
Blood Group : B-
Height: 150 cm
Weight: 50 kg
Insurance Policy Number: POL654321
Contact Address : 789 Maple Avenue
Telephone Number : 4567891
Driving License Number : 131415
-------------------------------------------------------------
Total Students : 2
-------------------------------------------------------------
//Name:prasad kakade
// Class: SE B
// Roll No: SBOC 67
// Name: OOPassign3.cpp
// Title: Creating a class which uses the concept of inheritance, displays data
and data members and uses the concept of exception handling

INPUT:

#include <iostream>
#include <string>
using namespace std;
class Publication
{
private:
string title;
float price;
public:
void add()
{
cout << "\nEnter the publication information:- " << endl;
cout << "Enter Title of the Publication : ";
cin.ignore();
getline(cin, title);
cout << "Enter Price of Publication : ";
cin >> price;
}
void display() const
{
cout << "\n--------------------------------------------------";
cout << "\nTitle of Publication : " << title;
cout << "\nPublication Price : " << price;
}
};
class Book : public Publication
{
private:
int page_count;
public:
void add_book()
{
try
{
add();
cout << "Enter Page Count of Book : ";
cin >> page_count;
if (page_count <= 0)
{
throw page_count;
}
}
catch (int)
{
cout << "\nInvalid Page Count!!!";
page_count = 0;
}
}
void display_book() const
{
display();
cout << "\nPage Count : " << page_count;
cout << "\n--------------------------------------------------\n";
}
};
class Tape : public Publication
{
private:
float play_time;
public:
void add_tape()
{
try
{
add();
cout << "Enter Play Duration of the Tape : ";
cin >> play_time;
if (play_time <= 0)
{
throw play_time;
}
}
catch (float)
{
cout << "\nInvalid Play Time!!!";
play_time = 0;
}
}
void display_tape() const
{
display();
cout << "\nPlay Time : " << play_time << " min";
cout << "\n--------------------------------------------------\n";
}
};
int main()
{
Book b1[10];
Tape t1[10];
int ch, b_count = 0, t_count = 0;
do
{
cout << "\n* * * * * PUBLICATION DATABASE SYSTEM * * * * *";
cout << "\n--------------------MENU-----------------------";
cout << "\n1. Add Information to Books";
cout << "\n2. Add Information to Tapes";
cout << "\n3. Display Books Information";
cout << "\n4. Display Tapes Information";
cout << "\n5. Exit";
cout << "\n\nEnter your choice : ";
cin >> ch;
switch (ch)
{
case 1:
if (b_count < 10)
{
b1[b_count].add_book();
b_count++;
}
else
{
cout << "\nBook array is full!" << endl;
}
break;
case 2:
if (t_count < 10)
{
t1[t_count].add_tape();
t_count++;
}
else
{
cout << "\nTape array is full!" << endl;
}
break;
case 3:
cout << "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";
for (int j = 0; j < b_count; j++)
{
b1[j].display_book();
}
break;
case 4:
cout << "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";
for (int j = 0; j < t_count; j++)
{
t1[j].display_tape();
}
break;
case 5:
cout << "\nExiting..." << endl;
break;
default:
cout << "\nInvalid choice! Please select a valid option." << endl;
break;
}
}
while (ch != 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

Enter your choice : 1

Enter the publication information:-


Enter Title of the Publication : It Ends With Us
Enter Price of Publication : 130
Enter Page Count of Book : 376

* * * * * PUBLICATION DATABASE SYSTEM * * * * *


--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit

Enter your choice : 2

Enter the publication information:-


Enter Title of the Publication : It Starts With Us
Enter Price of Publication : 140
Enter Play Duration of the Tape : 336

* * * * * PUBLICATION DATABASE SYSTEM * * * * *


--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit

Enter your choice : 3

* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *


--------------------------------------------------
Title of Publication : It Ends With Us
Publication Price : 130
Page Count : 376
--------------------------------------------------

* * * * * PUBLICATION DATABASE SYSTEM * * * * *


--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit

Enter your choice : 4


* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *
--------------------------------------------------
Title of Publication : It Starts With Us
Publication Price : 140
Play Time : 336 min
--------------------------------------------------
//Name:prasad kakade
// Class: SE B
// Roll No: SBOC 67
// Name: OOPassign4.cpp
// Title: Write a C++ program that creates an output file, writes information to
it, closes the file and open it again as an input file and read the information
from the file
INPUT:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name;
string studentClass;
int rollNo;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your class: ";
getline(cin, studentClass);
cout << "Enter your roll number: ";
cin >> rollNo;
ofstream outFile("student_info.txt");
if (!outFile)
{
cerr << "Error opening file for writing!" << endl;
return 1;
}
outFile << "Name: " << name << endl;
outFile << "Class: " << studentClass << endl;
outFile << "Roll Number: " << rollNo << endl;
outFile.close();
ifstream inFile("student_info.txt");
if (!inFile)
{
cerr << "Error opening file for reading!" << endl;
return 1;
}
string line;
cout << "\nFile content:\n";
while (getline(inFile, line))
{
cout << line << endl;
}
inFile.close();
return 0;
}
OUTPUT:

Enter your name: Prasad


kakade
Enter your class: SE B
Enter your roll number: 67

File content:
Name: Prasad
kakade
Class: SE B
Roll Number: 67
//Name:prasad kakade
// Class: SE B
// Roll No: SBOC 67
// Name: OOPassign5.cpp
// Title: Implement a function template selection Sort. Write a program that
inputs, sorts and outputs an integer array and a float array.

INPUT:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
template<typename T>
void selection_sort(T a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
swap(a[i], a[minIndex]);
}
cout << "Sorted List: ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
int main()
{
int choice, n;
cout << "Choice (1: Integer, 2: Float, 3: String): ";
cin >> choice;
const int MAX_SIZE = 100;
switch (choice)
{
case 1:
{
int intArray[MAX_SIZE];
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter elements:\n";
for (int i = 0; i < n; i++) {
cout << "intArray[" << i << "] = ";
cin >> intArray[i];
}
selection_sort(intArray, n);
break;
}
case 2:
{
float floatArray[MAX_SIZE];
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter elements:\n";
for (int i = 0; i < n; i++) {
cout << "floatArray[" << i << "] = ";
cin >> floatArray[i];
}
selection_sort(floatArray, n);
break;
}
case 3:
{
string strArray[MAX_SIZE];
cout << "Enter number of elements: ";
cin >> n;
cin.ignore();
cout << "Enter elements:\n";
for (int i = 0; i < n; i++) {
cout << "strArray[" << i << "] = ";
getline(cin, strArray[i]);
}
selection_sort(strArray, n);
break;
}
default:
cout << "Invalid choice.\n";
break;
}
return 0;
}
OUTPUT:

Choice (1: Integer, 2: Float, 3: String): 1


Enter number of elements: 3
Enter elements:
intArray[0] = 6
intArray[1] = 8
intArray[2] = 4
Sorted List: 4 6 8

Choice (1: Integer, 2: Float, 3: String): 2


Enter number of elements: 3
Enter elements:
floatArray[0] = 4.9
floatArray[1] = 9.9
floatArray[2] = 5
Sorted List: 4.9 5 9.9

Choice (1: Integer, 2: Float, 3: String): 3


Enter number of elements: 5
Enter elements:
strArray[0] = K
strArray[1] = A
strArray[2] = N
strArray[3] = A
strArray[4] = K
Sorted List: A A K K N
//Name:prasad kakade
// Class: SE B
// Roll No: SBOC 67
// Name: OOPassign7.cpp
// Title: To use map associative container..

INPUT:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
typedef map<string, int> mapType;
mapType populationMap;
populationMap["Maharashtra"] = 112374333;
populationMap["Rajasthan"] = 68548437;
populationMap["Karnataka"] = 68256000;
populationMap["Punjab"] = 30992000;
populationMap["West Bengal"] = 103553153;
populationMap["Andhra Pradesh"] = 26690000;
populationMap["Gujarat"] = 72653000;
cout << "======== Population of States in India ========\n";
cout << "Size of populationMap: " << populationMap.size() << "\n";
string state_name;
cout << "Enter the name of the state: ";
getline(cin, state_name);
cout << "You entered: " << state_name << "\n";
mapType::iterator iter = populationMap.find(state_name);
if (iter != populationMap.end())
{
cout << state_name << "'s population is: " << iter->second << "\n";
}
else
{
cout << "State not found in the populationMap\n";
}
populationMap.clear();
return 0;
}

OUTPUT:

======== Population of States in India ========


Size of populationMap: 7
Enter the name of the state: Rajasthan
You entered: Rajasthan
Rajasthan's population is: 68548437

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy