Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.