#include using namespace std; class Dept { public: Dept(int idn); // Constructor; int my_id; virtual ~Dept(); // Can't be pure // ~Dept(); // Will have problem if not vertual }; Dept::Dept(int id) :my_id(id) {} Dept::~Dept() // Nothing to be done {} class CS: public Dept { public: CS(int n); // Constructor, n is the no of courses CS(const CS & c); // Copy constructor; CS & operator=(const CS & c); // Overloading =; ~CS(); // Destructor; int *my_courses; int my_n; }; CS::CS(int n) :Dept(5555), my_n(n) { my_courses = new int[n]; } CS::CS(const CS & a) // Copy constructor :Dept(5555) { my_n = a.my_n; my_courses = new int[my_n]; for (int i=0; i< my_n; i++) { my_courses[i] = a.my_courses[i]; } } CS & CS::operator=(const CS & rvalue) { if (this == & rvalue) // This handles a = a situation; return *this; // Nothing to be done; Dept::operator=(rvalue); // Assign values defined in the base class; my_n = rvalue.my_n; delete [] my_courses; // Remove the dynamic arrays of the calling object my_courses = new int[my_n]; // Create new dynamic arrays of the calling object for (int i=0; i< my_n; i++) // copy the contents of the arrays of the operand's { // into the newly created arrays of the calling object. my_courses[i] = rvalue.my_courses[i]; } return *this; } CS::~CS() // destructor { delete [] my_courses; cout << my_n << " numbers deleted\n"; // Just for demo; } class Honor: public CS { public: Honor(int n, int h); // Constructor; ~Honor(); int my_h; int *h_courses; }; Honor::Honor(int n, int h) :CS(n), my_h(h) { h_courses = new int[my_h]; } Honor::~Honor() { delete [] h_courses; cout << my_h << " honor courses deleted\n"; } void print(CS c) { cout << "\n" << c.my_id << endl; for (int i=0; i< c.my_n; i++) cout << c.my_courses[i] << ","; cout << "\n\n"; } int main() { int n=7; CS a(n),b(4); for (int i=0; i< n; i++) a.my_courses[i] = 1000+i+1; b = a; print(b); cout << endl; CS *pc; pc = new CS(21); delete pc; cout << endl; Dept *pd; pd = new CS(1500); delete pd; cout << endl; Honor *hp; hp = new Honor(190,19); delete hp; cout << endl; CS *cp; cp = new Honor(230,23); delete cp; cout << endl; Dept *dp; dp = new Honor(290,29); delete dp; cout << endl; return 0; } /******* Outputs of this program ************************** 5555 1001,1002,1003,1004,1005,1006,1007, 7 numbers deleted 21 numbers deleted 1500 numbers deleted 19 honor courses deleted 190 numbers deleted 23 honor courses deleted 230 numbers deleted 29 honor courses deleted 290 numbers deleted 7 numbers deleted 7 numbers deleted Press any key to continue ***********************************************************/