OOP All Practicals With Output
OOP All Practicals With Output
/*
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 mul ply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers */
# include<iostream>
using namespace std;
class Complex //decaring 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); // Addi on
Complex operator * (Complex); // Mul plica on
};
Complex::Complex() // Default Constructor
{
real = 0;
img = 0;
}
istream & operator >> (istream &, Complex & i)
{
cin >> i.real >> i.img;
return cin;
}
ostream & operator << (ostream &, const Complex & d)
{
cout << d.real << " + " << d.img << "i" << endl;
return cout;
}
Complex Complex::operator + (Complex c1) // Overloading + operator
{
Complex temp;
temp.real = real + c1.real;
temp.img = img + c1.img;
return temp;
}
Complex Complex::operator * (Complex c2) // Overloading * Operator
{
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;
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;
C3 = C1+C2;
cout << "Addi on : " << C3 << endl;
C4 = C1 * C2;
cout << "Mul plica on : " << C4 << endl;
return 0;
}
Experiment No : 1
Output :
Experiment No : 2
/*
Experiment Number 2 : Develop a program in C++ to create a database of student’s informa on system
containing the following informa on: Name, Roll number, Class, Division, Date of Birth, Blood group,
Contactaddress, Telephone number, Driving license no. and other. Construct the database with suitable
member func ons. Make use of constructor, default constructor, copy constructor, destructor, sta c member
func ons, friend class, this pointer, inline code and dynamic memory alloca on operators-new and delete as
well as excep on handling.
*/
#include<iostream>
#include<string.h>
using namespace std;
class StudData;
class Student{
string name;
int roll_no;
string cls;
char* division;
string dob;
char* bloodgroup;
sta c int count;
public:
Student() // Default Constructor
{
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
{
delete division;
delete[] bloodgroup;
}
sta c int getCount()
{
return count;
}
void getData(StudData*);
void dispData(StudData*);
};
class StudData{
string caddress;
long int* telno;
long int* dlno;
friend class Student;
public:
StudData()
{
caddress="";
telno=new long;
dlno=new long;
}
~StudData()
{
delete telno;
delete dlno;
}
void getStudData()
{
cout<<"Enter Contact Address : ";
cin.get();
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;
}
};
int main()
{
book b1; // object of class book
tape t1; // object of class tape
b1.add_book();
t1.add_tape();
cout << "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";
b1.display_book();
cout << "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";
t1.display_tape();
return 0;
}
Experiment No : 3
Output :
Experiment No : 4
/*
Write a C++ program that creates an output file, writes informa on to it, closes the file, open it again as an
input file and read the informa on from the file.
*/
#include<iostream>
#include<fstream>
using namespace std;
class Employee // declaring class employee
{
string Name;
int ID;
double salary;
public:
void accept()
{
cout<<"\n Name : ";
cin>>Name;
cout<<"\n Id : ";
cin>>ID;
cout<<"\n Salary : ";
cin>>salary;
}
void display()
{
cout<<"\n Name : "<<Name;
cout<<"\n Id : "<<ID;
cout<<"\n Salary : "<<salary<<endl;
}
};
int main()
{
Employee o[5];
fstream f;
int i,n;
f.open("aaa.txt",ios::out);
cout<<"\n Enter the number of employees you want to store : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Enter informa on of Employee "<<i+1<<"\n";
o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
}
f.close();
f.open("aaa.txt",ios::in);
cout<<"\n Informa on of Employees is as follows : \n";
for(i=0;i<n;i++)
{
cout<<"\nEmployee "<<i+1<<"\n";
f.write((char*)&o[i],sizeof o[i]);
o[i].display();
}
f.close();
return 0;
}
Experiment No : 4
Output :
Experiment No : 5
/*
Write a func on template for selec on sort that inputs, sorts and outputs an integer array and
a float array.
*/
#include<iostream>
using namespace std;
int n;
#define size 10
template<class T>
void sel(T A[size])
{
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;
}
cout<<"\nSorted array:";
for(i=0;i<n;i++)
{
cout<<"\n "<<A[i];
}
}
int main()
{
int A[size];
float B[size] ;
int i;
cout<<"\nEnter total no of int elements:";
cin>>n;
cout<<"\nEnter int elements:";
for(i=0;i<n;i++)
{
cin>>A[i];
}
sel(A);
#include <iostream>
#include <map>
#include <string>
#include <u lity>