// studenten.cc #include #include #include "studenten3.hh" // --------------------------------------------------------------------- Studenten :: Studenten() { matrikel = skz = 0; pname = pvorname = 0; } // --------------------------------------------------------------------- Studenten :: Studenten(const char vorname[], const char name[], const long int mat_nr, const int skz_nr) { matrikel = mat_nr; skz = skz_nr; pname = new char[strlen(name)+1]; strcpy(pname,name); pvorname = new char[strlen(vorname)+1]; strcpy(pvorname,vorname); } // --------------------------------------------------------------------- // Copy constructor will be applied only to uninitialized data // ===> no deallocation of memory necessary Studenten :: Studenten(const Studenten& orig) { matrikel = orig.matrikel; skz = orig.skz; if (orig.pname !=0 ) { pname = new char[strlen(orig.pname)+1]; strcpy(pname,orig.pname); } else { pname = 0; } if (orig.pvorname != 0) { pvorname = new char[strlen(orig.pvorname)+1]; strcpy(pvorname,orig.pvorname); } else { pvorname = 0; } } // --------------------------------------------------------------------- Studenten :: ~Studenten() { if (pvorname != 0) delete [] pvorname; if ( pname != 0) delete [] pname; } // --------------------------------------------------------------------- // Assignment operator will be applied also to already initialized data // ===> deallocation of memory may be necessary Studenten& Studenten :: operator=(const Studenten & orig) { if ( &orig != this ) { if (pvorname != 0) delete [] pvorname; if (pname != 0) delete [] pname; matrikel = orig.matrikel; skz = orig.skz; if (orig.pname !=0 ) { pname = new char[strlen(orig.pname)+1]; strcpy(pname,orig.pname); } else { pname = 0; } if (orig.pvorname != 0) { pvorname = new char[strlen(orig.pvorname)+1]; strcpy(pvorname,orig.pvorname); } else { pvorname = 0; } } return *this; } // --------------------------------------------------------------------- ostream & operator<<(ostream & s, const Studenten & orig) { return s << orig.pvorname << " " << orig.pname << " , " << orig.matrikel << " , " << orig.skz; } // --------------------------------------------------------------------- void Studenten :: Print(ostream & s) { if (pvorname != 0) s << pvorname << " "; if ( pname != 0) s << pname << " , "; s << matrikel << " , " << skz << endl; return; } // --------------------------------------------------------------------- // Methods to access the private data // Set data values void Studenten :: SetVorname(const char vorname[]) { if (pvorname != 0) delete [] pvorname; pvorname = new char[strlen(vorname)+1]; strcpy(pvorname,vorname); return; } void Studenten :: SetName(const char name[]) { if (pname != 0) delete [] pname; pname = new char[strlen(name)+1]; strcpy(pname,name); return; }