Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class Shape {
  9. protected:
  10. int width, height;
  11. public:
  12. Shape(int a = 0, int b = 0) {
  13. width = a;
  14. height = b;
  15. }
  16.  
  17. virtual void area() {
  18. cout << "Parent class area" << endl;
  19. }
  20. };
  21.  
  22. class Rectangle : public Shape {
  23. public:
  24. Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
  25. /*Rectangle inicijalizira a i b u argumentnoj linijite poziva roditrljski
  26. shape kojima daje iste argumente
  27. ,a Shape ih po definiciji stavljau visinu i sirinu,triba imati na umu da poziva
  28. shape sa istim brojem argumenata*/
  29.  
  30. void area() {
  31. cout << "Rectangle class area : " << (width * height) << endl;
  32. }
  33. };
  34.  
  35. class Triangle : public Shape {
  36. public:
  37. Triangle(int a = 0, int b = 0) : Shape(a, b) {}
  38.  
  39. void area() {
  40. cout << "Triangle class area : " << (width * height / 2) << endl;
  41. }
  42. };
  43.  
  44. int main()
  45. {
  46. Shape *shape[4];
  47. Rectangle r1(5, 3);
  48. Rectangle r2(25, 7);
  49. Triangle t1(2, 6);
  50. Triangle t2(7, 9);
  51.  
  52. shape[0] = &r1;//znaci shape od 0 je pokazivac na r1 ,kao da pise *shape1=&r1
  53. shape[1] = &r2;
  54. shape[2] = &t1;
  55. shape[3] = &t2;
  56.  
  57. for (int i = 0; i < 4; i++)
  58. {
  59. (*shape[i]).area();
  60. }
  61.  
  62. system("PAUSE");
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement