Skip to content

Instantly share code, notes, and snippets.

@kateolenya
Last active March 29, 2019 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateolenya/fd16273b5bf8a4c38703731aa2a81107 to your computer and use it in GitHub Desktop.
Save kateolenya/fd16273b5bf8a4c38703731aa2a81107 to your computer and use it in GitHub Desktop.
Basic example of inheritance in C++
#include <iostream>
using namespace std;
class Device {
public:
int serial_number = 12345678;
void turn_on() {
cout << "Device is on" << endl;
}
private:
int pincode = 87654321;
};
class Computer: public Device {};
int main() {
Computer Computer_instance;
Computer_instance.turn_on();
cout << "Serial number is: " << Computer_instance.serial_number << endl;
// cout << "Pin code is: " << Computer_instance.pincode << endl;
// will cause compile time error
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment