fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Person {
  5. public:
  6. string name;
  7.  
  8. void showName() {
  9. cout << "Name: " << name << '\n';
  10. }
  11. };
  12.  
  13. class Student : public Person { // Single Inheritance
  14. public:
  15. int roll;
  16.  
  17. void showRoll() {
  18. cout << "Roll: " << roll << '\n';
  19. }
  20. };
  21.  
  22. int main() {
  23. Student s; // Student object
  24. s.name = "Fahim"; // inherited from Person
  25. /// s.roll = 101;
  26.  
  27. s.showName(); // calling base class method
  28. /// s.showRoll(); // calling derived class method
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Name: Fahim