k

k / B64 OOP

Last active 2 weeks ago

Like 0

k revised this gist 2 weeks ago · 3a92350

No changes

k revised this gist 3 weeks ago · 4bccb97

No changes

k revised this gist 3 weeks ago · bf7fcb0

10 files changed, 826 insertions

exp1.cpp (file created)
@@ -0,0 +1,40 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Student
5 + {
6 + private:
7 + int roll;
8 + string name;
9 + float marks;
10 +
11 + public:
12 + void getData()
13 + {
14 + cout << "Enter roll no: ";
15 + cin >> roll;
16 + cout << "Enter name: ";
17 + cin >> name;
18 + cout << "Enter marks: ";
19 + cin >> marks;
20 + }
21 +
22 + void showData()
23 + {
24 + cout << "\nRoll No: " << roll << endl;
25 + cout << "Name: " << name << endl;
26 + cout << "Marks: " << marks << endl;
27 + if (marks >= 40)
28 + cout << "Result: Pass" << endl;
29 + else
30 + cout << "Result: Fail" << endl;
31 + }
32 + };
33 +
34 + int main()
35 + {
36 + Student s;
37 + s.getData();
38 + s.showData();
39 + return 0;
40 + }
exp10.cpp (file created)
@@ -0,0 +1,189 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Consumer
5 + {
6 + protected:
7 + int id;
8 + string name;
9 + int prevReading, currReading;
10 +
11 + public:
12 + void getData()
13 + {
14 + cout << "Enter consumer id: ";
15 + cin >> id;
16 + cout << "Enter name: ";
17 + cin >> name;
18 + cout << "Enter previous reading: ";
19 + cin >> prevReading;
20 + cout << "Enter current reading: ";
21 + cin >> currReading;
22 + while (currReading < prevReading)
23 + {
24 + cout << "Current reading cannot be less than previous. Enter again: ";
25 + cin >> currReading;
26 + }
27 + }
28 +
29 + int units()
30 + {
31 + return currReading - prevReading;
32 + }
33 +
34 + int getId()
35 + {
36 + return id;
37 + }
38 +
39 + virtual float bill() = 0;
40 + virtual string type() = 0;
41 +
42 + void showBill()
43 + {
44 + cout << "\n-------- ELECTRICITY BILL --------" << endl;
45 + cout << "Consumer ID : " << id << endl;
46 + cout << "Name : " << name << endl;
47 + cout << "Type : " << type() << endl;
48 + cout << "Units consumed: " << units() << endl;
49 + cout << "Bill amount : Rs. " << bill() << endl;
50 + cout << "----------------------------------" << endl;
51 + }
52 +
53 + virtual ~Consumer() {}
54 + };
55 +
56 + class Domestic : public Consumer
57 + {
58 + public:
59 + float bill()
60 + {
61 + int u = units();
62 + float amt;
63 + if (u <= 100)
64 + amt = u * 3.5;
65 + else if (u <= 300)
66 + amt = 100 * 3.5 + (u - 100) * 5.0;
67 + else
68 + amt = 100 * 3.5 + 200 * 5.0 + (u - 300) * 7.5;
69 + return amt + 50;
70 + }
71 + string type()
72 + {
73 + return "Domestic";
74 + }
75 + };
76 +
77 + class Commercial : public Consumer
78 + {
79 + public:
80 + float bill()
81 + {
82 + int u = units();
83 + float amt;
84 + if (u <= 200)
85 + amt = u * 7.0;
86 + else
87 + amt = 200 * 7.0 + (u - 200) * 9.5;
88 + return amt + 150;
89 + }
90 + string type()
91 + {
92 + return "Commercial";
93 + }
94 + };
95 +
96 + class Industrial : public Consumer
97 + {
98 + public:
99 + float bill()
100 + {
101 + return units() * 8.5 + 500;
102 + }
103 + string type()
104 + {
105 + return "Industrial";
106 + }
107 + };
108 +
109 + int main()
110 + {
111 + Consumer *list[50];
112 + int n = 0, ch, t, id;
113 +
114 + do
115 + {
116 + cout << "\n===== Electricity Billing System =====" << endl;
117 + cout << "1. Add consumer" << endl;
118 + cout << "2. Show all bills" << endl;
119 + cout << "3. Search by consumer id" << endl;
120 + cout << "4. Exit" << endl;
121 + cout << "Enter choice: ";
122 + cin >> ch;
123 +
124 + switch (ch)
125 + {
126 + case 1:
127 + if (n >= 50)
128 + {
129 + cout << "Limit reached" << endl;
130 + break;
131 + }
132 + cout << "1. Domestic 2. Commercial 3. Industrial" << endl;
133 + cout << "Enter type: ";
134 + cin >> t;
135 + if (t == 1)
136 + list[n] = new Domestic;
137 + else if (t == 2)
138 + list[n] = new Commercial;
139 + else if (t == 3)
140 + list[n] = new Industrial;
141 + else
142 + {
143 + cout << "Invalid type" << endl;
144 + break;
145 + }
146 + list[n]->getData();
147 + n++;
148 + cout << "Consumer added" << endl;
149 + break;
150 +
151 + case 2:
152 + if (n == 0)
153 + cout << "No records" << endl;
154 + for (int i = 0; i < n; i++)
155 + list[i]->showBill();
156 + break;
157 +
158 + case 3:
159 + {
160 + cout << "Enter consumer id: ";
161 + cin >> id;
162 + bool found = false;
163 + for (int i = 0; i < n; i++)
164 + {
165 + if (list[i]->getId() == id)
166 + {
167 + list[i]->showBill();
168 + found = true;
169 + }
170 + }
171 + if (!found)
172 + cout << "Consumer not found" << endl;
173 + break;
174 + }
175 +
176 + case 4:
177 + cout << "Exiting..." << endl;
178 + break;
179 +
180 + default:
181 + cout << "Invalid choice" << endl;
182 + }
183 + } while (ch != 4);
184 +
185 + for (int i = 0; i < n; i++)
186 + delete list[i];
187 +
188 + return 0;
189 + }
exp2.cpp (file created)
@@ -0,0 +1,55 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Rectangle
5 + {
6 + int length, breadth;
7 +
8 + public:
9 + void setData(int l, int b)
10 + {
11 + length = l;
12 + breadth = b;
13 + }
14 +
15 + int area()
16 + {
17 + return length * breadth;
18 + }
19 +
20 + int perimeter();
21 + void display();
22 + };
23 +
24 + int Rectangle::perimeter()
25 + {
26 + return 2 * (length + breadth);
27 + }
28 +
29 + void Rectangle::display()
30 + {
31 + cout << "Length = " << length << ", Breadth = " << breadth << endl;
32 + cout << "Area = " << area() << endl;
33 + cout << "Perimeter = " << perimeter() << endl;
34 + }
35 +
36 + int main()
37 + {
38 + Rectangle r1, r2;
39 + int l, b;
40 +
41 + cout << "Enter length and breadth of rectangle 1: ";
42 + cin >> l >> b;
43 + r1.setData(l, b);
44 +
45 + cout << "Enter length and breadth of rectangle 2: ";
46 + cin >> l >> b;
47 + r2.setData(l, b);
48 +
49 + cout << "\nRectangle 1" << endl;
50 + r1.display();
51 + cout << "\nRectangle 2" << endl;
52 + r2.display();
53 +
54 + return 0;
55 + }
exp3.cpp (file created)
@@ -0,0 +1,53 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Box
5 + {
6 + int l, b, h;
7 +
8 + public:
9 + Box()
10 + {
11 + l = b = h = 1;
12 + cout << "Default constructor called" << endl;
13 + }
14 +
15 + Box(int x, int y, int z)
16 + {
17 + l = x;
18 + b = y;
19 + h = z;
20 + cout << "Parameterized constructor called" << endl;
21 + }
22 +
23 + Box(Box &obj)
24 + {
25 + l = obj.l;
26 + b = obj.b;
27 + h = obj.h;
28 + cout << "Copy constructor called" << endl;
29 + }
30 +
31 + int volume()
32 + {
33 + return l * b * h;
34 + }
35 +
36 + ~Box()
37 + {
38 + cout << "Destructor called for box with volume " << volume() << endl;
39 + }
40 + };
41 +
42 + int main()
43 + {
44 + Box b1;
45 + Box b2(2, 3, 4);
46 + Box b3(b2);
47 +
48 + cout << "\nVolume of b1 = " << b1.volume() << endl;
49 + cout << "Volume of b2 = " << b2.volume() << endl;
50 + cout << "Volume of b3 = " << b3.volume() << endl << endl;
51 +
52 + return 0;
53 + }
exp4.cpp (file created)
@@ -0,0 +1,42 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + int total = 0;
5 +
6 + class Test
7 + {
8 + int id;
9 +
10 + public:
11 + Test()
12 + {
13 + total++;
14 + id = total;
15 + cout << "Object " << id << " created" << endl;
16 + }
17 +
18 + ~Test()
19 + {
20 + cout << "Object " << id << " destroyed" << endl;
21 + total--;
22 + }
23 + };
24 +
25 + int main()
26 + {
27 + cout << "Inside main" << endl;
28 + Test t1, t2;
29 +
30 + {
31 + cout << "\nEntering block" << endl;
32 + Test t3, t4;
33 + cout << "Leaving block" << endl;
34 + }
35 +
36 + cout << "\nBack in main" << endl;
37 + Test *p = new Test;
38 + delete p;
39 +
40 + cout << "\nEnd of main" << endl;
41 + return 0;
42 + }
exp5.cpp (file created)
@@ -0,0 +1,61 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Area
5 + {
6 + public:
7 + float area(float r)
8 + {
9 + return 3.14 * r * r;
10 + }
11 +
12 + int area(int l, int b)
13 + {
14 + return l * b;
15 + }
16 +
17 + float area(float b, float h, int t)
18 + {
19 + return 0.5 * b * h;
20 + }
21 + };
22 +
23 + int add(int a, int b)
24 + {
25 + return a + b;
26 + }
27 +
28 + int add(int a, int b, int c)
29 + {
30 + return a + b + c;
31 + }
32 +
33 + double add(double a, double b)
34 + {
35 + return a + b;
36 + }
37 +
38 + int main()
39 + {
40 + Area a;
41 + float r, base, height;
42 + int l, b;
43 +
44 + cout << "Enter radius of circle: ";
45 + cin >> r;
46 + cout << "Area of circle = " << a.area(r) << endl;
47 +
48 + cout << "Enter length and breadth of rectangle: ";
49 + cin >> l >> b;
50 + cout << "Area of rectangle = " << a.area(l, b) << endl;
51 +
52 + cout << "Enter base and height of triangle: ";
53 + cin >> base >> height;
54 + cout << "Area of triangle = " << a.area(base, height, 1) << endl;
55 +
56 + cout << "\nadd(2, 3) = " << add(2, 3) << endl;
57 + cout << "add(2, 3, 4) = " << add(2, 3, 4) << endl;
58 + cout << "add(2.5, 3.7) = " << add(2.5, 3.7) << endl;
59 +
60 + return 0;
61 + }
exp6.cpp (file created)
@@ -0,0 +1,90 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Complex
5 + {
6 + float real, imag;
7 +
8 + public:
9 + Complex()
10 + {
11 + real = 0;
12 + imag = 0;
13 + }
14 +
15 + Complex(float r, float i)
16 + {
17 + real = r;
18 + imag = i;
19 + }
20 +
21 + Complex operator+(Complex c)
22 + {
23 + Complex temp;
24 + temp.real = real + c.real;
25 + temp.imag = imag + c.imag;
26 + return temp;
27 + }
28 +
29 + Complex operator-(Complex c)
30 + {
31 + Complex temp;
32 + temp.real = real - c.real;
33 + temp.imag = imag - c.imag;
34 + return temp;
35 + }
36 +
37 + void operator-()
38 + {
39 + real = -real;
40 + imag = -imag;
41 + }
42 +
43 + bool operator==(Complex c)
44 + {
45 + return (real == c.real && imag == c.imag);
46 + }
47 +
48 + void display()
49 + {
50 + if (imag >= 0)
51 + cout << real << " + " << imag << "i" << endl;
52 + else
53 + cout << real << " - " << -imag << "i" << endl;
54 + }
55 + };
56 +
57 + int main()
58 + {
59 + float r, i;
60 + cout << "Enter real and imaginary part of first number: ";
61 + cin >> r >> i;
62 + Complex c1(r, i);
63 +
64 + cout << "Enter real and imaginary part of second number: ";
65 + cin >> r >> i;
66 + Complex c2(r, i);
67 +
68 + Complex c3 = c1 + c2;
69 + Complex c4 = c1 - c2;
70 +
71 + cout << "\nc1 = ";
72 + c1.display();
73 + cout << "c2 = ";
74 + c2.display();
75 + cout << "c1 + c2 = ";
76 + c3.display();
77 + cout << "c1 - c2 = ";
78 + c4.display();
79 +
80 + if (c1 == c2)
81 + cout << "c1 and c2 are equal" << endl;
82 + else
83 + cout << "c1 and c2 are not equal" << endl;
84 +
85 + -c1;
86 + cout << "After unary minus, c1 = ";
87 + c1.display();
88 +
89 + return 0;
90 + }
exp7.cpp (file created)
@@ -0,0 +1,76 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class B;
5 +
6 + class A
7 + {
8 + int x;
9 +
10 + public:
11 + void setData(int n)
12 + {
13 + x = n;
14 + }
15 + friend void findMax(A, B);
16 + friend void swapValues(A &, B &);
17 + friend void show(A, B);
18 + };
19 +
20 + class B
21 + {
22 + int y;
23 +
24 + public:
25 + void setData(int n)
26 + {
27 + y = n;
28 + }
29 + friend void findMax(A, B);
30 + friend void swapValues(A &, B &);
31 + friend void show(A, B);
32 + };
33 +
34 + void findMax(A a, B b)
35 + {
36 + if (a.x > b.y)
37 + cout << "Maximum is " << a.x << endl;
38 + else
39 + cout << "Maximum is " << b.y << endl;
40 + }
41 +
42 + void swapValues(A &a, B &b)
43 + {
44 + int temp = a.x;
45 + a.x = b.y;
46 + b.y = temp;
47 + }
48 +
49 + void show(A a, B b)
50 + {
51 + cout << "A = " << a.x << ", B = " << b.y << endl;
52 + }
53 +
54 + int main()
55 + {
56 + A a;
57 + B b;
58 + int n1, n2;
59 +
60 + cout << "Enter value for class A: ";
61 + cin >> n1;
62 + cout << "Enter value for class B: ";
63 + cin >> n2;
64 +
65 + a.setData(n1);
66 + b.setData(n2);
67 +
68 + show(a, b);
69 + findMax(a, b);
70 +
71 + swapValues(a, b);
72 + cout << "After swapping: ";
73 + show(a, b);
74 +
75 + return 0;
76 + }
exp8.cpp (file created)
@@ -0,0 +1,123 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Person
5 + {
6 + protected:
7 + string name;
8 + int age;
9 +
10 + public:
11 + void getPerson()
12 + {
13 + cout << "Enter name: ";
14 + cin >> name;
15 + cout << "Enter age: ";
16 + cin >> age;
17 + }
18 + void showPerson()
19 + {
20 + cout << "Name: " << name << endl;
21 + cout << "Age: " << age << endl;
22 + }
23 + };
24 +
25 + class Student : public Person
26 + {
27 + protected:
28 + int roll;
29 +
30 + public:
31 + void getStudent()
32 + {
33 + getPerson();
34 + cout << "Enter roll no: ";
35 + cin >> roll;
36 + }
37 + void showStudent()
38 + {
39 + showPerson();
40 + cout << "Roll No: " << roll << endl;
41 + }
42 + };
43 +
44 + class Exam : public Student
45 + {
46 + protected:
47 + float m1, m2;
48 +
49 + public:
50 + void getMarks()
51 + {
52 + cout << "Enter marks of 2 subjects: ";
53 + cin >> m1 >> m2;
54 + }
55 + void showMarks()
56 + {
57 + cout << "Marks: " << m1 << ", " << m2 << endl;
58 + }
59 + };
60 +
61 + class Sports
62 + {
63 + protected:
64 + float score;
65 +
66 + public:
67 + void getScore()
68 + {
69 + cout << "Enter sports score: ";
70 + cin >> score;
71 + }
72 + };
73 +
74 + class Result : public Exam, public Sports
75 + {
76 + public:
77 + void display()
78 + {
79 + showStudent();
80 + showMarks();
81 + cout << "Sports score: " << score << endl;
82 + cout << "Total: " << m1 + m2 + score << endl;
83 + }
84 + };
85 +
86 + class Teacher : public Person
87 + {
88 + string subject;
89 +
90 + public:
91 + void getTeacher()
92 + {
93 + getPerson();
94 + cout << "Enter subject: ";
95 + cin >> subject;
96 + }
97 + void showTeacher()
98 + {
99 + showPerson();
100 + cout << "Subject: " << subject << endl;
101 + }
102 + };
103 +
104 + int main()
105 + {
106 + Result r;
107 + cout << "--- Student Details ---" << endl;
108 + r.getStudent();
109 + r.getMarks();
110 + r.getScore();
111 +
112 + Teacher t;
113 + cout << "\n--- Teacher Details ---" << endl;
114 + t.getTeacher();
115 +
116 + cout << "\n--- Student Result ---" << endl;
117 + r.display();
118 +
119 + cout << "\n--- Teacher Info ---" << endl;
120 + t.showTeacher();
121 +
122 + return 0;
123 + }
exp9.cpp (file created)
@@ -0,0 +1,97 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Shape
5 + {
6 + public:
7 + virtual void area() = 0;
8 + virtual void name()
9 + {
10 + cout << "This is a shape" << endl;
11 + }
12 + virtual ~Shape() {}
13 + };
14 +
15 + class Circle : public Shape
16 + {
17 + float r;
18 +
19 + public:
20 + Circle(float x)
21 + {
22 + r = x;
23 + }
24 + void name()
25 + {
26 + cout << "This is a circle" << endl;
27 + }
28 + void area()
29 + {
30 + cout << "Area = " << 3.14 * r * r << endl;
31 + }
32 + };
33 +
34 + class Rectangle : public Shape
35 + {
36 + float l, b;
37 +
38 + public:
39 + Rectangle(float x, float y)
40 + {
41 + l = x;
42 + b = y;
43 + }
44 + void name()
45 + {
46 + cout << "This is a rectangle" << endl;
47 + }
48 + void area()
49 + {
50 + cout << "Area = " << l * b << endl;
51 + }
52 + };
53 +
54 + class Triangle : public Shape
55 + {
56 + float b, h;
57 +
58 + public:
59 + Triangle(float x, float y)
60 + {
61 + b = x;
62 + h = y;
63 + }
64 + void area()
65 + {
66 + cout << "Area = " << 0.5 * b * h << endl;
67 + }
68 + };
69 +
70 + int main()
71 + {
72 + float r, l, b, tb, h;
73 + cout << "Enter radius of circle: ";
74 + cin >> r;
75 + cout << "Enter length and breadth of rectangle: ";
76 + cin >> l >> b;
77 + cout << "Enter base and height of triangle: ";
78 + cin >> tb >> h;
79 +
80 + Shape *s[3];
81 + s[0] = new Circle(r);
82 + s[1] = new Rectangle(l, b);
83 + s[2] = new Triangle(tb, h);
84 +
85 + cout << endl;
86 + for (int i = 0; i < 3; i++)
87 + {
88 + s[i]->name();
89 + s[i]->area();
90 + cout << endl;
91 + }
92 +
93 + for (int i = 0; i < 3; i++)
94 + delete s[i];
95 +
96 + return 0;
97 + }