k

k / B71 OOP experiments

Last active 2 weeks ago

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