Oop Practical Ranveer
Oop Practical Ranveer
:01
2:
3: Implement a class Complex which represents the Complex
4: Number data type. Implement the following 1. Constructor
5: (including a default constructor which creates the complex
6: number 0+0i). 2. Overload operator+ to add two complex
7: numbers. 3. Overload operator* to multiply two complex numbers.
8: 4. Overload operators << and >> to print and read Complex
Number
9: */
10:
11: // Name :Ranveer Suresh Shinde
12: // Class : SE(comp)
13: // Roll No.:04
14: // Sub :OOP
15:
16: # include<iostream>
17: using namespace
std; 18:
19: class complex
20: {
21:
22:
23:
24: int real,imag;
25: public:
26: complex()
27: { real=0;
28: imag=0;
29:
30: }
31: complex operator +(complex c)
32: { complex obj;
33: obj.real = real + c.real ;
34: obj.imag = imag + c.imag ;
35: return(obj);
36: }
37: complex operator *(complex c)
38: { complex obj1; obj1.real = (real*c.real)-
39: (imag*c.imag); obj1.imag =
40: (imag*c.real)+(real*c.imag);
41: return(obj1);
42: }
43: friend istream & operator >> (istream & input ,complex &t)
44: {
45: cout<<"enter the real part :";
46: input>>t.real;
47: cout<<"enter the imagnary part :";
48: input>>t.imag;
49: }
50:
51: friend ostream & operator << (ostream & output ,complex &t)
52: {
53: output<<t.real<<" + i "<<t.imag;
54: }
55:
56: };
57: int main()
58: {
59: complex c1,c2,c3,c4;
60: cout<<"Enter real and imagnary part of first no. :"<<endl;
61: cin>>c1;
62: cout<<"Enter real and imagnary part of second no. :"<<endl;
63: cin>>c2;
64: c3=c1+c2;
65: c4=c1*c2;
66: cout<<"Addition ="<<c3<<endl; 67:
cout<<"Multiplication ="<<c4;
68: return 0;
69:
70: }
71: // OUTPUT=>
72: /*
73: Enter real and imagnary part of first no. :
74: enter the real part :7
75: enter the imagnary part :8
76: Enter real and imagnary part of second no. :
77: enter the real part :3
78: enter the imagnary part :5
79: Addition =10 + i 13
80: Multiplication =-19 + i 59
81: */
1: /* Assignment No.:02
2:
3: Develop a program in C++ to create a database of student’s
4: information system containing the following information:
5: Name, Roll number, Class, Division, Date of Birth, Blood group,
6: contact address, Telephone number, Driving license no. and other.
7: Construct the database with suitable member functions. Make
8: use of constructor, default constructor, copy constructor,
9: destructor, static member functions, friend class, this pointer,
10: inline code and dynamic memory allocation operators-new and
delete
11: as well as exception handling.
12: */
13:
14: // Name: Ranveer Suresh Shinde
15: // Class: SE (comp)
16: // Roll No :04
17: // Sub: OOP
19: #include <iostream>
20: using namespace std;
21:
22: class student
23: {
24: public:
25: char name[20];
26: int rollno;
27: char class1[5]; char
28: division[5]; long DOB;
29: char blood_group[5];
30: char
31: contact_address[50];
32: long telephone_no; long
33: driving_license_no; void
34: accept(); void
35: display();
36: };
37: void student::accept()
38: {
39: cout<<"Enter name of the student:"<<endl;
40: cin>>name;
41: cout<<"Enter roll no. of the student:"<<endl;
42: cin>>rollno;
43: cout<<"Enter class of the student:"<<endl;
44: cin>>class1;
45: cout<<"Enter division of the student:"<<endl;
46: cin>>division;
47: cout<<"Enter DOB of the student:";
48: cin>>DOB;
49: cout<<"Enter Blood group of the student:"<<endl;
50: cin>>blood_group;
51: cout<<"Enter contact address of the student:"<<endl;
52: cin>>contact_address;
53: cout<<"Enter telephone number of the
54: student:"<<endl; cin>>telephone_no;
55: cout<<"Enter driving license number of the student:"<<endl;
56: cin>>driving_license_no;
57: }
58: void student::display()
59: {
60: cout<<"name of the student:"<<name<<endl;
61: cout<<"roll no. of the student:"<<rollno<<endl;
62: cout<<"class of the student:"<<class1<<endl;
63: cout<<"division of the student:"<<division<<endl;
64: cout<<"DOB of the student:"<<DOB<<endl;
65: cout<<"Blood group of the student:"<<blood_group<<endl;
66: cout<<"contact address of the student:"<<contact_address<<endl;
67: cout<<"telephone number of the student:"<<telephone_no<<endl;
68: cout<<"driving license number of the student:"<<driving_license_no<<endl;
69:
70: }
71: int main()
72: {
73: student obj;
74: obj.accept();
75: obj.display();
76: return 0;
77: }
78:
79: // OUTPUT =>
80:
81: /*
82: Enter name of the student:
83: Pranav
84: Enter roll no. of the student:
85: 19
86: Enter class of the student:
87: SE
88: Enter division of the student:
89: A
90: Enter DOB of the student:19072004
91: Enter Blood group of the student:
92: AB+
93: Enter contact address of the student:
94: Baramati
95: Enter telephone number of the student:
96: 123456
97: Enter driving license number of the student:
98: 778899
99: name of the student:Pranav
100: roll no. of the student:19
101: class of the student:SE
102: division of the student:A
103: DOB of the student:19072004
104: Blood group of the student:AB+
105: contact address of the student:Baramati
106: telephone number of the student:123456
107: driving license number of the student:778899
108: */
1: /* Assignment No.:03
2:
3: Imagine a publishing company which does marketing
4: for book and audio cassette versions. Create a
class 5: publication that stores the title (a string)
and
6: price (type float) of publications. From this
class 7: derive two classes: book which adds a page
count 8: (type int) and tape which adds a playing
time in
9: minutes (type float). Write a program that
instantiates 10: the book and tape class, allows user to
enter data and 11: displays the data members. If an
exception is caught,
12: replace all the data member values with zero values.
13: */
14:
15: // Name : Ranveer Suresh Shinde
16: // Class :SE(comp)
17: // Roll No.:04
18: // Sub :OOP
19:
20: #include<iostream>
21: using namespace std;
22: public:
25:
23: class
publication 26:
24: { string title;
27: float price;
28: publication()
29: { title=" ";
30: price=00.00;
31: }
32: publication(string t, float
33: p)
34: { title=t;
35: price=p;
36: }
37:
38: };
39:
40: class book:public publication
41: {
42: public:
43: int pagecount;
44: book()
45: {
46: pagecount=0;
47: }
48: book(string t, float p,int c):publication(t,p)
49: {
50: pagecount=c;
51: }
52: void display()
53: {
54: cout<<"title="<<title<<endl; 55:
cout<<"price="<<price<<endl;
56: cout<<"pagecount="<<pagecount<<endl;
57: }
58: };
59:
60: class tape:public publication
61: {
62: public:
63: int rcd;
64: tape()
65: { rcd=0;
66: }
67: tape(string t,float p,int
68: r):publication(t,p)
69: { rcd=r;
70: }
71: void display()
72: { cout<<"title="<<title<<endl;
73: cout<<"price="<<price<<endl;
74: cout<<"rcd="<<rcd<<endl; }
75:
76:
77:
78: }
; 79:
80: int main()
81: {
82: int c,r;
83: string t;
84: float p;
85: cout<<"enter the title:";
86: cin>>t;
87: cout<<"enter the price:";
88: cin>>p;
89: cout<<"enter the
pagecount:";
90: cin>>c;
91: cout<<"enter the
recording:";
92: cin>>r;
93: cout<<"book data:"<<endl;
94: book obj(t,p,c);
95: obj.display();
96:
97:
tape obj1(t,p,r);
98:
obj1.display();
99:
100: return 0; 101: }
102: // OUTPUT=>
103: /*
104: enter the title:Ekigai
105: enter the price:120
106: enter the pagecount:180
107: enter the recording:140
108: book data:
109: title=Ekigai
110: price=120
111: pagecount=180
112: title=Ekigai
113: price=120
114: rcd=140
115: */
116:
117:
1: /* Assignment No.:04
2:
3: Write a C++ program that creates an output file, write
4: information to it, closes the file, open it again as a
5: input file and read the information from the file.
6: */
7:
8: // Name : Ranveer Suresh Shinde
9: // Class :SE(comp)
10: // Roll No.:04
11: // Sub :OOP
12:
13: #include<iostream>
14: #include<fstream>
15: using namespace std;
16: int main()
17: { ofstream outfile;
18: outfile.open("temp.txt",ios::app);
19: string s;
20: while(1)
21: { cout<<"enter 1-line 0-exit"<< endl;
22: int ch;
23: cout<<"enter
24: choice:"; cin>>ch;
25: if(ch==1)
26: { cout<<"enter text :"<<endl;
27: cin.get();
28: getline(cin,s);
29: outfile<<s<<endl;
30: }
31: else if(ch==0)
32: { cout<<"exited :"<<endl;
33: break;
34: }
35: else
36:
37:
38:
39:
1: /* Assignment No.:06
2:
3: Write C++ program using STL for sorting and searching
4: user defined records such as Item records (Item code,
5: name, cost, quantity etc) using Vector container.
6: */
7:
8: // Name : Ranveer Suresh Shinde
9: // Class :SE(comp)
10: // Roll No.:04
11: // Sub :OOP
12:
13: #include <iostream>
14: #include <algorithm>
15: #include <vector>
16: using namespace std;
17: class Item
18: {
19: public:
20: char name[10];
21: int quantity;
22: int cost;
23: int code;
24: bool operator==(const Item& i1)
25: {
26: if(code==i1.code)
27: return 1;
28: return 0;
29: }
30: bool operator<(const Item& i1)
31: {
32: if(code<i1.code)
33: return 1;
34: return 0;
35: }
36: };
37: vector<Item> o1;
38: void print(Item &i1);
39: void display();
40: void insert();
41: void search();
42: bool compare(const Item &i1, const Item &i2)
43: {
44: return i1.cost < i2.cost;
45: }
46: int main()
47: {
48: int ch;
49: while(1)
50: {
51: cout<<"\n1.Insert";
52: cout<<"\n2.Display";
53: cout<<"\n3.Search";
54: cout<<"\n4.Sort";
55: cout<<"\n5.Exit";
56: cout<<"\nEnter your choice:";
57: cin>>ch;
58: switch(ch)
59: {
60: case 1:
61: insert();
62: break;
63:
64: case 2:
65: display();
66: break;
67:
68: case 3:
69: search();
70: break;
71: case 4:
72: sort(o1.begin(),o1.end(),compare);
73: cout<<"\n\n Sorted on Cost";
74: display();
75: break;
76: case 5:
77: break;
78: }
79: }
80: return 0;
81: }
82: void insert()
83: {
84: Item i1;
85: cout<<"\nEnter Item Name:";
86: cin>>i1.name;
87: cout<<"\nEnter Item
Quantity:";
88: cin>>i1.quantity;
89: cout<<"\nEnter Item Cost:";
90: cin>>i1.cost;
91: cout<<"\nEnter Item Code:";
92: cin>>i1.code;
93: o1.push_back(i1);
94: }
95: void display()
96: {
97: for_each(o1.begin(),o1.end(),print);
98: }
99: void print(Item &i1)
100: {
101: cout<<"\n";
102: cout<<"\nItem Name:"<<i1.name;
103: cout<<"\nItem Quantity:"<<i1.quantity;
104: cout<<"\nItem Cost:"<<i1.cost;
105: cout<<"\nItem Code:"<<i1.code;
106: }
107: void search()
108: {
109: vector<Item>::iterator p;
110: Item i1;
111: cout<<"\nEnter Item Code to search:";
112: cin>>i1.code;
113: p=find(o1.begin(),o1.end(),i1);
114: if(p==o1.end())
115: {
116: cout<<"\nNot found.";
117: }
118: else
119: {
120: cout<<"\nFound."<<endl;
121: cout<<"Item Name : "<<p ->name<<endl;
122: cout<<"Item Quantity : "<<p ->quantity<<endl;
123: cout<<"Item Cost : "<<p ->cost<<endl;
124: cout<<"Item Code: "<<p ->code<<endl;
125: }
126: }
127: //OUTPUT=>
128: /*
129: 1.Insert
130: 2.Display
131: 3.Search
132: 4.Sort
133: 5.Exit
134: Enter your choice:1
135: Enter Item Name:book
136: Enter Item Quantity:546
137: Enter Item Cost:54633
138: Enter Item Code:653963
139:
140: 1.Insert
141: 2.Display
142: 3.Search
143: 4.Sort
144: 5.Exit
145: Enter your choice:1
146: Enter Item Name:pen
147: Enter Item Quantity:63
148: Enter Item Cost:500
149: Enter Item
Code:136533 150:
151: 1.Insert
152: 2.Display
153: 3.Search
154: 4.Sort
155: 5.Exit
156: Enter your choice:1
157: Enter Item Name:pencile
158: Enter Item Quantity:696
159: Enter Item Cost:5000
160: Enter Item Code:6894333
161:
162: 1.Insert
163: 2.Display
164: 3.Search
165: 4.Sort 166: 5.Exit
167: Enter your
choice:2 168:
169: Item Name:book
170: Item Quantity:546
171: Item Cost:54633
172: Item Code:653963
173:
174: Item Name:pen
175: Item Quantity:63
176: Item Cost:500
177: Item Code:136533
178:
179: Item Name:pencile
180: Item Quantity:696
181: Item Cost:5000
182: Item Code:6894333
183: 1.Insert
184: 2.Display
185: 3.Search
186: 4.Sort
187: 5.Exit 188: Enter
your choice:3 189:
190: Enter Item Code to search:136533
191: Found.
192: Item Name : pen
193: Item Quantity : 63
194: Item Cost : 500
195: Item Code: 136533
196: 1.Insert
197: 2.Display
198: 3.Search
199: 4.Sort
200: 5.Exit
201: Enter your choice:4
202: Sorted on Cost
203:
204: Item Name:pen
205: Item Quantity:63
206: Item Cost:500
207: Item Code:136533
208:
209: Item Name:pencile
210: Item Quantity:696
211: Item Cost:5000
212: Item Code:6894333
213:
214: Item Name:book
215: Item Quantity:546
216: Item Cost:54633
217: Item Code:653963
218: */
1: /* Assignment No.:07
2:
3: Write a program in C++ to use map associative
container. 4: The keys will be the names of states and the
values will 5: be the populations of the states. When the
program runs, 6: the user is prompted to type the name of
a state. The
7: program then looks in the map, using the state name
as an
8: index and returns the population of the state.
9: */
10:
11: // Name : Ranveer Suresh Shinde
12: // Class :SE(comp)
13: // Roll No.:04
14: // Sub :OOP
15:
16:
#include<iostream>
17: #include<map> 18:
19: using namespace std;
20:
21: int main()
23: map<string,int> state;
22: { state.insert(pair<string,int>("Maha",562565));
24: state.insert(pair<string,int>("Gujrat",45565));
25: state.insert(pair<string,int>("karnatka",15654));
26: state.insert(pair<string,int>("Goa",12854));
27: state.insert(pair<string,int>("UP",54564));
28:
29: string search;
30: cout<<"enter string to be search :";
31: cin>>search;
32:
33: map<string,int>::iterator i; int
34: count=0; for(i=state.begin();i!
35: =state.end();i++)
36: { if (search==i->first)
37: { count++; cout<<"present=>"<<i-
38: >first<<endl; }
39:
40:
41:
42:
43: }
44:
45: if(count==0)
46: {
47: cout<<"not present"<<endl;
48: }
49: return 0;
50: }
51: // OUTPUT=>
52: /*
53: enter string to be search :Goa
54: present=>Goa
55: */