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