Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of private inheritance in C++
#include <iostream>
using namespace std;
class Device {
public:
int serial_number = 12345678;
void turn_on() {
cout << "Device is on" << endl;
}
};
class Computer: private Device {
public:
void say_hello() {
turn_on();
cout << "Welcome to Windows 95!" << endl;
}
};
int main() {
Device Unknown_device;
Computer my_computer;
cout << "\t Unknown Device" << endl;
cout << "Serial number is: "<< Unknown_device.serial_number << endl;
Unknown_device.turn_on();
// cout << "Serial number is: " << my_computer.serial_number << endl;
// my_computer.turn_on();
// will cause compile time error
cout << "\t My computer" << endl;
my_computer.say_hello();
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.