Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C++]Определить конструктор для инициализации поле


Автор: ArtiSoft 13.12.2009, 11:32
У меня такое задание по лабу, кстати она самая сложная и последняя:
дана структура:

struct Student
{
char *LastName;
char *FirstName;
char *Patronimyc;
int Born;
char *Group;
}

Определить конструктор для инициализации полей по умолчанию. Определить конструктор копирования и деструктор. Написать тестовый пример. Строковые поля описать как указатели, при этом в конструкторе должно производиться не тупое присвоение указателей, а копирование строк. Также внести в класс тестовую функцию - изменяющую первую букву отчества на 'Ы' для проверки корректности конструктора.


так вот я ничего непонял что делать?

Автор: airyashov 13.12.2009, 13:11
почитать про ООП

Автор: ArtiSoft 13.12.2009, 16:45
Ну дайте какую нить полезную ссылочку сайта. У меня в коспекте это нет((( 

Автор: t_gran 14.12.2009, 07:19
Код

#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;
}


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)