Oop SVCET
Oop SVCET
Title:- Implement a class Complex which represents the Complex Number data type.
Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.
Roll No:-
Class:-SE Computer
Sub:-OOPL & CGL
Date:-
****************************************************************************/
Program-
#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<<"\nEnter the 1st number\n";
cin>>c1;
cout<<"\nEnter 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:-
student@student-OptiPlex-3010:~$ ./a.out
Default constructor value=
0+0i
Enter the 1st number
Enter the real part 2
Enter the imaginary par t4
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
student@student-OptiPlex-3010:~$ */
Practical No:2
/*
Title:- Develop an object oriented program in C++ to create a database of student information
system containing the following information: Name, Roll number, Class, division, Date of Birth,
Blood group, Contact address, telephone number, driving license no. etc Construct the database
with suitable member functions for initializing and destroying the data viz constructor, default
constructor, Copy constructor, destructor, static member functions, friend class, this pointer,
inline code and dynamic memory allocation operators-new and delete.
Roll No:-
Class:-SE Computer
Sub:-OOPL & CGL
Date:-
****************************************************************************/
Program-
#include<iostream>
#include<string.h>
using namespace std;
class person_additional_info
{
char address[20],license[20],insurance[20];
long int contact;
public:
person_additional_info() //Default constructor
{
strcpy(address,"XYZ");
strcpy(license,"XY-0000000000");
strcpy(insurance,"XY00000000X");
contact=000000000;
}
~person_additional_info() //Destructor
{
cout<<"I am in Destructor";
}
};
friend class person; // Declaration Friend class
//Definition of friend class
class person
{
char name[20], dob[10], blood[10];
float ht,wt;
static int count; // Static variable
person_additional_info *pai;
public:
person() //Default constructor
{ strcpy(name,"XYZ");
strcpy(dob,"dd/mm/yy");
strcpy(blood,"A +");
ht=0;
wt=0;
pai=new person_additional_info;
}
person(person*p1) //Copy constructor
{ strcpy(name,p1->name);
strcpy(dob,p1->dob);
strcpy(blood,p1->blood);
ht=p1->ht;
wt=p1->wt;
pai=new person_additional_info;
strcpy(pai->address,p1->pai->address);
strcpy(pai->license,p1->pai->license);
strcpy(pai->insurance,p1->pai->insurance);
pai->contact=p1->pai->contact;
}
static void showcount() //Static member function
{
cout<<"\nNo of records count="<<count<<"\n";
}
~person() //Destructor
{
cout<<"\nI am in Destructor\n";
}
void getdata(char name[20]);
inline void display(); // Inline function declaration
};
void person::getdata(char name[20])
{
strcpy(this->name,name); //this pointer
cout<<"\n Enter date of birth";
cin>>dob;
cout<<"\n Enter blood group";
cin>>blood;
cout<<"\n Enter height";
cin>>ht;
cout<<"\n Enter weight";
cin>>wt;
cout<<"\n Enter address";
cin>>pai->address;
cout<<"\n Enter Licence number";
cin>>pai->license;
cout<<"\n Enter Insurance policy number";
cin>>pai->insurance;
cout<<"\n Enter Contact number";
cin>>pai->contact;
count++;
}
//inline function definition
void person::display()
{
cout<<"\t"<<name;
cout<<"\t"<<dob;
cout<<"\t"<<blood;
cout<<"\t"<<ht;
cout<<"\t"<<wt;
cout<<"\t"<<pai->address;
cout<<"\t"<<pai->license;
cout<<"\t"<<pai->insurance;
cout<<"\t"<<pai->contact;
}
int person::count; //Static variable definition
int main()
{
person *p1,*p2;
int ch;
p1=new person; //call default constructor & dynamic memory allocation
p2=new person(p1); //call copy constructor
cout<<"\tName";
cout<<"\tDob";
cout<<"\t Blood";
cout<<"\tHt";
cout<<"\tWt";
cout<<"\tAddress";
cout<<"\tLicense";
cout<<"\tInsurance";
cout<<"\tContact";
cout<<endl;
cout<<"Default Constructor Value \n";
p1->display();
cout<<"\n";
cout<<"Copy Constructor Value \n";
p2->display();
int n;
cout<<"\nEnter how many records you want??";
cin>>n;
person p3[n]; //array of object
char name[20];
int x=0;
do
{
cout<<"\nWelcome to Personal database system";
cout<<"\n1.Enter the record";
cout<<"\n2.Display the record";
cout<<"\n3.Exit";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter the Name ";
cin>>name;
p3[x].getdata(name);
person::showcount(); // calls static function
x++;
break;
}
case 2:
{
cout<<"\tName";
cout<<"\tDob";
cout<<"\t Blood";
cout<<"\tHt";
cout<<"\tWt";
cout<<"\tAddress";
cout<<"\tLicense";
cout<<"\tInsurance";
cout<<"\tContact";
for(int i=0;i<n;i++)
{
cout<<"\n";
p3[i].display(); //calls inline function
}
break;
}
}
}while(ch!=3);
delete p1; //dynamic memory de-allocation
delete p2;
return 0;
/*Output:-
student@student-OptiPlex-3010:~$ g++ groupa6.cpp
student@student-OptiPlex-3010:~$ ./a.out
Name Dob Blood Ht Wt Address License Insurance Contact
Default Constructor Value
XYZ dd/mm/yy A + 0 0 XYZ XY-0000000000 XY00000000X 0
Enter height 5
Enter weight 50
};
try
{
if(pages > 500 && pages <1500)
{
if(price >100 && price < 2000)
{
displayb();
}
}
else throw(pages);
}
catch(int i){
cout<<"Caught exception in function of book"<<endl;
cout<<"You entered an invalid data"<<endl;
title = "0";
price = 0.0;
pages = 0;
displayb();
throw;
}
}
void displayb()
{
cout<<"The book you entered is :"<<endl<<endl;
cout<<"The entered title is :"<<title<<endl;
cout<<"The entered price is :"<<price<<endl;
cout<<"The entered pages are :"<<pages<<endl;
}
};
try
{
if(min > 30.00 && min < 90.00)
{
if(price > 500 && price < 1000)
{
displayt();
}
}
else throw(min);
}
catch(float f){
cout<<"caught exception in the function of tape"<<endl;
cout<<"you entered an invalid data"<<endl;
title = "0";
price = 0.0;
min = 0.0;
displayt();
throw;
}
}
void displayt()
{
cout<<"The details of tape you entered is :"<<endl<<endl;
cout<<"The entered title is :"<<title<<endl;
cout<<"The entered price is :"<<price<<endl;
cout<<"The entered play time is :"<<min<<endl;
}
};
int main()
{
books b;
tape t;
int choice;
cout<<"Do want to buy a book(Press 1) or a tape(Press 2) :"<<flush;
cin>>choice;
switch(choice)
{
case 1: try
{
b.getb();
}
catch(int i){
cout<<"Caught exception in int main()"<<endl;
}
break;
case 2: try
{
t.gett();
}
catch(float f){
cout<<"Caught exception in int main()"<<endl;
}
break;
default:cout<<"Entered bad choice!! Try again!!"<<endl;
}
return 0;
}
/*Output:-
system@system-Lenovo:~$ g++ filename.cpp
system@system-Lenovo:~$ ./a.out
Do want to buy a book(Press 1) or a tape(Press 2) :1
Enter the details of the book :
Enter the title of the book :the man
Enter the price of the book :200
Enter the pages of the book :366
Caught exception in function of book
You entered an invalid data
The book you entered is :
The entered title is :0
The entered price is :0
The entered pages are :0
Caught exception in int main()
system@system-Lenovo:~$ ./a.out
Do want to buy a book(Press 1) or a tape(Press 2) :2
Enter the details of the tape :
Enter the title of the tape :tape real
Enter the price of the tape :600
Enter the pages of the playing time(in minutes) :80
The details of tape you entered is :
The entered title is :tape real
The entered price is :600
The entered play time is :80
*/
Practical No:4
/*
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.
Roll No:-
Class:-SE Computer
Sub:-OOPL & CGL
Date:-
******************************************************************
**********/
Program-
#include <iostream>
#include <fstream>
using namespace std;
class employee //name of employee class
{
char name[20]; //variable declaration
int emp_id;
float salary;
public:
void accept()
{
cin>>name;
cin>>emp_id;
cin>>salary;
}
void display()
{
cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;
}
};
int main()
{
employee o[5];
fstream f;
int i,n;
f.open("input.txt");
cout<<"\n How many employee information wanted to store:";
cin>>n;
//create employee
cout<<"\n Enter information of employees (Enter name, emp_id, salary)";
for(i=0;i<n;i++)
{
cout<<"\n Enter information of "<<i<<" employee";
o[i].accept();
f.write((char *)&o[i], sizeof(o[i]));
}
f.close();
f.open("input.txt", ios::in);
cout<<"\n Information of employee is as follows";
for(i=0;i<n;i++)
{
f.read((char*)&o[i], sizeof(o[i]));
o[i].display();
}
f.close();
return 0;
}/*Output:-
srl@srl-Lenovo-G550:~/Desktop/OOPL$ g++ b16.cpp
srl@srl-Lenovo-G550:~/Desktop/OOPL$ ./a.out
How many employee information wanted to store:3
//accept input from user
//write object in employee
//read data from employee
Enter information of 3 employees (Enter name, emp_id, salary)
Enter information of 0 employeea
1
100
Enter information of 1 employeeb
2
200
Enter information of 2 employeec
3
300
1
Information of employee is as follows
a 1 100
b 2 200
c 3 300
*/
Practical No:5
/*
Title: - Write a function template selection Sort. Write a program that inputs, sorts
and outputs
an int array and a float array.
Roll No:-
Class:-SE Computer
Sub:-OOPL & CGL
Date:-
******************************************************************
**********/
Program-
#include<iostream>
using namespace std;
#define size 10
int n;
template<class T>
void selection(T A[size])
{
int i,j,min;
T temp;
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(A[j]<A[min])
min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
cout<<"\nSorted list=\n";
for(i=0;i<n;i++)
cout<<"\t"<<A[i];
}
int main()
{
int i,A[size];
float B[size];
cout<<"Selection sort\n";
cout<<"\nInteger Element\n";
cout<<"Enter how many elements you want\n";
cin>>n;
cout<<"\nEnter the Integer element\n";
for(i=0;i<n;i++)
cin>>A[i];
selection(A);
cout<<"\nFloat Element\n";
cout<<"Enter how many elements you want\n";
cin>>n;
cout<<"\nEnter float element\n";
for(i=0;i<n;i++)
cin>>B[i];
selection(B);
return 0;
}
/*Output:-
temp=*n;
*n=*l;
*l=temp;
temp=*c;
*c=*k;
*k=temp;
tempno=*no;
*no=*j1;
*j1=tempno;
}
i++;
j1++;
k++;
l++;
}
f++;
n++;
no++;
c++;
}
}
void record::searchlist()
{
string key;
cout<<"Enter the item code:"<<endl;
cin>>key;
c=code.begin();
n=ni.begin();
no=number.begin();
f=cost.begin();
while(c!=code.end())
{
if(key==*c)
{
}
cout<<"Item available!"<<endl;
cout<<"Item name: "<<*n<<endl;
cout<<"Item quantity: "<<*no<<endl;
cout<<"Item cost: "<<*f<<endl;
c++;
n++;
no++;
f++;
}
}
int main()
{
record obj;
string key;
int ch,chr;
char x='y';
do
{
cout<<"1. Personal record\n2. Item record\nEnter choice:\n";
cin>>ch;
do
{
if(ch==1)
{
cout<<"1. Enter details\n2. Display\n3. Search entry\n4. Sort records\nEnter
choice\n";
cin>>chr;
switch(chr)
{
case 1:
obj.getp();
obj.display();
break;
case 2:
obj.display();
break;
case 3:
cout<<"Enter either name, d.o.b or phone number you want to find\n";
cin>>key;
obj.searchp(key);
break;
case 4:
obj.sortp();
obj.display();
break;
default:
cout<<"Wrong choice"<<endl;
}
}
else if(ch==2)
{
cout<<"1. Enter details\n2. Display\n3. Search entry\n4. Sort records\nEnter
choice\n";
cin>>chr;
switch(chr)
{
case 1:
obj.getlist();
obj.displayit();
break;
case 2:
obj.displayit();
break;
case 3:
obj.searchlist();
break;
case 4:
obj.sortitem();
obj.displayit();
break;
default:
cout<<"Wrong choice"<<endl;
}
}
else
{
}
cout<<"Wrong choice"<<endl;
break;
cout<<"Do you wish to continue? Y or N\n";
cin>>x;
}while(x=='y'||x=='Y');
cout<<"Do you wish to select another type of record? Y or N\n";
cin>>x;
}while(x=='y'||x=='Y');
return 0;
}
/*Output:-
[student@localhost ~]$ g++ C3.cpp
[student@localhost ~]$ ./a.out
1. Personal record
2. Item record
Enter choice:
1
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
1
Enter the number of members in record:
2
Enter name:
abc
Enter date of birth:
5/6/1993
Enter phone number:
22556
Enter name:
pqr
Enter date of birth:
9/7/1993
Enter phone number:
22076
abc 5/6/1993 22556
pqr 9/7/1993 22076
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
2
abc 5/6/1993 22556
pqr 9/7/1993 22076
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
3
Enter either name, d.o.b or phone number you want to find
abc
Record found!
Corresponding D.O.B: 5/6/1993
Corresponding phone number: 22556
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
4
abc 5/6/1993 22556
pqr 9/7/1993 22076
Do you wish to continue? Y or N
n
Do you wish to select another type of record? Y or N
n
[student@localhost ~]$ */