-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleInheritance.cpp
More file actions
255 lines (212 loc) · 5.78 KB
/
Copy pathMultipleInheritance.cpp
File metadata and controls
255 lines (212 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
#include<iostream>
using namespace std;
-----> Syntax for inheriting in multiple inheritance
class DerivedC : visibility-mode base1, visibility-mode base2{
class body of class DerivedC;
}
class Base1{
protected:
int base1int;
public:
void set_base1int(int a){
base1int = a;
}
};
class Base2{
protected:
int base2int;
public:
void set_base2int(int b){
base2int = b;
}
};
class Derived : public Base1, public Base2{
public:
void show(){
cout << "The value of Base1 is " << base1int << endl;
cout << "The value of Base2 is " << base2int << endl;
cout << "The sum of these values is " << base1int + base2int << endl;
}
};
int main(){
Derived sumit, kasu;
sumit.set_base1int(57);
sumit.set_base2int(57);
sumit.show();
kasu.set_base1int(75);
kasu.set_base2int(75);
kasu.show();
return 0;
}
// Exercise on C++ inheritance
create 2 classes : 1. SimpleCalculator - Take input of 2 numbers using a utility function and perform +, -, *, / and display the result using another function.2. ScientificCalculator - Take input of 2 numbers using a utility function and perform any four scientific operations of your choice and display the result using another function.create another class HybridCalculator and inherit it using these 2 classes : question1.what type of inheritance are you using ? question2.which mood inheritance are you using ? question3.create an object of HybridCalculator and display result of simple and scientific calculator.
✅ Answers to Questions:
Q1: What type of inheritance are you using?
Multiple Inheritance — HybridCalculator inherits from both SimpleCalculator and ScientificCalculator.
Q2: Which mode of inheritance are you using?
Public Mode Inheritance — Both parent classes are inherited using the public keyword.
Q3: Create an object of HybridCalculator and display result of simple and scientific calculator.
#include <iostream>
#include <cmath>
using namespace std;
// SimpleCalculator Class
class SimpleCalculator
{
protected:
double a, b;
public:
void getSimpleInput()
{
cout << "Enter two numbers for Simple Calculator: ";
cin >> a >> b;
}
void displaySimpleOperations()
{
cout << "\n--- Simple Calculator Operations ---\n";
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
if (b != 0)
cout << "Division: " << a / b << endl;
else
cout << "Division: Cannot divide by zero.\n";
}
};
// ScientificCalculator Class
class ScientificCalculator
{
protected:
double x, y;
public:
void getScientificInput()
{
cout << "\nEnter two numbers for Scientific Calculator: ";
cin >> x >> y;
}
void displayScientificOperations()
{
cout << "\n--- Scientific Calculator Operations ---\n";
cout << "Power (x^y): " << pow(x, y) << endl;
if (x >= 0)
cout << "Square root of x: " << sqrt(x) << endl;
else
cout << "Square root: Invalid input (negative number)\n";
if (x > 0)
cout << "Log10 of x: " << log10(x) << endl;
else
cout << "Log10: Invalid input (non-positive number)\n";
cout << "Sine of x degrees: " << sin(x * M_PI / 180) << endl;
}
};
// HybridCalculator Class using Multiple Inheritance
class HybridCalculator : public SimpleCalculator, public ScientificCalculator
{
// No additional members needed
};
int main()
{
HybridCalculator calc;
// Simple Calculator Operations
calc.getSimpleInput();
calc.displaySimpleOperations();
// Scientific Calculator Operations
calc.getScientificInput();
calc.displayScientificOperations();
return 0;
}
// Ambiguity in Inheritance
#include<iostream>
using namespace std;
class Base1{
public:
void greet(){
cout << "Hi, How are you?" << endl;
}
};
class Base2{
public:
void greet()
{
cout << "Hn, Kaise ho?" << endl;
}
};
class Derived : public Base1, public Base2{
int a;
// Ambiguity Resolution
public:
void greet(){
Base1::greet();
}
};
int main(){
Base1 objBase1;
Base2 objBase2;
objBase1.greet();
objBase2.greet();
Derived objDerived;
objDerived.greet();
return 0;
}
*/
// Virtual Base Class in C++
#include<iostream>
using namespace std;
class Student{
protected:
int roll_no;
public:
void setNum(int a){
roll_no = a;
}
void printNum(){
cout << "Your roll number is: " << roll_no << endl;
}
};
class Test : virtual public Student{
protected:
float maths, physics;
public:
void setMarks(float m, float p){
maths = m;
physics = p;
}
void printMarks(){
cout << "Your result is here: " << endl
<< "Maths: " << maths << endl
<< "Physics: " << physics << endl;
}
};
class Sports : public virtual Student{
protected:
float score;
public:
void setScore(float s)
{
score = s;
}
void printScore()
{
cout << "Your Yoga score is: " << score << endl;
}
};
class Result : public Test, public Sports{
private:
float total;
public:
void display(){
total = maths + physics + score;
printNum();
printMarks();
printScore();
cout << "Your total score is: " << total << endl;
}
};
int main(){
Result Sumit;
Sumit.setNum(4201);
Sumit.setMarks(90.0, 95.0);
Sumit.setScore(85.0);
Sumit.display();
return 0;
}