| Код | #include <iostream> #include <string.h>
using namespace std;
struct TStudent { char *LastName; char *FirstName; char *Patronimyc; int Born; char *Group;
TStudent(): LastName(NULL), FirstName(NULL), Patronimyc(NULL), Born(-1), Group(NULL) {}; TStudent(const char *, const char *, const char *, int, const char *); TStudent(const TStudent &); ~TStudent() {Free();};
void Set(const char *, const char *, const char *, int, const char *); void Free(); bool ChangeLasName(const char);
const TStudent &operator = (const TStudent &); friend ostream &operator << (ostream &os, const TStudent &); }; //----------------------------------------------// TStudent::TStudent ( const char *theLastName, const char *theFirstName, const char *thePatronimyc, int theBorn, const char *theGroup ) { Set(theLastName, theFirstName, thePatronimyc, theBorn, theGroup); }; //----------------------------------------------// TStudent::TStudent(const TStudent &theStudent) { Set(theStudent.LastName, theStudent.FirstName, theStudent.Patronimyc, theStudent.Born, theStudent.Group); } //----------------------------------------------// void TStudent::Set ( const char *theLastName, const char *theFirstName, const char *thePatronimyc, int theBorn, const char *theGroup ) { LastName= new char [strlen(theLastName)+1]; FirstName= new char [strlen(theFirstName)+1]; Patronimyc= new char [strlen(thePatronimyc)+1]; Group= new char [strlen(theGroup)+1]; Born= theBorn; strcpy(LastName, theLastName); strcpy(FirstName, theFirstName); strcpy(Patronimyc, thePatronimyc); strcpy(Group, theGroup); }; //----------------------------------------------// void TStudent::Free() { if (LastName) { delete [] LastName; LastName= NULL; } if (FirstName) { delete [] FirstName; FirstName= NULL; } if (Patronimyc) { delete [] Patronimyc; Patronimyc= NULL; } if (Group) { delete [] Group; Group= NULL; } Born= -1; } //----------------------------------------------// bool TStudent::ChangeLasName(const char theChar) { if (LastName && *LastName) { *LastName= theChar; } return LastName; } //----------------------------------------------// const TStudent &TStudent::operator = (const TStudent &theStudent) { Free(); Set(theStudent.LastName, theStudent.FirstName, theStudent.Patronimyc, theStudent.Born, theStudent.Group); return *this; } //----------------------------------------------// ostream &operator << (ostream &os, const TStudent &theStudent) { return os << theStudent.LastName << " " << theStudent.FirstName << " " << theStudent.Patronimyc << " " << theStudent.Born << " " << theStudent.Group; } //----------------------------------------------//
int main (int argc, char **argv) { TStudent a("Иванов", "Иван", "ХБЗ", 2, "ПОВТ-03"); TStudent b; b= a; b.ChangeLasName('Ы'); cout << a << endl; cout << b << endl; return 0; }
|
|