OOPCGL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

OOPS PRACTICALS

PRACTICAL NO:1
Implement a class Complex which represents the Complex Number data type.

Implement the following using C++

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 multiply two complex numbers.

4. Overload operators << and >> to print and read Complex Numbers.

PROGRAM
#include <iostream>

using namespace std;

class Complex {

private:

int real, imag;

public:

//constructor

Complex(int r = 0, int i = 0) {real = r; imag = i;}

//friend functions of complex class

friend ostream & operator << (ostream &out, Complex const &obj);

friend istream & operator >> (istream &in, Complex &obj);

//overloading of + operator

Complex operator + (Complex const &obj)

Complex res;

res.real = real + obj.real;

res.imag = real + obj.imag;

return res;

//overloading of * operator

Complex operator * (Complex const &obj)

{
Complex res;

res.real=((real)*(obj.real))-((imag)*(obj.imag));

res.imag=((real)*(obj.imag))+((obj.real)*(imag));

return res;

};

ostream & operator << (ostream &out, Complex const &obj)

out << obj.real;

out << "+i" << obj.imag;

return out;

istream & operator >> (istream &in, Complex &obj)

cout << "\n Enter Real Part: "<<endl;

in >> obj.real;

cout << "\n Enter Imag Part: "<<endl;

in >> obj.imag;

return in;

int main()

Complex c1,c2,c3,c4;

cout <<"\n Enter First Number : ";

cin >> c1;

cout <<"\n Enter Second Number : ";

cin >> c2;

//Addition

c3 = c1+c2;

cout << "\n The Addition Is : " <<c3 ;

//Multiplication

c4 = c1 * c2;
cout << "\n The Multiplication Is :" <<c4 <<endl;

return 0;

OUTPUT

Enter First Number:

Enter Real Part: 3

Enter Imag Part: 3

Enter Second Number :

Enter Real Part: 7

Enter Imag Part: 7

The Addition Is : 10+i10

The Multiplication Is :0+i42

---------------------------------------------------------------------------------------------------------------------------------------------------

PRACTICAL NO .2
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.

PROGRAM
#include<iostream> //header file used for cin and cout

#include<string.h> //header file for string class

using namespace std; //refer cin and cout

#define max 100;

class per_info //stud_info is friend class of per_info

string lic, dob, bldgrp; //personal info variables

public:

per_info(); //DECLARE DEFAULT CONSTRUCTOR//

per_info(per_info &); //DECLARE COPY CONSTRUCTOR//

~per_info() //DEFINITION AND DECLARATION OF DESTRUCTOR


{

cout<<"\nDESTRUCTOR IS CALLED!!!!!"<<endl<<"RECORD DELETED SUCCESSFULLY"<<endl;

friend class student; //FRIEND FUNCTION DECLARATION

};

class student //DEFINITION OF STUDENT CLASS

string name, address, year; //OBJECTS OF STRING CLASS

char div;

int roll_no;

long mob;

static int cnt; // STATIC VARIABLE DECLARATION

public:

void create(per_info &);//TO CREATE DATABASE AND PASSED REFERENCE OF PERSONAL INFO OBJECT

void display(per_info &); //TO DISPLAY DATABASE

inline static void inccnt() //STATIC FUNCTION//

cnt++;

inline static void showcnt() //STATIC FUNCTION//

//INLINE FUNCTION//

cout<<"\nTOTAL NO OF RECORDS ARE : "<<cnt;

student(); //DEFAULT CONSTRUCTOR//

student(student &); //COPY CONSTRUCTOR OF STUDENT CLASS//

~student() //DESTRUCTOR OF STUDENT CLASS//

cout<<"\nDESTRUCTOR IS CALLED!!!"<<endl<<"RECORD DELETED SUCCESSFULLY"<<endl;

};

int student::cnt; //DEFINITION OF STATIC MEMBER//

student::student() //CONSTRUCTOR DEFINITION//


{

name="ANAGHA NIRGUDE";

address="SR NO.81 BABBAR SOLANKI \nDIGHI, PUNE";

year="SE COMPUTER";

div='A';

roll_no=21042;

mob=942329999;

per_info::per_info() //CONSTRUCTOR DEFINITION//

lic="ABD45656";

dob="02/11/1997";

bldgrp="A-";

student::student(student &obj) //DEFINITION OF COPY CONTRUCTOR OF STUDENT CLASS

this->name=obj.name; //this is a pointer points to the object which invokes it

this->address=obj.address; //this-> can be written as name

this->year=obj.year;

this->div=obj.div;

this->roll_no=obj.roll_no;

this->mob=obj.mob;

per_info::per_info(per_info &obj) //DEFINITION OF COPY CONTRUCTOR OF PERSONAL CLASS

lic=obj.lic;

dob=obj.dob;

bldgrp=obj.bldgrp;

//TO CREATE THE DATABASE

//DEFINTION OF CREATE FUNTION

void student::create(per_info &obj)

{
cout<<"\nNAME : "<<endl;

cin>>name;

cout<<"\nADDRESS : "<<endl;

cin>>address;

cout<<"\nDATE OF BIRTH : "<<endl;

cin>>obj.dob;

cout<<"\nYEAR : "<<endl;

cin>>year;

cout<<"\nDIVISION: "<<endl;

cin>>div;

cout<<"\nROLL NUMBER : "<<endl;

cin>>roll_no;

cout<<"\nBLOOD GROUP : "<<endl;

cin>>obj.bldgrp;

cout<<"\nLICEINCE NUMBER : "<<endl;

cin>>obj.lic;

cout<<"\nPHONE NUMBER : "<<endl;

cin>>mob;

//DEFINTION OF DISPLAY FUNCTION

//TO DISPLAY DATABASE

void student::display(per_info &obj)

cout<<"\n***********************"<<endl;

cout<<"\nNAME OF STUDENT : "<<name<<endl;

cout<<"\nADDRESS OF STUDENT : "<<address<<endl;

cout<<"\nDATE OF BIRTH : "<<obj.dob<<endl;

cout<<"\nYEAR : "<<year<<endl;

cout<<"\nDIVISION : "<<div<<endl;

cout<<"\nROLL NO : "<<roll_no<<endl;

cout<<"\nBLOOD GROUP : "<<obj.bldgrp<<endl;

cout<<"\nLICEINCE NUMBER : "<<obj.lic<<endl;

cout<<"\nPHONE NUMBER : "<<mob<<endl;


cout<<"\n***********************"<<endl;

int main()

int n; //COUNT OF NUMBER OF STUDENTS

int ch;

char ans;

cout<<"\nENTER NO OF STUDENTS :"<<endl;

cin>>n;

cout<<"\n***********************"<<endl;

student *sobj=new student[n]; //NEW IS DYNAMIC MEMORY ALLOCATION OPERATOR, sobj IS POINTER OF
TYPE STUDENT AND IT IS A ARRAY OF OBJECT OF SIZE n

per_info *pobj=new per_info[n];//NEW IS DYNAMIC MEMORY ALLOCATION OPERATOR, pobj IS POINTER OF


TYPE STUDENT AND IT IS A ARRAY OF OBJECT OF SIZE n

do

cout<<"\n Menu \n 1. Create Database \n 2. Display Database \n 3. Copy Constructor\n 4. Default


Constructor \n 5. Delete (Destructor)";

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

cin>>ch;

switch(ch)

case 1: // create database

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

sobj[i].create(pobj[i]);

sobj[i].inccnt();

break;

case 2: // Display Database

{
sobj[0].showcnt(); //to display the total count of students

//we can use any object of student class as it is static member function

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

sobj[i].display(pobj[i]);

break;

case 3: // Copy Constructor

student obj1;

per_info obj2;

obj1.create(obj2);

student obj3(obj1); //invoking copy constructor of student info to copy contents


from object 1 to 3

per_info obj4(obj2); //invoking copy constructor of personal info to


copy contents from object 2 to 4

cout<<"\n Copy Constructor is called ";

obj3.display(obj4);

break;

case 4: // Default Constructor

student obj1; //obj1 is invoking default constructor of class student

per_info obj2; //obj2 is invoking default constructor of class personal

cout<<"\n Default Constructor is called ";

obj1.display(obj2);

break;

case 5: // destructor is called

delete [] sobj; //invoking destructor and delete sobj


dynamically

delete [] pobj; //invoking destructor and delete pobj dynamically

} //end of switch case


cout<<"\n Want to continue:(y/n)" ;

cin>>ans;

}while(ans=='y') ;

return 0;

OUTPUT
ENTER NO OF STUDENTS :

***********************

Menu

1. Create Database

2. Display Database

3. Copy Constructor

4. Default Constructor

5. Delete (Destructor)

Enter your Choice: 1

NAME :

PRERNA GAIKWAD

ADDRESS :

DATE OF BIRTH :

10.08.2005

YEAR :

2024

DIVISION:

A
ROLL NUMBER :

12

BLOOD GROUP :

A+

LICEINCE NUMBER :

535362272J

PHONE NUMBER :

9834219493

Want to continue:(y/n)y

Menu

1. Create Database

2. Display Database

3. Copy Constructor

4. Default Constructor

5. Delete (Destructor)

Enter your Choice: 2

TOTAL NO OF RECORDS ARE : 1

***********************

NAME OF STUDENT : PRERNA

ADDRESS OF STUDENT : GAIKWAD

DATE OF BIRTH : 10.08.2005

YEAR : 2024
DIVISION : A

ROLL NO : 12

BLOOD GROUP : A+

LICEINCE NUMBER : 535362272J

PHONE NUMBER : 9834219493

PRACTICAL NO.3
> Imagine a publishing company which does marketing for book and audio cassette versions.

> Create a class publication that stores the title (a string) and price (type float) of

publications.

> From this class derive two classes: book which adds a page count (type int) and tape

which adds a playing time in minutes (type float).

> Write a program that instantiates the book and tape class, allows user to enter data and

displays the data members. If an exception is caught, replace all the data member values with

zero values.

PROGRAM
#include<iostream>

#include<string>

using namespace std;

class publication

protected:

string title;

float price;

public:

publication()

title=" ";
price=0.0;

publication(string t,float p)

title=t;

price=p;

public:

void getdata()

cout<<"Enter title of publication: ";

cin>>title;

cout<<"Enter price of publication: ";

cin>>price;

void putdata(void)

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

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

};

class book : public publication

int pagecount;

public:

book()

pagecount=0;

//After: base class constructor is called

book(string t,float p,int pc):publication(t,p)

{
pagecount=pc;

void getdata(void)

publication::getdata(); //call publication class function to get getdata

cout <<"Enter Book Page Count :"; //Acquire book data from user

cin>> pagecount;

void putdata(void)

publication::putdata(); //Show Publication data

cout<< "Book page count:"<<pagecount <<endl; // Show book data

};

class CD: public publication

float time1;

public:

CD()

time1=0.0;

//After : base class constructor is called

CD(string t, float p, float tim):publication(t,p)

time1=tim;

void getdata(void)

publication::getdata();

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

cin>> time1;

}
void putdata(void)

publication::putdata();

cout<<" Tape's playing time :"<< time1<<endl;

};

int main()

cout<<endl<<"Book data"<<endl;

book b("C++",230,300);

b.putdata();

cout<<endl<<"CD Data"<<endl;

CD c("C++",100,120.5);

c.putdata();

cout<<"\n Enter New Details Of Book :\n";

b.getdata();

c.getdata();

cout<<"\n\n Book data entered by user:\n";

b.putdata();

c.putdata();

return 0;

OUTPUT
Book data

Publication titles :C++

Publication price :230

Book page count:300

CD Data

Publication titles :C++

Publication price :100

Tape's playing time :120.5


Enter New Details Of Book :

Enter title of publication: xyz

Enter price of publication: 200

Enter Book Page Count :456

Enter title of publication: abc

Enter price of publication: 400

Enter tape's playing time:30

Book data entered by user:

Publication titles :xyz

Publication price :200

Book page count:456

Publication titles :abc

Publication price :400

Tape's playing time :30

---------------------------------------------------------------------------------------------------------------------------------------------------

PRACTICAL NO 4
Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an
input file and read the information from the file.

#include<iostream>

#include<fstream>

using namespace std;

class test

public:

void writedata();

void readdata();

};

void test::writedata()

fstream fp;
char ch;

fp.open("it.txt",ios::out);

cin>>ch;

while (ch!='.')

fp.put(ch);

cin>>ch;

fp.close();

void test::readdata()

fstream fp;

char ch;

fp.open("it.txt",ios::in);

ch=fp.get();

while(!fp.eof())

cout<<ch;

ch=fp.get();

fp.close();

int main()

test ob;

int ch;

do

cout<<"\n1.Write\n2.Read";

cout<<"\nEnter your choice= ";

cin>>ch;

switch(ch)
{

case 1:

ob.writedata();

break;

case 2:

ob.readdata();

break;

}while(ch<3);

OUTPUT
1.Write

2.Read

Enter your choice= 1

PRACTICAL NO 5
Write a function template for selection sort that inputs, sorts and outputs an integer array and

a float array.

PROGRAM
#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<<" "<<A[i];

int main()

int A[size];

float B[size];

int i;

int ch;

do

cout<<"\n* * * * * SELECTION SORT SYSTEM * * * * *";

cout<<"\n--------------------MENU-----------------------";

cout<<"\n1. Integer Values";

cout<<"\n2. Float Values";

cout<<"\n3. Exit";

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

cin>>ch;

switch(ch)

case 1:

cout<<"\nEnter total no of int elements:";

cin>>n;
cout<<"\nEnter int elements:";

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

cin>>A[i];

sel(A);

break;

case 2:

cout<<"\nEnter total no of float elements:";

cin>>n;

cout<<"\nEnter float elements:";

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

cin>>B[i];

sel(B);

break;

case 3:

exit(0);

}while(ch!=3);

return 0;

OUTPUT
* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

Enter your choice : 1


Enter total no of int elements:3

Enter int elements:8

Sorted array: 0 2 8

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

Enter your choice : 2

Enter total no of float elements:3

Enter float elements:2.8

0.1

8.88

Sorted array: 0.1 2.8 8.88

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

Enter your choice : 3

PRACTICAL NO 6
Write C++ Program using STL for sorting and searching user defined records such as item records using vector
container.

PROGRAM
#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

class Item

public:

char name[10];

int quantity;

int cost;

int code;

bool operator==(const Item& i1)

if(code==i1.code)

return 1;

return 0;

bool operator<(const Item& i1)

if(code<i1.code)

return 1;

return 0;

};

vector<Item> o1;

void print(Item &i1);

void display();

void insert();

void search();

void dlt();

bool compare(const Item &i1, const Item &i2)

{
return i1.cost < i2.cost;

int main()

int ch;

do

cout<<"\n*** Menu ***";

cout<<"\n1.Insert";

cout<<"\n2.Display";

cout<<"\n3.Search";

cout<<"\n4.Sort";

cout<<"\n5.Delete";

cout<<"\n6.Exit";

cout<<"\nEnter your choice:";

cin>>ch;

switch(ch)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:

sort(o1.begin(),o1.end(),compare);
cout<<"\n\n Sorted on Cost";

display();

break;

case 5:

dlt();

break;

case 6:

exit(0);

}while(ch!=7);

return 0;

void insert()

Item i1;

cout<<"\nEnter Item Name:";

cin>>i1.name;

cout<<"\nEnter Item Quantity:";

cin>>i1.quantity;

cout<<"\nEnter Item Cost:";

cin>>i1.cost;

cout<<"\nEnter Item Code:";

cin>>i1.code;

o1.push_back(i1);

void display()

for_each(o1.begin(),o1.end(),print);

void print(Item &i1)


{

cout<<"\n";

cout<<"\nItem Name:"<<i1.name;

cout<<"\nItem Quantity:"<<i1.quantity;

cout<<"\nItem Cost:"<<i1.cost;

cout<<"\nItem Code:"<<i1.code;

void search()

vector<Item>::iterator p;

Item i1;

cout<<"\nEnter Item Code to search:";

cin>>i1.code;

p=find(o1.begin(),o1.end(),i1);

if(p==o1.end())

cout<<"\nNot found.";

else

cout<<"\nFound."<<endl;

cout<<"Item Name : "<<p ->name<<endl;

cout<<"Item Quantity : "<<p ->quantity<<endl;

cout<<"Item Cost : "<<p ->cost<<endl;

cout<<"Item Code: "<<p ->code<<endl;

void dlt()

vector<Item>::iterator p;

Item i1;

cout<<"\nEnter Item Code to delete:";

cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);

if(p==o1.end())

cout<<"\nNot found.";

else

o1.erase(p);

cout<<"\nDeleted.";

OUTPUT
*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete

6.Exit

Enter your choice:1

Enter Item Name:laptop

Enter Item Quantity:1

Enter Item Cost:80000

Enter Item Code:3536378

*** Menu ***

1.Insert

2.Display

3.Search
4.Sort

5.Delete

6.Exit

Enter your choice:2

Item Name:laptop

Item Quantity:1

Item Cost:80000

Item Code:3536378

*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete

6.Exit

Enter your choice:3

Enter Item Code to search:3536378

Found.

Item Name : laptop

Item Quantity : 1

Item Cost : 80000

Item Code: 3536378

*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete
6.Exit

Enter your choice:4

Sorted on Cost

Item Name:laptop

Item Quantity:1

Item Cost:80000

Item Code:3536378

*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete

6.Exit

Enter your choice:5

Enter Item Code to delete:3536378

Deleted.

*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete

6.Exit

Enter your choice:3

Enter Item Code to search:3536378


Not found.

*** Menu ***

1.Insert

2.Display

3.Search

4.Sort

5.Delete

6.Exit

Enter your choice:6

PRACTICAL NO 7
Write a program in C++ to use map associative container. The keys will be the names of states and the values
will be the populations of the states. When the program runs, the user is prompted to type the name of a state.
The program then looks in the map, using the state name as an index and returns the population of the state.

PROGRAM
#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