Oop Practicals
Oop Practicals
Oop Practicals
// 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:
#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:
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:
#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:
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:
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: