| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Mobile |
| 5 | { |
| 6 | public: |
| 7 | string brand; |
| 8 | int price; |
| 9 | void show() |
| 10 | { |
| 11 | cout << brand << " costs " << price << endl; |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | int main() |
| 16 | { |
| 17 | Mobile m; |
| 18 | m.brand = "Samsung"; |
| 19 | m.price = 15000; |
| 20 | m.show(); |
| 21 | return 0; |
| 22 | } |
| 23 |
Revision 8c428e00c8d490b5fa04a7dc1588a30e1456420c
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Stud |
| 5 | { |
| 6 | public: |
| 7 | int roll; |
| 8 | void show() |
| 9 | { |
| 10 | cout << "Roll: " << roll << endl; |
| 11 | } |
| 12 | }; |
| 13 | |
| 14 | int main() |
| 15 | { |
| 16 | Stud s[3]; |
| 17 | for (int i = 0; i < 3; i++) |
| 18 | { |
| 19 | s[i].roll = i + 1; |
| 20 | s[i].show(); |
| 21 | } |
| 22 | return 0; |
| 23 | } |
| 24 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Point |
| 5 | { |
| 6 | int x, y; |
| 7 | public: |
| 8 | Point(int a, int b) |
| 9 | { |
| 10 | x = a; |
| 11 | y = b; |
| 12 | } |
| 13 | Point(Point &p) |
| 14 | { |
| 15 | x = p.x; |
| 16 | y = p.y; |
| 17 | } |
| 18 | void show() |
| 19 | { |
| 20 | cout << "(" << x << ", " << y << ")" << endl; |
| 21 | } |
| 22 | ~Point() |
| 23 | { |
| 24 | cout << "Destructor" << endl; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | Point p1(2, 3); |
| 31 | Point p2(p1); |
| 32 | p1.show(); |
| 33 | p2.show(); |
| 34 | return 0; |
| 35 | } |
| 36 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Arr |
| 5 | { |
| 6 | int *p; |
| 7 | public: |
| 8 | Arr() |
| 9 | { |
| 10 | p = new int[5]; |
| 11 | cout << "Memory given" << endl; |
| 12 | } |
| 13 | ~Arr() |
| 14 | { |
| 15 | delete[] p; |
| 16 | cout << "Memory freed" << endl; |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | int main() |
| 21 | { |
| 22 | Arr a; |
| 23 | return 0; |
| 24 | } |
| 25 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | int vol(int a) |
| 5 | { |
| 6 | return a * a * a; |
| 7 | } |
| 8 | |
| 9 | float vol(float r, int h) |
| 10 | { |
| 11 | return 3.14 * r * r * h; |
| 12 | } |
| 13 | |
| 14 | int vol(int l, int b, int h) |
| 15 | { |
| 16 | return l * b * h; |
| 17 | } |
| 18 | |
| 19 | int main() |
| 20 | { |
| 21 | cout << "Cube: " << vol(3) << endl; |
| 22 | cout << "Cylinder: " << vol(2.0f, 5) << endl; |
| 23 | cout << "Box: " << vol(2, 3, 4) << endl; |
| 24 | return 0; |
| 25 | } |
| 26 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Num |
| 5 | { |
| 6 | public: |
| 7 | int x; |
| 8 | bool operator==(Num n) |
| 9 | { |
| 10 | return x == n.x; |
| 11 | } |
| 12 | }; |
| 13 | |
| 14 | int main() |
| 15 | { |
| 16 | Num a, b; |
| 17 | a.x = 5; |
| 18 | b.x = 5; |
| 19 | if (a == b) |
| 20 | cout << "Equal" << endl; |
| 21 | else |
| 22 | cout << "Not equal" << endl; |
| 23 | return 0; |
| 24 | } |
| 25 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class A |
| 5 | { |
| 6 | int x; |
| 7 | public: |
| 8 | A() |
| 9 | { |
| 10 | x = 100; |
| 11 | } |
| 12 | friend class B; |
| 13 | }; |
| 14 | |
| 15 | class B |
| 16 | { |
| 17 | public: |
| 18 | void show(A a) |
| 19 | { |
| 20 | cout << "x = " << a.x << endl; |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | int main() |
| 25 | { |
| 26 | A a; |
| 27 | B b; |
| 28 | b.show(a); |
| 29 | return 0; |
| 30 | } |
| 31 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Shape |
| 5 | { |
| 6 | public: |
| 7 | int a, b; |
| 8 | void set(int x, int y) |
| 9 | { |
| 10 | a = x; |
| 11 | b = y; |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | class Rect : public Shape |
| 16 | { |
| 17 | public: |
| 18 | void area() |
| 19 | { |
| 20 | cout << "Rect: " << a * b << endl; |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | class Tri : public Shape |
| 25 | { |
| 26 | public: |
| 27 | void area() |
| 28 | { |
| 29 | cout << "Triangle: " << 0.5 * a * b << endl; |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | Rect r; |
| 36 | Tri t; |
| 37 | r.set(4, 5); |
| 38 | t.set(4, 5); |
| 39 | r.area(); |
| 40 | t.area(); |
| 41 | return 0; |
| 42 | } |
| 43 |
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | class Emp |
| 5 | { |
| 6 | public: |
| 7 | virtual void pay() |
| 8 | { |
| 9 | cout << "Basic pay" << endl; |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | class Manager : public Emp |
| 14 | { |
| 15 | public: |
| 16 | void pay() |
| 17 | { |
| 18 | cout << "Manager pay 50000" << endl; |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | class Clerk : public Emp |
| 23 | { |
| 24 | public: |
| 25 | void pay() |
| 26 | { |
| 27 | cout << "Clerk pay 20000" << endl; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | int main() |
| 32 | { |
| 33 | Emp *e[2]; |
| 34 | Manager m; |
| 35 | Clerk c; |
| 36 | e[0] = &m; |
| 37 | e[1] = &c; |
| 38 | for (int i = 0; i < 2; i++) |
| 39 | e[i]->pay(); |
| 40 | return 0; |
| 41 | } |
| 42 |