Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Base
  7. {
  8. private:
  9. string value1;
  10. int value2;
  11. protected:
  12. int protected1()
  13. {
  14. cout << "Protected base function" << endl;
  15. return 0;
  16. }
  17.  
  18. public:
  19. string getValue1()
  20. {
  21. return value1;
  22. }
  23.  
  24. void setValue1(string v)
  25. {
  26. value1 = v;
  27. }
  28.  
  29. int getValue2()
  30. {
  31. return value2;
  32. }
  33.  
  34. void setValue2(int v)
  35. {
  36. value2 = v;
  37. }
  38.  
  39. };
  40. class Child1 : public Base {
  41. public:
  42. void publicFunc() {
  43. protected1();
  44. }
  45. };
  46.  
  47. class Child2 : public Child1 {
  48. protected:
  49. void protected1() {
  50. cout << "Protected Child2 function" << endl;
  51. }
  52. public:
  53. void publicFunc() {
  54. protected1();
  55. }
  56. };
  57. void reverse(int * firstP, int * secondP) {
  58. while (firstP < secondP) {
  59. int temp = *secondP;
  60. *secondP = *firstP;
  61. *firstP = temp;
  62. firstP = firstP + 1;
  63. secondP = secondP - 1;
  64. }
  65. }
  66. int main() {
  67. int * ptr = (int *)malloc(10 * sizeof(int));
  68. for (int i = 0; i < 10; i++) {
  69. *(ptr + i) = i + 1;
  70. }
  71. reverse(ptr, ptr + 9);
  72. int ptr2[10];
  73. for (int i = 0; i < 10; i++) {
  74. ptr2[i] = i + 1;
  75. }
  76. reverse(&ptr2[0], &ptr2[9]);
  77. //It operates the same because arrays are just glorified pointers which dont allow you to get out of the allocated space, which since we are not getting out of the allocated space doesnt matter..
  78.  
  79. Child1 c;
  80. c.publicFunc();
  81. Child2 c2;
  82. c2.publicFunc();
  83. Child1 * c3 = &(Child1)c2;
  84. c3->publicFunc();
  85. Base * b = &c;
  86. // b->publicFunc();
  87. /*
  88. Child1 instance -> Base func
  89. Child2 instance -> The non base func
  90. Child1 pointer -> the base func
  91. You get an error if you attempt to call the public func because it is not part of the base class
  92. */
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement