0% found this document useful (0 votes)
18 views37 pages

Oop 1

Oop

Uploaded by

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

Oop 1

Oop

Uploaded by

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

Name: Amruta Santosh Kudale

Roll No: 272


Div: B
Batch: S4

Assignment No 1
INPUT-

//Perform Complex number Operation

#include<iostream>

using namespace std;

class complex

public:

float x,y;

public:

complex operator +(complex);

complex operator -(complex);

complex operator /(complex);

complex operator

*(complex); int display();

};

complex complex::operator +(complex c)

complex ans;

ans.x=x+c.x;

ans.y=y+c.y;

return ans;

complex complex::operator -(complex c)


{

complex ans;

ans.x=x-c.x;

ans.y=y-c.y;

return ans;

complex complex::operator *(complex c)

complex ans;

ans.x=x*c.x-y*c.y;

ans.y=x*c.y+y*c.x;

return ans;

complex complex::operator /(complex c)

complex ans,a1;

int d;

d=c.x*c.x+c.y*c.y;

a1.x=x*c.x+y*c.y;

a1.y=c.x*y-c.y*x;

ans.x=a1.x/d;

ans.y=a1.y/d;

return ans;
}

int complex:: display()

cout<<x<<" + "<<y<<"i"<<endl;

return 0;

int main()

complex c1,c2,c3;
cout<<"Enter the first complex

number:"<<endl; cin>>c1.x>>c1.y;

cout<<"Enter the second complex

number:"<<endl; cin>>c2.x>>c2.y;

cout<<"First number:"<<endl;

c1.display();

cout<<"Second number:"<<endl;

c2.display();

c3=c1+c2;

cout<<"Addition:"<<endl;

c3.display();

c3=c2-c1;

cout<<"Subtraction:"<<endl;

c3.display();

c3=c1*c2;

cout<<"Multiplication:"<<endl;

c3.display();

c3=c1/c2;

cout<<"Division:"<<endl;

c3.display();

return 0;

OUTPUT-

Enter the first complex

number: 4+3i

Enter the second complex

number: First number:

4 + 3i

Second number:

0 + 0i

Addition:
4 + 3i

Subtraction:

-4 + -3i

Multiplication:

0 + 0i

Division:

nan + nani

INPUT-

#include<iostream>

using namespace std;

class complex {

float x;

float y;

public:

// Constructor to initialize the complex number to 0

complex() {

x = 0;

y = 0;

// Constructor to initialize complex number with given values

complex(float a, float b) {

x = a;

y = b;

// Overloading the + operator

complex operator+(const complex& c)

{ complex temp;

temp.x = x + c.x;
temp.y = y +

c.y; return temp;

// Overloading the * operator

complex operator*(const complex& c)

{ complex temp;

temp.x = (x * c.x) - (y * c.y);

temp.y = (y * c.x) + (x *

c.y); return temp;

// Friend function for output (<<)

friend ostream& operator<<(ostream &out, const complex &d);

// Friend function for input (>>)

friend istream& operator>>(istream &input, complex &num);

};

// Overload the << operator to print complex numbers

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

out << d.x;

if (d.y >= 0)

out << "+" << d.y << "i";

else

out << d.y <<

"i"; return out;

// Overload the >> operator to take complex number

input istream& operator>>(istream &input, complex

&num) {

input >> num.x >> num.y;


return input;

int main() {

complex c1, c2, c3,

c4; int ch;

do {

cout << "\n=========================";

cout << "\n 1.Create

constructor"; cout << "\n

2.Accept data";

cout << "\n 3.Display number";

cout << "\n 4.Add Number";

cout << "\n 5.Multiplication";

cout << "\n 6.Exit";

cout << "\n Enter your choice : ";

cin >> ch;

cout << "\n=========================";

switch(ch) {

case 1:

cout << "\n Complex number after initialization:

"; cout << "\n First complex number: " << c1;

cout << "\n Second complex number: " << c2;

break;

case 2:

cout << "Enter first complex number (x y): ";

cin >> c1;

cout << "Enter second complex number (x y): ";

cin >> c2;

break;

case 3:

cout << "\n First complex number: " << c1;

cout << "\n Second complex number: " << c2;


break;

case 4:

c3 = c1 + c2;

cout << "\n Addition result: " << c3;

break;

case 5:

c4 = c1 * c2;

cout << "\n Multiplication result: " << c4;

break;

case 6:

cout << "\n Exiting...";

break;

default:

cout << "\n Wrong choice...";

break;

} while(ch != 6);

return 0;

OUTPUT-

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number

5.Multiplication

6.Exit

Enter your choice : 1

=========================
Complex number after initialization:

First complex number: 0+0i

Second complex number: 0+0i

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number

5.Multiplication

6.Exit

Enter your choice : 2

=========================Enter first complex number (x y): 3 4

Enter second complex number (x y): 1 -2

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number

5.Multiplication

6.Exit

Enter your choice : 3

=========================

First complex number: 3+4i

Second complex number: 1-2i

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number
5. Multiplication

6.Exit

Enter your choice : 4

=========================

Addition result: 4+2i

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number

5.Multiplication

6.Exit

Enter your choice : 5

=========================

Multiplication result: 11-2i

=========================

1. Create constructor

2. Accept data

3. Display number

4. Add Number

5.Multiplication

6.Exit

Enter your choice : 6

=========================

Exiting...
Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4
Assignment No 2

INPUT-

#include <iostream>

using namespace std;

class data {

private:

string name;

char bg;

string address;

string dob;

string cldiv;

string lic;

static int c;

int roll;

long long int phone; // Change this to long long int

public:

data(); // Default constructor

static int getcount(); // To get the number of objects

void getdata(); // To input data

void show(); // To display data

data(const data &obj); // Copy constructor

data(int roll, long long int phone, string name, string address, string dob, string cldiv, string lic); // Parameterized
constructor

~data(); // Destructor
};

int data::c = 0;

data::data()

{ roll = 0;

phone = 0;

name = "Name";

address = "Address";

dob = "DOB";

cldiv = "Class and Division";

lic = "License no.";

cout << "Default Constructor" << endl;

c++;
}

data::data(int roll, long long int phone, string name, string address, string dob, string cldiv, string lic)

{ cout << "Parameterized Constructor" << endl;

c++;

this->roll = roll;

this->phone = phone;

this->name = name;

this->address = address;

this->dob = dob;

this->cldiv = cldiv;

this->lic = lic;
}

data::data(const data &obj) { // Copy constructor

cout << "Copy constructor implemented" << endl;

c++;
this->roll = obj.roll;

this->phone = obj.phone;

this->name = obj.name;

this->address = obj.address;

this->dob = obj.dob;
this->cldiv = obj.cldiv;

this->lic = obj.lic;

data::~data() {

cout << "Destructor called" << endl;

void data::getdata() {

cout << "Enter roll number: ";

cin >> roll;

cout << "Enter telephone number:

"; cin >> phone;

cout << "Enter Name: ";

cin.ignore(); // To ignore any leftover newline character in the buffer

getline(cin, name); // Use getline to read a full name, including spaces

cout << "Enter address: ";

getline(cin, address);

cout << "Enter D.O.B: ";

getline(cin, dob);

cout << "Enter Class and Division: ";

getline(cin, cldiv);

cout << "Enter license number: ";

cin >> lic;

void data::show() {

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

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

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

cout << "Address: " << address << endl;

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

cout << "Class and Division: " << cldiv <<

endl; cout << "License number: " << lic <<

endl;

}
int data::getcount() {

return c;

int main()

int num;

data *d1 = new

data(); d1->show();

delete d1;

// Updated to use long long int for phone number

data *d2 = new data(23, 9822794182LL, "Sakshi Bhosale", "Satara", "22.02.97", "SE B",

"MSIN92U"); d2->show();

data *d3 = new data(*d2); // Copy constructor is used

here d3->show();

delete d2; // Don't forget to delete dynamically allocated objects

cout << "Enter size of database: ";

cin >> num;

data *dx = new data[num]; // Dynamic allocation of array

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

dx[i].getdata();

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

dx[i].show();

cout << "Number of constructor calls and total number of objects: " << data::getcount() << endl;

delete[] dx; // Don't forget to delete the dynamically allocated array

return 0;

OUTPUT-

Default Constructor
Name: Name

Roll Number: 0

Telephone Number: 0

Address: Address

Date of Birth: DOB

Class and Division: Class and Division

License number: License no.

Destructor called

Parameterized Constructor

Name: Rohini Kolekar

Roll Number: 23

Telephone Number: 9822794182

Address: Satara

Date of Birth: 22.02.97

Class and Division: SE B

License number: MSIN92U

Copy constructor implemented

Name: Rohini Kolekar

Roll Number: 23

Telephone Number: 9822794182

Address: Satara

Date of Birth: 22.02.97

Class and Division: SE B

License number: MSIN92U

Destructor called
Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4
Assignment no 3
INPUT-

#include <iostream>

#include <string>

#include <conio.h>

using namespace std;

class publication

private:

string title;

float price;

public:void getdata()

cout << "Enter title of publication: ";

cin >> title;

cout << "Enter price of publication: ";

cin >> price;

void putdata()

cout << "Publication title: " << title << endl;

cout << "Publication price: " << price<<endl;

};

class book :public publication


{

private:int pagecount;

public:

void getdata()

publication::getdata();

cout << "Enter Book Page Count: ";

cin >> pagecount;

void putdata()

publication::putdata();

cout << "Book page count: " << pagecount << endl;

//Show book data

};

class tape :public publication

private:float ptime;

public:void getdata()

publication::getdata();

cout << "Enter tap's playing time: ";

cin >> ptime;

void putdata()

publication::putdata();

cout << "Tap's playing time: " << ptime << endl;

};
int main()

book b;

tape t;

b.getdata();

t.getdata();

b.putdata();

t.putdata();

return 0;

OUTPUT-

Enter title of publication: rohini

Enter price of publication: 1000

Enter Book Page Count: 420

Enter title of publication:

newpublication Enter price of

publication: 200000

Enter tap's playing time:

4:00 Publication title: rohini

Publication price: 1000

Book page count: 420

Publication title: newpublication

Publication price: 200000

Tap's playing time: 4


Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4
Assignment No 4
INPUT-

#include <iostream>
#include <fstream>

using namespace std;

class file
{
char name[40];
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()

{
file obj[5];
fstream f;
int i,n;

f.open("input.txt", ios::out);
cout<<"\nHow many employee information water to store ";
cin>>n;
cout<<"\nEnter information of 3
employee"; for(i=0;i<n;i++)
{
cout<<"\nEnter information of "<<i<<"
employee"; obj[i].accept();
f.write((char*) &obj[i],sizeof(obj[i]));

}
f.close();
f.open("input", ios::in);
cout<<"\nEnter information of employee is ";
for(i=0;i<n;i++)
{
f.read((char*) &obj[i],sizeof(obj[i]));
obj[i].display();

}
f.close();
return 0;
}

OUTPUT-

How many employee information water to store 3

Enter information of 3 employee

Enter information of 0 employee 40

30

10

Enter information of 1 employee 20

30

10

Enter information of 2 employee40

90

45

Enter information of employee is

40 30 10

20 30 10
40 90 45
Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4

Assignment No 5
INPUT-

#include <iostream>

#include <vector> // For dynamic array (std::vector)

using namespace std;

template <class T>

void sort()

int i, j, min,

n; T temp;

// Dynamically allocate the array using std::vector

cout << "\nEnter number of elements you want to sort: ";

cin >> n;

vector<T> a(n); // Vector of type T to store the input elements

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

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

cin >> a[i];

// Selection sort algorithm

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

min = i;

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

{
if (a[j] < a[min])

min = j; } }

if (min != i)

temp = a[i];

a[i] = a[min];

a[min] = temp;

// Display sorted array

cout << "\nThe array in sorted order is:

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

cout << "\t" << a[i];

cout << endl;

int main()

int choice;

char ans;

do

// Menu for choosing the sorting type

cout << "\n1. Integer sort.";

cout << "\n2. Float sort.";

cout << "\nEnter your choice: ";

cin >> choice;

// Switch case for choosing the type of sort

switch (choice)

{
case 1:

sort<int>(); // Call template function for sorting integers

break;

case 2:

sort<float>(); // Call template function for sorting floats

break;

default:

cout << "\nInvalid choice.";

break;

// Ask the user whether to continue or not

cout << "\nDo you wish to continue (Y/N)? ";

cin >> ans;

} while (ans == 'Y' || ans ==

'y'); return 0;

OUTPUT-

1. Integer sort.

2. Float sort.

Enter your choice: 1

Enter number of elements you want to sort: 5

Enter 5 elements: 30 5 25 9 10

The array in sorted order is: 5 9 10 25 30

Do you wish to continue (Y/N)? y

1. Integer sort.

2. Float sort.

Enter your choice: 2

Enter number of elements you want to sort: 5

Enter 5 elements: 8.20 3.25 9.5 1.25 3.46

The array in sorted order is: 1.25 3.25 3.46 8.2 9.5

Do you wish to continue (Y/N)?


Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4
Assignment No 6
INPUT-

#include <iostream>

#include<list>

using namespace std;

class record

list<string>name,dob,phone,ni;

list<string>::iterator it1,it2,it3,j,k,l,c,n;

list<string>code;

list<int>number;

list<float>cost;

list<int>::iterator no,j1;

list<float>::iterator f,i;

public:

void getp();

void display();

void searchp(string);

void sortp();

void checkempty();

void getlist();

void displayit();

void searchlist();

void sortitem();
};

void record::getp()

int count;

string n,d,p;

cout<<"Enter the number of members in record:"<<endl;

cin>>count;

for(int i=1;i<=count;i++)

cout<<"Enter name:"<<endl;

cin>>n;

name.push_back(n);

cout<<"Enter date of birth:"<<endl;

cin>>d;

dob.push_back(d);

cout<<"Enter phone number:"<<endl;

cin>>p;

phone.push_back(p);

void record::searchp(string data)

int flag=0;

it1=name.begin();

it2=dob.begin();

it3=phone.begin();

while(it1!=name.end()&&it2!=dob.end()&&it3!=phone.end())

if(*it1==data)

cout<<"Record found!"<<endl;

cout<<"Corresponding D.O.B: "<<*it2<<endl;


cout<<"Corresponding phone number: "<<*it3<<endl;

flag=1;

break;

if(*it2==data)

cout<<"Record found!"<<endl;

cout<<"Corresponding name "<<*it1<<endl;

cout<<"Corresponding phone number: "<<*it3<<endl;

flag=1;

break;

if(*it3==data)

cout<<"Record found!"<<endl;

cout<<"Corresponding name: "<<*it1<<endl;

cout<<"Corresponding D.O.B: "<<*it2<<endl;

flag=1;

break;

it1++;

it2++;

it3++;

if(flag==0)

cout<<"Record not found."<<endl;

void record:: display()

it1=name.begin();

it2=dob.begin();

it3=phone.begin();
while(it1!=name.end())

cout<<*it1<<"\t"<<*it2<<"\t"<<*it3<<endl;

it1++;

it2++;

it3++;

void record::sortp()

string temp;

it1=name.begin();

it2=dob.begin();

it3=phone.begin();

j=it1;

k=it2;

l=it3;

j++;

k++;

l++;

while(it1!=name.end())

while(j!=name.end())

if(*it1>*j)

temp=*it1;

*it1=*j;

*j=temp;

temp=*it2;

*it2=*k;

*k=temp;
temp=*it3;

*it3=*l;

*l=temp;

j++;

k++;

l++;

it1++;

it2++;

it3++;

void record::getlist()

cout<<"Enter the number of items:"<<endl;

int c,no;

string n;

float f;

cin>>c;

for(int i=1;i<=c;i++)

cout<<"Enter item name:"<<endl;

cin>>n;

ni.push_back(n);

cout<<"Enter item code:"<<endl;

cin>>n;

code.push_back(n);

cout<<"Enter cost:"<<endl;

cin>>f;

cost.push_back(f);

cout<<"Enter the quantity:"<<endl;


cin>>no;

number.push_back(no);

void record::displayit()

c=code.begin();

n=ni.begin();

no=number.begin();

f=cost.begin(); while(c!

=code.end())

cout<<*c<<"\t"<<*n<<"\t"<<*no<<"\t"<<*f<<endl;

c++;

n++;

no++;

f++;

void record::sortitem()

string temp;

int tempno;

float tempf;

c=code.begin();

n=ni.begin();

no=number.begin();

f=cost.begin();

i=f;

j1=no;

k=c;

l=n;
i++;

j1++;

k++;

l++;

while(f!=cost.end())

while(i!=cost.end())

if(*f>*i)

tempf=*f;

*f=*i;

*i=tempf;

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:
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-

1. Personal record

2. Item record

Enter choice:

1. Enter details

2. Display

3. Search entry

4. Sort records

Enter choice

Enter the number of members in

record: 2

Enter name:

Rohini

Enter date of birth:

13/06/2003

Enter phone number:

7410785906

Enter name:

Sarita

Enter date of birth:

5/06/1977

Enter phone number:

9765091725
Rohini 13/06/2003 7410785906

Sarita 5/06/1977 9765091725

Do you wish to continue? Y or

Ny

1. Enter details

2. Display

3. Search entry

4. Sort records

Enter choice

Sakshi 13/06/2003 7410785906

Sarita 5/06/1977 9765091725

Do you wish to continue? Y or

Ny

1. Enter details

2. Display

3. Search entry

4. Sort records

Enter choice

sakshi

Record not found.

Do you wish to continue? Y or

Ny

1. Enter details

2. Display

3. Search entry

4. Sort records

Enter choice

Rohini

Record found!
Corresponding D.O.B: 13/06/2003

Corresponding phone number: 7410785906

Do you wish to continue? Y or N

1. Enter details

2. Display

3. Search entry

4. Sort records

Enter choice

Rohini 13/06/2003 7410785906

Sarita 5/06/1977 9765091725

Do you wish to continue? Y or N


Name: Amruta Santosh Kudale
Roll No: 272
Div: B
Batch: S4

Assignment No 7
INPUT-

#include<string>

using namespace std;

int main()

// map declaration

map<int,string>mymap;

// mapping integers to

strings mymap[1] = "Hi";

mymap[2] = "This";

mymap[3] = "is";

mymap[4] = "DYP";

// using operator[] to print string

// mapped to integer

4 cout<<mymap[4];

return 0;

OUTPUT-

DYP

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

OUTPUT-

========Population of states in India==========

Size of populationMap5

Enter name of the state :Maharashtra

Maharashtra 's population is 7026357

You might also like