k

k / B65 OOP

Last active 2 weeks ago

Like 0

Revision fad3fa7ee118bb31623378cb0d034b832818d76b

exp1.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Book
5{
6public:
7 string title;
8 int price;
9 void show()
10 {
11 cout << "Title: " << title << endl;
12 cout << "Price: " << price << endl;
13 }
14};
15
16int main()
17{
18 Book b;
19 b.title = "C++";
20 b.price = 300;
21 b.show();
22 return 0;
23}
24
exp2.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Bank
5{
6 int bal;
7public:
8 void set()
9 {
10 bal = 1000;
11 }
12 void deposit(int a)
13 {
14 bal = bal + a;
15 }
16 void show();
17};
18
19void Bank::show()
20{
21 cout << "Balance: " << bal << endl;
22}
23
24int main()
25{
26 Bank b;
27 b.set();
28 b.deposit(500);
29 b.show();
30 return 0;
31}
32
exp3.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Circle
5{
6 float r;
7public:
8 Circle()
9 {
10 r = 1;
11 }
12 Circle(float x)
13 {
14 r = x;
15 }
16 void area()
17 {
18 cout << "Area: " << 3.14 * r * r << endl;
19 }
20 ~Circle()
21 {
22 cout << "Circle destroyed" << endl;
23 }
24};
25
26int main()
27{
28 Circle c1;
29 Circle c2(3);
30 c1.area();
31 c2.area();
32 return 0;
33}
34
exp4.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Demo
5{
6public:
7 Demo()
8 {
9 cout << "Constructor" << endl;
10 }
11 ~Demo()
12 {
13 cout << "Destructor" << endl;
14 }
15};
16
17int main()
18{
19 Demo *p = new Demo;
20 delete p;
21 return 0;
22}
23
exp5.cpp Raw
1#include <iostream>
2using namespace std;
3
4void print(int a)
5{
6 cout << "Int: " << a << endl;
7}
8
9void print(double a)
10{
11 cout << "Double: " << a << endl;
12}
13
14void print(char a)
15{
16 cout << "Char: " << a << endl;
17}
18
19int main()
20{
21 print(10);
22 print(5.5);
23 print('A');
24 return 0;
25}
26
exp6.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Complex
5{
6public:
7 int r, i;
8 Complex operator+(Complex c)
9 {
10 Complex t;
11 t.r = r + c.r;
12 t.i = i + c.i;
13 return t;
14 }
15};
16
17int main()
18{
19 Complex a, b, c;
20 a.r = 2;
21 a.i = 3;
22 b.r = 4;
23 b.i = 5;
24 c = a + b;
25 cout << c.r << " + " << c.i << "i" << endl;
26 return 0;
27}
28
exp7.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Stud
5{
6 int m1, m2, m3;
7public:
8 Stud()
9 {
10 m1 = 70;
11 m2 = 80;
12 m3 = 90;
13 }
14 friend void avg(Stud s);
15};
16
17void avg(Stud s)
18{
19 cout << "Average: " << (s.m1 + s.m2 + s.m3) / 3 << endl;
20}
21
22int main()
23{
24 Stud s;
25 avg(s);
26 return 0;
27}
28
exp8.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Animal
5{
6public:
7 void eat()
8 {
9 cout << "Animal eats" << endl;
10 }
11};
12
13class Dog : public Animal
14{
15public:
16 void bark()
17 {
18 cout << "Dog barks" << endl;
19 }
20};
21
22int main()
23{
24 Dog d;
25 d.eat();
26 d.bark();
27 return 0;
28}
29
exp9.cpp Raw
1#include <iostream>
2using namespace std;
3
4class Animal
5{
6public:
7 virtual void sound() = 0;
8};
9
10class Dog : public Animal
11{
12public:
13 void sound()
14 {
15 cout << "Bark" << endl;
16 }
17};
18
19class Cat : public Animal
20{
21public:
22 void sound()
23 {
24 cout << "Meow" << endl;
25 }
26};
27
28int main()
29{
30 Animal *a;
31 Dog d;
32 Cat c;
33 a = &d;
34 a->sound();
35 a = &c;
36 a->sound();
37 return 0;
38}
39