k

k / B71 OOP experiments

Last active 2 weeks ago

Like 0

k revised this gist 2 weeks ago · 1d358c4

No changes

k revised this gist 2 weeks ago · 2a19bc5

9 files changed, 250 insertions

exp1.cpp (file created)
@@ -0,0 +1,20 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Student
5 + {
6 + public:
7 + int roll;
8 + void show()
9 + {
10 + cout << "Roll no: " << roll << endl;
11 + }
12 + };
13 +
14 + int main()
15 + {
16 + Student s;
17 + s.roll = 10;
18 + s.show();
19 + return 0;
20 + }
exp2.cpp (file created)
@@ -0,0 +1,27 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Box
5 + {
6 + public:
7 + int l, b;
8 + void get()
9 + {
10 + l = 5;
11 + b = 4;
12 + }
13 + void area();
14 + };
15 +
16 + void Box::area()
17 + {
18 + cout << "Area: " << l * b << endl;
19 + }
20 +
21 + int main()
22 + {
23 + Box x;
24 + x.get();
25 + x.area();
26 + return 0;
27 + }
exp3.cpp (file created)
@@ -0,0 +1,35 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class A
5 + {
6 + public:
7 + int x;
8 + A()
9 + {
10 + x = 0;
11 + cout << "Default constructor x = " << x << endl;
12 + }
13 + A(int a)
14 + {
15 + x = a;
16 + cout << "Parameterized constructor x = " << x << endl;
17 + }
18 + A(A &o)
19 + {
20 + x = o.x;
21 + cout << "Copy constructor x = " << x << endl;
22 + }
23 + ~A()
24 + {
25 + cout << "Destructor called" << endl;
26 + }
27 + };
28 +
29 + int main()
30 + {
31 + A a1;
32 + A a2(5);
33 + A a3(a2);
34 + return 0;
35 + }
exp4.cpp (file created)
@@ -0,0 +1,24 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class A
5 + {
6 + public:
7 + A()
8 + {
9 + cout << "Object created" << endl;
10 + }
11 + ~A()
12 + {
13 + cout << "Object destroyed" << endl;
14 + }
15 + };
16 +
17 + int main()
18 + {
19 + A a;
20 + {
21 + A b;
22 + }
23 + return 0;
24 + }
exp5.cpp (file created)
@@ -0,0 +1,25 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + int add(int a, int b)
5 + {
6 + return a + b;
7 + }
8 +
9 + int add(int a, int b, int c)
10 + {
11 + return a + b + c;
12 + }
13 +
14 + float add(float a, float b)
15 + {
16 + return a + b;
17 + }
18 +
19 + int main()
20 + {
21 + cout << add(2, 3) << endl;
22 + cout << add(1, 2, 3) << endl;
23 + cout << add(1.5f, 2.5f) << endl;
24 + return 0;
25 + }
exp6.cpp (file created)
@@ -0,0 +1,24 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class Num
5 + {
6 + public:
7 + int x;
8 + Num operator+(Num n)
9 + {
10 + Num t;
11 + t.x = x + n.x;
12 + return t;
13 + }
14 + };
15 +
16 + int main()
17 + {
18 + Num a, b, c;
19 + a.x = 10;
20 + b.x = 20;
21 + c = a + b;
22 + cout << "Sum: " << c.x << endl;
23 + return 0;
24 + }
exp7.cpp (file created)
@@ -0,0 +1,25 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class A
5 + {
6 + int x;
7 + public:
8 + A()
9 + {
10 + x = 50;
11 + }
12 + friend void show(A a);
13 + };
14 +
15 + void show(A a)
16 + {
17 + cout << "x = " << a.x << endl;
18 + }
19 +
20 + int main()
21 + {
22 + A a;
23 + show(a);
24 + return 0;
25 + }
exp8.cpp (file created)
@@ -0,0 +1,38 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class A
5 + {
6 + public:
7 + void showA()
8 + {
9 + cout << "Class A" << endl;
10 + }
11 + };
12 +
13 + class B : public A
14 + {
15 + public:
16 + void showB()
17 + {
18 + cout << "Class B" << endl;
19 + }
20 + };
21 +
22 + class C : public B
23 + {
24 + public:
25 + void showC()
26 + {
27 + cout << "Class C" << endl;
28 + }
29 + };
30 +
31 + int main()
32 + {
33 + C c;
34 + c.showA();
35 + c.showB();
36 + c.showC();
37 + return 0;
38 + }
exp9.cpp (file created)
@@ -0,0 +1,32 @@
1 + #include <iostream>
2 + using namespace std;
3 +
4 + class A
5 + {
6 + public:
7 + virtual void show()
8 + {
9 + cout << "Class A" << endl;
10 + }
11 + };
12 +
13 + class B : public A
14 + {
15 + public:
16 + void show()
17 + {
18 + cout << "Class B" << endl;
19 + }
20 + };
21 +
22 + int main()
23 + {
24 + A *p;
25 + A a;
26 + B b;
27 + p = &a;
28 + p->show();
29 + p = &b;
30 + p->show();
31 + return 0;
32 + }