#include <iostream>
#include <string>
using namespace std;
class Base {
public :
int rn;
string nm, bg, dob;
Base( ) {
cin .ignore ( ) ;
cout << "Enter name: " ;
getline( cin , nm) ;
cin .ignore ( ) ;
cout << "Enter date of birth: " ;
getline( cin , dob) ;
cout << "Enter blood group: " ;
getline( cin , bg) ;
try
{
if ( rn== 0 )
{
throw "Illegal instruction" ;
}
cout << "Enter roll no: " ;
cin >> rn;
}
catch ( ...)
{
cout << "catch exception" ;
}
}
~Base( ) {
cout << "Base class destructor executed." << endl;
}
friend class Derived;
} ;
class Derived {
public :
string c, d;
static string add;
static string cn;
Derived( ) {
cin .ignore ( ) ;
cout << "Enter class name: " ;
getline( cin , c) ;
cout << "Enter division: " ;
getline( cin , d) ;
}
Derived( const Derived & obj) {
this- > c = obj.c ;
this- > d = obj.d ;
}
void fun( ) {
cout << "Enter contact details: " ;
getline( cin , cn) ;
cout << "Enter address: " ;
getline( cin , add) ;
}
void display( Base & b) {
cout << "Name: " << b.nm << endl;
cout << "Roll no: " << b.rn << endl;
cout << "Date of birth: " << b.dob << endl;
cout << "Blood group: " << b.bg << endl;
cout << "Class name: " << c << endl;
cout << "Division: " << d << endl;
cout << "Contact no: " << cn << endl;
cout << "Address: " << add << endl;
}
} ;
// Static member initialization
string Derived:: add = "" ;
string Derived:: cn = "" ;
int main( ) {
// Dynamic allocation for Base and Derived objects
Base * b = new Base( ) ;
Derived * d = new Derived( ) ;
d- > fun( ) ;
d- > display( * b) ;
delete b;
delete d;
// Allocating second set of objects
Base * b1 = new Base( ) ;
Derived * d1 = new Derived( ) ;
d1- > fun( ) ;
d1- > display( * b1) ;
delete b1;
delete d1;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgQmFzZSB7CnB1YmxpYzoKICAgIGludCBybjsKICAgIHN0cmluZyBubSwgYmcsIGRvYjsKCiAgICBCYXNlKCkgewogICAgICAgIGNpbi5pZ25vcmUoKTsKICAgICAgICBjb3V0IDw8ICJFbnRlciBuYW1lOiAiOwogICAgICAgIGdldGxpbmUoY2luLCBubSk7CiAgICAgICAgCiAgICAgICAgY2luLmlnbm9yZSgpOwogICAgICAgIGNvdXQgPDwgIkVudGVyIGRhdGUgb2YgYmlydGg6ICI7CiAgICAgICAgZ2V0bGluZShjaW4sIGRvYik7CiAgICAgICAgY291dCA8PCAiRW50ZXIgYmxvb2QgZ3JvdXA6ICI7CiAgICAgICAgZ2V0bGluZShjaW4sIGJnKTsKICAgICAgICB0cnkKICAgICAgICB7CiAgICAgICAgICAgIGlmKHJuPT0wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0aHJvdyAiSWxsZWdhbCBpbnN0cnVjdGlvbiI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dCA8PCAiRW50ZXIgcm9sbCBubzogIjsKICAgICAgICAgICAgY2luID4+IHJuOwogICAgICAgIH0KICAgICAgICBjYXRjaCguLi4pCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDwiY2F0Y2ggZXhjZXB0aW9uIjsKICAgICAgICB9CiAgICB9CgogICAgfkJhc2UoKSB7CiAgICAgICAgY291dCA8PCAiQmFzZSBjbGFzcyBkZXN0cnVjdG9yIGV4ZWN1dGVkLiIgPDwgZW5kbDsKICAgIH0KCiAgICBmcmllbmQgY2xhc3MgRGVyaXZlZDsKfTsKCmNsYXNzIERlcml2ZWQgewpwdWJsaWM6CiAgICBzdHJpbmcgYywgZDsKICAgIHN0YXRpYyBzdHJpbmcgYWRkOwogICAgc3RhdGljIHN0cmluZyBjbjsKCiAgICBEZXJpdmVkKCkgewogICAgICAgIGNpbi5pZ25vcmUoKTsKICAgICAgICBjb3V0IDw8ICJFbnRlciBjbGFzcyBuYW1lOiAiOwogICAgICAgIGdldGxpbmUoY2luLCBjKTsKICAgICAgICBjb3V0IDw8ICJFbnRlciBkaXZpc2lvbjogIjsKICAgICAgICBnZXRsaW5lKG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