Skip to content

Commit f2f52cc

Browse files
committed
Mod 2:2
1 parent 2e907a7 commit f2f52cc

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

pyqs/Increment.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Increment
5+
{
6+
int value;
7+
8+
public:
9+
Increment(int v = 0) : value(v) {}
10+
11+
Increment &operator++()
12+
{
13+
++value;
14+
return *this;
15+
}
16+
17+
Increment operator++(int)
18+
{
19+
Increment temp = *this;
20+
value++;
21+
return temp;
22+
}
23+
24+
void display()
25+
{
26+
cout << "value = " << value << endl;
27+
}
28+
};
29+
30+
int main()
31+
{
32+
Increment ob(5);
33+
(++ob).display();
34+
(ob++).display();
35+
ob.display();
36+
37+
return 0;
38+
}

pyqs/OutOfBound.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <exception>
4+
using namespace std;
5+
6+
class ArrayIndexOutOfBounException : public exception
7+
{
8+
int idx;
9+
char *message;
10+
11+
public:
12+
ArrayIndexOutOfBounException(int i) : idx(i)
13+
{
14+
message = new char[100];
15+
}
16+
17+
const char *what() const noexcept override
18+
{
19+
strcpy(message, "Array index out of bound");
20+
return message;
21+
}
22+
23+
~ArrayIndexOutOfBounException()
24+
{
25+
delete[] message;
26+
}
27+
};
28+
29+
int main()
30+
{
31+
int arr[] = {1, 2, 3, 4};
32+
int idx;
33+
cout << "Enter index:";
34+
cin >> idx;
35+
36+
try
37+
{
38+
if (idx < 0 || idx > 3)
39+
throw ArrayIndexOutOfBounException(idx);
40+
41+
cout << "Value = " << arr[idx] << endl;
42+
}
43+
catch (const ArrayIndexOutOfBounException& e)
44+
{
45+
cout << e.what() << endl;
46+
}
47+
return 0;
48+
}

pyqs/Template.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
template <typename T>
6+
T add(T a, T b)
7+
{
8+
return a + b;
9+
}
10+
11+
template <>
12+
string add<string>(string a, string b)
13+
{
14+
return a + b;
15+
}
16+
17+
int main()
18+
{
19+
cout << "Int Addition: " << add(1, 2) << endl;
20+
cout << "Float Addition: " << add(1.3, 5.6) << endl;
21+
cout << "String: " << add<string>("Hello ", "World") << endl;
22+
23+
return 0;
24+
}

pyqs/customExp.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <exception>
4+
using namespace std;
5+
6+
class test : public exception
7+
{
8+
char *message;
9+
10+
public:
11+
test(const char *msg)
12+
{
13+
message = new char[strlen(msg) + 1];
14+
strcpy(message, msg);
15+
}
16+
17+
// Override what() to return the message
18+
const char *what() const noexcept override
19+
{
20+
return message;
21+
}
22+
23+
~test()
24+
{
25+
delete[] message;
26+
}
27+
};
28+
29+
int main()
30+
{
31+
char input[100];
32+
cout << "Enter" << endl;
33+
cin >> input;
34+
35+
try
36+
{
37+
if (strcmp(input, "123") == 0)
38+
{
39+
throw test("Entered Number");
40+
}
41+
else
42+
{
43+
cout << "Valid i/p: " << input << endl;
44+
}
45+
}
46+
catch (test &e)
47+
{
48+
cout << e.what() << endl;
49+
}
50+
return 0;
51+
}

pyqs/operatorOverload.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class operatorOverload
5+
{
6+
int i;
7+
8+
public:
9+
friend istream &operator>>(istream &in, operatorOverload &t);
10+
friend ostream &operator<<(ostream &out, operatorOverload &t);
11+
};
12+
13+
istream &operator>>(istream &in, operatorOverload &t)
14+
{
15+
in >> t.i;
16+
return in;
17+
}
18+
19+
ostream &operator<<(ostream &out, operatorOverload &t)
20+
{
21+
out << t.i;
22+
return out;
23+
}
24+
25+
int main()
26+
{
27+
operatorOverload obj;
28+
cout << "Enter input" << endl;
29+
cin >> obj;
30+
cout << obj;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)