k

k / B62 OOP

Last active 2 weeks ago

Like 0
exp1.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Car
5{
6public:
7 string name;
8 int speed;
9 void show()
10 {
11 cout << name << " " << speed << endl;
12 }
13};
14
15int main()
16{
17 Car c;
18 c.name = "BMW";
19 c.speed = 120;
20 c.show();
21 return 0;
22}
23
exp2.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Emp
5{
6 int id;
7 float sal;
8public:
9 void get(int a, float b)
10 {
11 id = a;
12 sal = b;
13 }
14 void show();
15};
16
17void Emp::show()
18{
19 cout << "ID: " << id << endl;
20 cout << "Salary: " << sal << endl;
21}
22
23int main()
24{
25 Emp e1, e2;
26 e1.get(1, 5000);
27 e2.get(2, 7000);
28 e1.show();
29 e2.show();
30 return 0;
31}
32
exp3.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Rect
5{
6 int l, b;
7public:
8 Rect()
9 {
10 l = 1;
11 b = 1;
12 }
13 Rect(int x, int y)
14 {
15 l = x;
16 b = y;
17 }
18 void area()
19 {
20 cout << "Area: " << l * b << endl;
21 }
22 ~Rect()
23 {
24 cout << "Destructor" << endl;
25 }
26};
27
28int main()
29{
30 Rect r1;
31 Rect r2(4, 5);
32 r1.area();
33 r2.area();
34 return 0;
35}
36
exp4.cpp Raw
1#include <iostream>
2using namespace std;
3
4int c = 0;
5
6class Test
7{
8public:
9 Test()
10 {
11 c++;
12 cout << "Created object " << c << endl;
13 }
14 ~Test()
15 {
16 cout << "Destroyed object " << c << endl;
17 c--;
18 }
19};
20
21int main()
22{
23 Test t1, t2, t3;
24 return 0;
25}
26
exp5.cpp Raw
1#include <iostream>
2using namespace std;
3
4void area(int a)
5{
6 cout << "Square: " << a * a << endl;
7}
8
9void area(int a, int b)
10{
11 cout << "Rectangle: " << a * b << endl;
12}
13
14void area(float r)
15{
16 cout << "Circle: " << 3.14 * r * r << endl;
17}
18
19int main()
20{
21 area(4);
22 area(3, 5);
23 area(2.0f);
24 return 0;
25}
26
exp7.cpp Raw
1#include <iostream>
2using namespace std;
3
4class B;
5
6class A
7{
8 int x;
9public:
10 A()
11 {
12 x = 10;
13 }
14 friend void add(A a, B b);
15};
16
17class B
18{
19 int y;
20public:
21 B()
22 {
23 y = 20;
24 }
25 friend void add(A a, B b);
26};
27
28void add(A a, B b)
29{
30 cout << "Sum: " << a.x + b.y << endl;
31}
32
33int main()
34{
35 A a;
36 B b;
37 add(a, b);
38 return 0;
39}
40
exp8.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Person
5{
6public:
7 string name;
8};
9
10class Marks
11{
12public:
13 int m;
14};
15
16class Student : public Person, public Marks
17{
18public:
19 void show()
20 {
21 cout << "Name: " << name << endl;
22 cout << "Marks: " << m << endl;
23 }
24};
25
26int main()
27{
28 Student s;
29 s.name = "Ram";
30 s.m = 85;
31 s.show();
32 return 0;
33}
34
exp9.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Shape
5{
6public:
7 virtual void draw()
8 {
9 cout << "Shape" << endl;
10 }
11};
12
13class Circle : public Shape
14{
15public:
16 void draw()
17 {
18 cout << "Circle" << endl;
19 }
20};
21
22class Square : public Shape
23{
24public:
25 void draw()
26 {
27 cout << "Square" << endl;
28 }
29};
30
31int main()
32{
33 Shape *s;
34 Circle c;
35 Square q;
36 s = &c;
37 s->draw();
38 s = &q;
39 s->draw();
40 return 0;
41}
42