k

k / B65 OOP

Last active 2 weeks ago

Like 0

k revised this gist 2 weeks ago · bdd5d18

No changes

k revised this gist 2 weeks ago · fad3fa7

9 files changed, 254 insertions

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