Simple C++ inheritance

inherit.cpp

#include <iostream>

class Base {
  int x;
};

class A : public Base {
  int a;
 public:
  void set_a(int arg) {
    a = arg;
  }
  int get_a() {
    return a;
  }
};

class B : public Base {
  int b;
 public:
  void set_b(int arg) {
    b = arg;
  }
  int get_b() {
    return b;
  }
};

int main() {
  A a_object;
  a_object.set_a(4);

  B b_object;
  b_object.set_b(a_object.get_a());

  std::cout << "a of a_object = " << a_object.get_a() << "\n";
  std::cout << "b of b_object = " << b_object.get_b() << "\n";

  return 0;
}

// OUTPUT
// a of a_object = 1
// b of b_object = 4

This code was developed by me, Georgios Samaras .

Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)

Leave a comment