Object Oriented Programming (LAB) Comp (ONPO121A)
Object Oriented Programming (LAB) Comp (ONPO121A)
Object Oriented Programming (LAB) Comp (ONPO121A)
(LAB)(Code : 210247)
Semester III - Computer Engineering
Copyright © by Authors. All rights reserved. No part of this publication may be reproduced, copied, or stored in a
retrieval system, distributed or transmitted in any form or by any means, including photocopy, recording, or other
electronic or mechanical methods, without the prior written permission of the publisher.
This book is sold subject to the condition that it shall not, by the way of trade or otherwise, be lent, resold, hired out, or
otherwise circulated without the publisher’s prior written consent in any form of binding or cover other than which it is
published and without a similar condition including this condition being imposed on the subsequent purchaser and without
limiting the rights under copyright reserved above.
This edition is for sale in India, Bangladesh, Bhutan, Maldives, Nepal, Pakistan, Sri Lanka and designated countries in
South-East Asia. Sale and purchase of this book outside of these countries is unauthorized by the publisher.
ISBN : 978-93-89889-53-6
Published by
TechKnowledge Publications
Head Office : B/5, First floor, Maniratna Complex, Taware Colony, Aranyeshwar Corner,
Pune - 411 009. Maharashtra State, India
Ph : 91-20-24221234, 91-20-24225678.
Email : [email protected],
Website : www.techknowledgebooks.com
Course Objectives :
To understand basics of Computer Graphics, apply various methods and techniques for implementing line-circle
drawing, projections, animation, shading, illumination and lighting using concepts of Object Oriented Programming.
List of Assignments
Group A
Implement a class Complex which represents the Complex Number data type. Implement the following
1.
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
Develop a program in C++ to create a database of student’s information system containing the following
2.
information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address,
Telephone number, Driving license no. and other. Construct the database with suitable member functions.
Make use of constructor, default constructor, copy constructor, destructor, static member functions, friend
class, this pointer, inline code and dynamic memory allocation operators-new and delete as well as
exception handling.
Imagine a publishing company which does marketing for book and audio cassette versions. Create a class
3.
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.
Group B
Write a C++ program that creates an output file, writes information to it, closes the file, open it again as
4.
an input file and read the information from the file.
Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array.
5.
Group C
Write C++ program using STL for sorting and searching user defined records such as personal records
6.
(Name, DOB, Telephone number etc) using vector container.
OR
Write C++ program using STL for sorting and searching user defined records such as Item records (Item
code, name, cost, quantity etc) using vector container.
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.
INDEX
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 multiply two complex numbers. 4. Overload operators << and >> to print and read
Complex Numbers.
Develop a program in C++ to create a database of student’s information system containing the following information: Name,
Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone number, Driving license no. and other.
Construct the database with suitable member functions. Make use of constructor, default constructor, copy constructor,
destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and
delete as well as exception handling.
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.
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.
Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array.
Write C++ program using STL for sorting and searching user defined records such as personal records (Name, DOB,
Telephone number etc) using vector container.
OR
Write C++ program using STL for sorting and searching user defined records such as Item records (Item code, name, cost,
quantity etc) using vector container.
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.
Lab Manual
Lab Manual
e
ns g
Part I : Object Oriented Programming
io ed
Group A
Program L1
at wl
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 multiply two complex numbers. 4. Overload operators <<
and >> to print and read Complex Numbers.
ic o
Solution :
#include<iostream>
bl kn
#include<stdlib.h>
using namespace std;
class complex_number
{
float r;
Pu ch
float i;
public:
complex_number()
{
Te
r=0.0;
i=0.0;
}
complex_number(float temp1, float temp2)
{
r=temp1;
i=temp2;
}
//display number
friend void operator<<(ostream &a, complex_number &b)
{
a<<"\n"<<b.r<<" + "<<b.i<<" i";
}
friend istream &operator>>( istream &input, complex_number &num )
{
Object Oriented Programming (SPPU-Comp) L-2 Lab Manual
input >>num.r>>num.i;
return input;
}
complex_number operator+(complex_number);
complex_number operator*(complex_number);
};
e
complex_number complex_number::operator +(complex_number A)
ns g
{
complex_number B;
io ed
B.r=r+A.r;
B.i=i+A.i;
return B;
at wl
}
complex_number complex_number::operator *(complex_number A)
{
complex_number B;
ic o
B.r=r-A.r;
B.i=i-A.i;
bl kn
B.r=((r)*(A.r))-((i)*(A.i));
B.i=((r)*(A.i))+((A.r)*(i));
return B;
Pu ch
}
int main()
{
int ch;
Te
complex_number A;
complex_number B;
complex_number C;
cout<<"\n Operations on complex numbers";
while(1)
{
cout<<"\n Menu";
cout<<"\n1. Create constructor";
cout<<"\n2. Accept numbers";
cout<<"\n3. Display numbers";
cout<<"\n4. Add numbers";
cout<<"\n5. Multiply numbers";
Object Oriented Programming (SPPU-Comp) L-3 Lab Manual
cout<<"\n6. Exit";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
e
cout<<"\n Complex number after initialization ";
cout<<A;
ns g
cout<<B;
break;
io ed
case 2:
cout<<"\n Enter first complex number (First real and then imaginary part): ";
cin>>A;
at wl cout<<"\n Enter second complex number (First real and then imaginary part): ";
cin>>B;
break;
case 3:
ic o
cout<<"\n First complex number";
cout<<A;
bl kn
case 4:
C=A+B;
cout<<"\n Addition of two complex numbers is";
cout<<C;
Te
break;
case 5:
C=A*B;
cout<<"\n Multiplication of two complex numbers is";
cout<<C;
break;
case 6:
exit(0);
}
}
return 0;
}
Object Oriented Programming (SPPU-Comp) L-4 Lab Manual
Output :
e
3. Display numbers
4. Add numbers
ns g
5. Multiply numbers
6. Exit
io ed
Enter your choice: 1
Complex number after initialization
0+0i
at wl
0+0i
Menu
1. Create constructor
ic o
2. Accept numbers
3. Display numbers
bl kn
4. Add numbers
5. Multiply numbers
6. Exit
Pu ch
1. Create constructor
2. Accept numbers
3. Display numbers
4. Add numbers
5. Multiply numbers
6. Exit
Enter your choice: 3
First complex number
5 + 10.2 i
Second complex number
Object Oriented Programming (SPPU-Comp) L-5 Lab Manual
15 + 20.4 i
Menu
1. Create constructor
2. Accept numbers
3. Display numbers
e
4. Add numbers
ns g
5. Multiply numbers
6. Exit
io ed
Enter your choice: 4
Addition of two complex numbers is
20 + 30.6 i
at wl
Menu
1. Create constructor
2. Accept numbers
ic o
3. Display numbers
bl kn
4. Add numbers
5. Multiply numbers
6. Exit
Pu ch
Menu
1. Create constructor
2. Accept numbers
3. Display numbers
4. Add numbers
5. Multiply numbers
6. Exit
Object Oriented Programming (SPPU-Comp) L-6 Lab Manual
Program L2
Develop a program in C++ to create a database of student’s information system containing the following
information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone
number, Driving license no. and other. Construct the database with suitable member functions. Make use of
constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer,
inline code and dynamic memory allocation operators-new and delete as well as exception handling.
e
Solution :
#include<iostream>
ns g
#include<string>
#include<cstring>
io ed
#include<stdlib.h>
using namespace std;
class student
{
at wl
int roll;
char name[30];
float marks;
ic o
public:
student()
bl kn
{
roll=0;
marks=0;
strcpy(name, "");
Pu ch
}
student(int roll, char name[30], float marks)
{
this->roll=roll;
Te
strcpy(this->name, name);
this->marks=marks;
}
void accept()
{
cout<<"\n Enter name: ";
cin>>name;
cout<<"\n Enter Roll Number: ";
cin>>roll;
cout<<"\n Enter marks: ";
cin>>marks;
}
Object Oriented Programming (SPPU-Comp) L-7 Lab Manual
void display()
{
cout<<"\n\n Name: "<<name;
cout<<"\n Roll Number: "<<roll;
cout<<"\n Marks: "<<marks;
}
e
};
int main()
ns g
{
int i,ch, n;
io ed
student s[10] ;
while(1)
{
cout<<"\n1.Create\n2.Display\n3. Exit";
at wl
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
ic o
case 1:
cout<<"\n Enter how many student information you want to store: ";
bl kn
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Enter information of "<<i+1 <<"th student:" ;
Pu ch
s[i].accept();
}
break;
case 2:
Te
Output :
1.Create
2.Display
3. Exit
Enter your choice: 1
e
Enter how many student information you want to store: 3
Enter information of 1th student:
ns g
Enter name: dmjadhav
Enter Roll Number:10
io ed
Enter marks:66.66
Enter information of 2th student:
Enter name: maansari
at wl
Enter Roll Number:11
Enter marks:66.66
Enter information of 3th student:
ic o
Enter name: xyz
Enter Roll Number:12
bl kn
Enter marks: 66
1.Create
2.Display
Pu ch
3.Exit
Enter your choice:2
Student database is as follows
Name: dmjadhav
Te
Roll Number:10
Marks:66.66
Name:maansari
Roll Number: 11
Marks: 66.66
Name: xyz
Roll Number: 12
Marks: 66
Object Oriented Programming (SPPU-Comp) L-9 Lab Manual
Program L3
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.
Solution :
e
#include<iostream>
#include<stdexcept>
ns g
#include<typeinfo>
#include<string>
io ed
#include<cstring>
#include<stdlib.h>
using namespace std;
at wl
class publish
{
public:
char title[20];
ic o
float price;
void getdata()
bl kn
{
cout<<"\n Enter title and price : ";
cin>>title>>price;
}
Pu ch
void putdata()
{
cout<<"\n Information of publication: ";
Te
void putdata(publish p)
{
cout<<"\n Information of book: ";
cout<<"\nTitle: "<<p.title<<"\t Price:"<<p.price<<"\t Page count"<<page_count;
}
};
e
class CD: public publish
{
ns g
float play_time;
public:
io ed
void getdata()
{
cout<<"\n Enter play time : ";
at wl
cin>>play_time;
}
void putdata(publish p)
{
ic o
cout<<"\n Information of CD: ";
cout<<"\n Title: "<<p.title<<"\t Price:"<<p.price<<"\t Page count"<<play_time;
bl kn
}
};
int main()
Pu ch
{
int ch;
publish p;
book b;
Te
CD c;
while(1)
{
cout<<"\n1. Enter publication information";
cout<<"\n2. Enter book info";
cout<<"\n3. Enter CD info";
cout<<"\n4. Exit";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
Object Oriented Programming (SPPU-Comp) L-11 Lab Manual
p.getdata();
p.putdata();
break;
case 2:
b.getdata();
b.putdata(p);
e
break;
case 3:
ns g
c.getdata();
c.putdata(p);
io ed
break;
case 4:
exit(0);
at wl
}
}
return 0;
}
ic o
Output :
bl kn
3. Enter CD info
4. Exit
Enter your choice3
Enter play time : 56.5
Information of CD:
e
Title: techmax Price:34543 Page count56.5
ns g
Group B
io ed
Program L4
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.
at wl
Solution :
#include<iostream>
#include<fstream>
ic o
using namespace std;
class file
bl kn
{
char name[20];
int emp_id;
float salary;
Pu ch
public:
void accept()
{
Te
cin>>name;
cin>>emp_id;
cin>>salary;
}
void display()
{
cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;
}
};
int main()
{
Object Oriented Programming (SPPU-Comp) L-13 Lab Manual
file o[5];
fstream f;
int i,n;
f.open("input"); //create file
cout<<"\n How many employee information wanted to store:";
e
cin>>n;
cout<<"\n Enter information of 3 employees (Enter name, emp_id, salary)";
ns g
for(i=0;i<n;i++)
{
io ed
cout<<"\n Enter information of "<<i<<" employee ";
o[i].accept(); //accept input from user
f.write((char* ) &o[i], sizeof(o[i])); //write object in file
at wl
}
f.close();
f.open("input",ios::in);
ic o
cout<<"\n Information of employee is as follows";
for(i=0;i<n;i++)
bl kn
{
f.read((char* ) &o[i], sizeof(o[i])); //read data from file
o[i].display(); //display data
Pu ch
}
f.close();
return 0;
}
Te
Output :
Program L5
Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array.
Solution :
#include<iostream>
using namespace std;
template<typename T>
e
void sort(T a[], int n)
ns g
{
int pos_min,temp, i;
io ed
for (int i=0; i < n-1; i++)
{
pos_min = i;
at wl for (int j=i+1; j < n; j++)
{
if (a[j] < a[pos_min])
ic o
pos_min=j;
}
bl kn
if (pos_min != i)
{
temp = a[i];
a[i] = a[pos_min];
Pu ch
a[pos_min] = temp;
}
}
//display sorted elements
Te
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
sort<int>(a, n);
cout<<"\n Sorting floating point numbers";
cout<<"\n Enter how many number wanted to sort";
e
cin>>n;
for(i=0;i<n;i++)
ns g
cin>>b[i];
sort<float>(b, n);
io ed
return 0;
}
Output :
at wl
Selection sort using function template
Sorting int numbers
Enter how many number wanted to sort5
ic o
54321
Sorted elements are :
bl kn
1 2 3 4 5
Sorting floating point numbers
Enter how many number wanted to sort5
5.5 6.6 456.44 2.2 1.2
Pu ch
Group C
Te
Program L6
Write C++ program using STL for sorting and searching user defined records such as personal records (Name,
DOB, Telephone number etc) using vector container.
OR
Write C++ program using STL for sorting and searching user defined records such as Item records (Item code,
name, cost, quantity etc) using vector container.
Solution :
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
Object Oriented Programming (SPPU-Comp) L-16 Lab Manual
e
{
public:
ns g
int rollno;
string name;
io ed
char mob[20];
bool operator==(const student &student1)
{
at wlreturn(rollno==student1.rollno);
}
bool operator<(const student &student1)
{
ic o
return(rollno<student1.rollno);
}
bl kn
out<<"\n\t\t"<<k.rollno<<"\t\t"<<k.name<<"\t\t"<<k.mob;
return out;
}
// overloading operator >>
istream & operator >> (istream &in , student &k)
{
cout<<"\nEnter Roll No : ";
in>>k.rollno;
cout<<"\nEnter Name : ";
in>>k.name;
cout<<"\nEnter mob : ";
in>>k.mob;
Object Oriented Programming (SPPU-Comp) L-17 Lab Manual
return in;
}
e
}
// function to accept record
ns g
vector<student> read()
{
io ed
int n;
student k;
vector<student> j;
at wl
cout<< "\nEnter total no. of students : ";
cin>>n;
for(int i=0;i<n;i++)
{
ic o
cin>>k;
j.push_back(k);
bl kn
}
return j;
}
Pu ch
}
// function to search record
void search( vector<student>&j )
{
Object Oriented Programming (SPPU-Comp) L-18 Lab Manual
student k;
cout<<"\nEnter Student Roll No To Search : ";
cin>>k.rollno;
cout<<"\n\n\t\tROLL NO\t\tNAME\t\tDATE OF BIRTH";
vector<student>::iterator p;
p=find(j.begin(),j.end(),k);
e
if(p!=j.end())
cout<<*p;
ns g
else
cout<<"\nNot found ";
io ed
}
// Main Function
bl kn
int main()
{
vector<student> s;
Pu ch
int ch;
do
{
cout<<"\n\n\t\t* * * * Personal Record Database * * * *";
Te
cout<<"\n\t\t1.Create Record";
cout<<"\n\t\t2.Display Record ";
cout<<"\n\t\t3.Search Record";
cout<<"\n\t\t4.Sort Record";
cout<<"\n\t\t5.Quit Menu";
cout<<"\n\t\t--------------------------";
cout<<"\n\t\tEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
s=read();
Object Oriented Programming (SPPU-Comp) L-19 Lab Manual
break;
case 2:
print(s);
break;
case 3:
search(s);
e
break;
case 4:
ns g
sort(s);
print(s);
io ed
break;
}
}while(ch!=5);
at wl
} // end of the main function
Output :
ic o
* * * * Personal Record Database * * * *
1.Create Record
bl kn
2.Display Record
3.Search Record
4.Sort Record
5.Quit Menu
Pu ch
--------------------------
Enter your choice : 1
e
Enter Roll No : 102
ns g
Enter Name : neeta
io ed
Enter mob : 2343123211
at wl
* * * * Personal Record Database * * * *
1.Create Record
2.Display Record
ic o
3.Search Record
4.Sort Record
bl kn
5.Quit Menu
--------------------------
Enter your choice : 2
Pu ch
e
2.Display Record
3.Search Record
ns g
4.Sort Record
5.Quit Menu
io ed
--------------------------
Enter your choice : 4
at wl
ROLL NO NAME Mobile Number
100 geeta 7678456432
101 seeta 5432123427
102 neeta 2343123211
ic o
103 rachana 1223122211
bl kn
3.Search Record
4.Sort Record
5.Quit Menu
--------------------------
Te
*/
Program L7
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.
Solution :
#include <iostream>
#include <map>
#include <string>
#include <utility>
Object Oriented Programming (SPPU-Comp) L-22 Lab Manual
int main()
{
// initialize container
typedef map<string, int> mapType;
e
mapType populationMap;
ns g
// insert elements in map
populationMap.insert(pair<string, int>("Maharashtra", 123144223));
io ed
populationMap.insert(pair<string, int>("Rajasthan", 81032689));
populationMap.insert(pair<string, int>("Gujarat", 63872399));
populationMap.insert(pair<string, int>("Karanataka", 67562686));
at wl
populationMap.insert(pair<string, int>("Tamilnadu", 77841267));
mapType::iterator iter;
ic o
// display the size of the map
cout << " * * * * Population of states in India * * * * \n";
bl kn
string state_name;
cout << "\nEnter name of the state : ";
Te
cin>> state_name;
iter = populationMap.find(state_name);
if( iter != populationMap.end() )
cout << state_name <<"s populations is " << iter->second ;
else
cout << "Key is not in populationMap" << '\n';
Output :
e
* * * * Population of states in India * * * *
ns g
Size of populationMap: 5
Enter name of the state : Maharashtra
io ed
Maharashtra's populations is 123144223
at wl
ic o
bl kn
Pu ch
Te