fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Base {
  6. public:
  7. int rn;
  8. string nm, bg, dob;
  9.  
  10. Base() {
  11. cin.ignore();
  12. cout << "Enter name: ";
  13. getline(cin, nm);
  14.  
  15. cin.ignore();
  16. cout << "Enter date of birth: ";
  17. getline(cin, dob);
  18. cout << "Enter blood group: ";
  19. getline(cin, bg);
  20. try
  21. {
  22. if(rn==0)
  23. {
  24. throw "Illegal instruction";
  25. }
  26. cout << "Enter roll no: ";
  27. cin >> rn;
  28. }
  29. catch(...)
  30. {
  31. cout<<"catch exception";
  32. }
  33. }
  34.  
  35. ~Base() {
  36. cout << "Base class destructor executed." << endl;
  37. }
  38.  
  39. friend class Derived;
  40. };
  41.  
  42. class Derived {
  43. public:
  44. string c, d;
  45. static string add;
  46. static string cn;
  47.  
  48. Derived() {
  49. cin.ignore();
  50. cout << "Enter class name: ";
  51. getline(cin, c);
  52. cout << "Enter division: ";
  53. getline(cin, d);
  54. }
  55.  
  56. Derived(const Derived &obj) {
  57. this->c = obj.c;
  58. this->d = obj.d;
  59. }
  60.  
  61. void fun() {
  62. cout << "Enter contact details: ";
  63. getline(cin, cn);
  64. cout << "Enter address: ";
  65. getline(cin, add);
  66. }
  67.  
  68. void display(Base &b) {
  69. cout << "Name: " << b.nm << endl;
  70. cout << "Roll no: " << b.rn << endl;
  71. cout << "Date of birth: " << b.dob << endl;
  72. cout << "Blood group: " << b.bg << endl;
  73. cout << "Class name: " << c << endl;
  74. cout << "Division: " << d << endl;
  75. cout << "Contact no: " << cn << endl;
  76. cout << "Address: " << add << endl;
  77. }
  78. };
  79.  
  80. // Static member initialization
  81. string Derived::add = "";
  82. string Derived::cn = "";
  83.  
  84. int main() {
  85. // Dynamic allocation for Base and Derived objects
  86. Base *b = new Base();
  87. Derived *d = new Derived();
  88.  
  89. d->fun();
  90. d->display(*b);
  91.  
  92. delete b;
  93. delete d;
  94.  
  95. // Allocating second set of objects
  96. Base *b1 = new Base();
  97. Derived *d1 = new Derived();
  98.  
  99. d1->fun();
  100. d1->display(*b1);
  101.  
  102. delete b1;
  103. delete d1;
  104.  
  105. return 0;
  106. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Enter name: Enter date of birth: Enter blood group: catch exceptionEnter class name: Enter division: Enter contact details: Enter address: Name: 
Roll no: 0
Date of birth: 
Blood group: 
Class name: 
Division: 
Contact no: 
Address: 
Base class destructor executed.
Enter name: Enter date of birth: Enter blood group: catch exceptionEnter class name: Enter division: Enter contact details: Enter address: Name: 
Roll no: 0
Date of birth: 
Blood group: 
Class name: 
Division: 
Contact no: 
Address: 
Base class destructor executed.