-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp7.cpp
More file actions
173 lines (134 loc) · 3.33 KB
/
Exp7.cpp
File metadata and controls
173 lines (134 loc) · 3.33 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
//inheritance
#include <iostream>
using namespace std;
class BaseException : public exception {
public:
BaseException(const string& message) : message(message) {}
virtual const string& what() const override {
return message;
}
private:
string message;
};
class DerivedException : public BaseException {
public:
DerivedException(const string& message) : BaseException(message) {}
};
class Person {
public:
void validateAge(int age) {
if (age < 0 || age > 120) {
throw DerivedException("Invalid age: " + to_string(age));
}
}
};
class Employee : public Person {
public:
void validateEmployee(int age, const string& title) {
Person::validateAge(age);
if (title.empty()) {
throw BaseException("Employee must have a title");
}
}
};
int main() {
try {
Employee employee;
employee.validateEmployee(-1, "");
} catch (const BaseException& e) {
cout << "Base exception: " << e.what() << endl;
} catch (const DerivedException& e) {
cout << "Derived exception: " << e.what() << endl;
} catch (const exception& e) {
cout << "Unexpected exception: " << e.what() << endl;
}
return 0;
}
//Exception handling
#include <iostream>
using namespace std;
int divide(int x, int y) {
if (y == 0) {
throw exception(); // Throw an exception if y is 0
}
return x / y;
}
int main() {
try {
int result = divide(10, 0); // Will throw an exception
cout << "Result: " << result << endl;
} catch (exception& e) {
cout << "Error: Division by zero" << endl;
}
return 0;
}
//friend function
#include <iostream>
using namespace std;
class Data {
private:
int value;
public:
Data(int value) : value(value) {}
friend void validateData(Data& data) {
if (data.value < 0) {
throw invalid_argument("Data value cannot be negative");
}
}
void print() const {
cout << "Value: " << value << endl;
}
};
int main() {
try {
Data data(-10); // Data value is negative
validateData(data);
} catch (const invalid_argument& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
//Virtual function
C++
#include <iostream>
using namespace std;
class Shape {
public:
virtual double getArea() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double radius) : radius(radius) {}
double getArea() const override {
if (radius <= 0) {
throw invalid_argument("Radius must be positive");
}
return radius * radius * 3.14159;
}
};
class Rectangle : public Shape {
private:
double length, width;
public:
Rectangle(double length, double width) : length(length), width(width) {}
double getArea() const override {
if (length <= 0 || width <= 0) {
throw invalid_argument("Length and width must be positive");
}
return length * width;
}
};
int main() {
try {
Shape* shapes[] = {new Circle(-1), new Rectangle(-2, 3)};
for (Shape* shape : shapes) {
cout << "Area: " << shape->getArea() << endl;
delete shape;
}
} catch (const invalid_argument& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}