Josif_tepe

Untitled

Sep 11th, 2025
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A {
  5. private:
  6.     int a;
  7. public:
  8.    
  9.     A (int _a) {
  10.         a = _a;
  11.     }
  12.     virtual void print() {
  13.         cout << a << endl;
  14.     }
  15. };
  16.  
  17. class B : public A {
  18. private:
  19.     int b;
  20. public:
  21.     B(int _a, int _b) : A(_a) {
  22.         b = _b;
  23.     }
  24.    
  25.     virtual void print() override {
  26.         cout << b << endl;
  27.     }
  28. };
  29.  
  30. int main() {
  31.    
  32.     A * a = new B(4, 5);
  33.    
  34.     a->print();
  35.    
  36.      return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment