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


Автор: 6ooxa 31.3.2007, 00:42
Помогите с задачкой...
Создать программу с классом Abitur, который включает в себя ФИО, адрес и оценки по предметам абитуриентов, сдавших экзамены в университет.
Номер для каждого абитуриетна запрашивается, а массив оценок создается в динамической памяти.
В состав класса входт следующие функции-члены класса:
конструктор с параметрами
деструктор
функция просмотра текущего состояния объектов PRINT()
функция-друг, позволющая определить количество абитуриентов, проживающих в Москве
виртуальная функция просмотра текущего объекта print()

Производный класс Abitur_1 содержит следующие данные о льготах:
тип медали(золотая или серебрянная)
призовое место на вероссийской олимпиаде
переопределенную функцию просмотра состояния объектов-абитуриентов, имеющих льготы при поступлении.


M
Kuvaldis
Название темы должно отражать ее муть!

Автор: GIK 1.4.2007, 12:11
Что мне понравилось в топе, дак это его название - класс, которое в полной мере отражает суть вопроса smile

Вот, кое что набросал, если что переделаешь под свои нужды smile
Код

#include<stdio>
#include<conio>
#include<cstdio>
#include<string>
#include<stdlib>
#include<iostream>
#include<fstream>
#include<new>


using namespace std;

class Abitur{
 int i, j;
 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];

 public:
  Abitur(const char *name_,const char *surname_, const char *patronymic_, const char *address_ , const int *estimation);
  ~Abitur();

friend int inMoscow(Abitur *abitur[]);
friend class Abitur_;
virtual void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
   cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  }
};

class Abitur_: public Abitur{
  char medal[7]; //Gold end Silver
  int place;     //1 2 end 3

  public:
   Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_);
  ~Abitur_();

  void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
  cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  cout<<"Medal: "<<medal<<endl;
  cout<<"Place: "<<place<<endl;
  };
};


 Abitur::Abitur(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_)
 {
   strcpy(name, name_);
   strcpy(surname, surname_);
   strcpy(patronymic, patronymic_);
   strcpy(address, address_);

   for(j=0; j<3; j++)
    {
     estimation[j]=estimation_[j];
    }

 };

 Abitur::~Abitur(){
 };

 Abitur_::Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_)
 {
   strcpy(name, name_);
   strcpy(surname, surname_);
   strcpy(patronymic, patronymic_);
   strcpy(address, address_);
   strcpy(medal, medal_);
   place=place_;

   for(j=0; j<3; j++)
    {
     estimation[j]=estimation_[j];
    }
 }

 Abitur_::~Abitur_(){
 };


 int inMoscow(Abitur *abitur[]){
  int n=0;;
   for(int i=0; i<3; i++){
    if(strcmp(abitur[i]->address, "Moscow")==0){ n++;};
   }
  return n;
};



 void main(){
 int i, j, r;
 Abitur *abitur[6];

 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];
 char medal[7];
 int place;



 cout<<"Enter the data on the student:"<<endl;

 for(i=0; i<3; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try{
    abitur[i] = new Abitur(name, surname, patronymic, address, estimation);
  } catch(bad_alloc ba){
   cout<<"Mistake at allocation of memory";
   getch;
   return;
  }
 }

 for(i=3; i<6; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Medal :";
   cin>>medal;
   cout<<endl;
   cout<<"Place :";
   cin>>place;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try{
    abitur[i] = new Abitur_(name, surname, patronymic, address, estimation, medal, place);
  } catch(bad_alloc ba){
   cout<<"Mistake at allocation of memory";
   getch;
   return;
  }
 }


 cout<<"Enter number of the student:";
 cin>>r;
 if(r<0 || r>6){cout<<"Not the correct data"<<endl;}
 else{r--; abitur[r]->print_() ;}

 cout<<inMoscow(abitur)<<" students live at Moscow"<<endl;

 delete [] abitur;
 getch();

}

 

Автор: GIK 1.4.2007, 17:30
Опа, косячок smile Конструктор производного класса глюк выдает, не могу понять зачим так smile
Народ, посмотрите.

Автор: Kuvaldis 1.4.2007, 17:34
GIK, 
Цитата

Конструктор производного класса глюк выдает, не могу понять зачим так

Потому что у тебя это 666-е сообщение 

Автор: Xenon 1.4.2007, 17:41
Жуть, ошибок дофига было - либо твой компилятор косячит, либо ты все ворнинги повырубал smile
Исправил более-менее:
Код

#include<stdio.h>
#include<conio.h>
#include<cstdio>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<new>


using namespace std;

class Abitur{
 int i, j;
 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];

 public:
  Abitur(const char *name_,const char *surname_, const char *patronymic_, const char *address_ , const int *estimation);
  virtual ~Abitur();

friend int inMoscow(Abitur *abitur[]);
friend class Abitur_;
virtual void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
   cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  }
};

class Abitur_: public Abitur{
  char medal[7]; //Gold end Silver
  int place;     //1 2 end 3

  public:
   Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_);
  ~Abitur_();

  void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
  cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  cout<<"Medal: "<<medal<<endl;
  cout<<"Place: "<<place<<endl;
  };
};


 Abitur::Abitur(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_)
 {
   strcpy(name, name_);
   strcpy(surname, surname_);
   strcpy(patronymic, patronymic_);
   strcpy(address, address_);

   for(j=0; j<3; j++)
    {
     estimation[j]=estimation_[j];
    }

 };

 Abitur::~Abitur(){
 };

 Abitur_::Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_):Abitur(name_,surname_,patronymic_,address_,estimation)
 {
    strcpy(medal, medal_);
    place=place_;
 }

 Abitur_::~Abitur_(){
 };


 int inMoscow(Abitur *abitur[]){
  int n=0;;
   for(int i=0; i<3; i++){
    if(strcmp(abitur[i]->address, "Moscow")==0){ n++;};
   }
  return n;
};



 void main(){
 int i, j, r;
 Abitur *abitur[6];

 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];
 char medal[7];
 int place;



 cout<<"Enter the data on the student:"<<endl;

 for(i=0; i<3; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try{
    abitur[i] = new Abitur(name, surname, patronymic, address, estimation);
  } catch(bad_alloc ba){
   cout<<"Mistake at allocation of memory";
   getch();
   return;
  }
 }

 for(i=3; i<6; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Medal :";
   cin>>medal;
   cout<<endl;
   cout<<"Place :";
   cin>>place;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try
   {
       abitur[i] = new Abitur_(name, surname, patronymic, address, estimation, medal, place);    
   }
   catch(bad_alloc ba)
   {
   cout<<"Mistake at allocation of memory";
   getch();
   return;
  }
 }


 cout<<"Enter number of the student:";
 cin>>r;
 if(r<0 || r>6){cout<<"Not the correct data"<<endl;}
 else{r--; abitur[r]->print_() ;}

 cout<<inMoscow(abitur)<<" students live at Moscow"<<endl;

 for (int i = 0; i < 6; ++i)
 {
     delete abitur[i];
 }
 getch();
}


Kuvaldis Опередил smile

Автор: GIK 1.4.2007, 20:38
Xenon,  а зачем ты убрал всю инфу из конструктора проиводного класса, оставил только медаль и место.. smile (Риторический вопрос smile )
Кстати, у меня шестой булдер, и найти что-то по новей не могу (и не хочу, я веть не програмер, это всего лишьт мое хобби smile)
Код

#include<stdio.h>
#include<conio.h>
#include<cstdio>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<new>


using namespace std;

class Abitur{
 int i, j;
 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];

 public:
  Abitur(const char *name_,const char *surname_, const char *patronymic_, const char *address_ , const int *estimation);
  virtual ~Abitur();

friend int inMoscow(Abitur *abitur[]);
friend class Abitur_;
virtual void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
   cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  }
};

class Abitur_: public Abitur{
  char medal[7]; //Gold end Silver
  int place;     //1 2 end 3

  public:
   Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_);
  ~Abitur_();

  void print_(){
  cout<<"Name: "<< name <<endl;
  cout<<"Surname: "<< surname <<endl;
  cout<<"Patronymic: "<< patronymic <<endl;
  cout<<"Address: "<<address<<endl;
  for(int i=0; i<3; i++)
  cout << "estimation "<< i+1 <<" "<< estimation[i] <<endl;
  cout<<"Medal: "<<medal<<endl;
  cout<<"Place: "<<place<<endl;
  };
};


 Abitur::Abitur(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_)
 {
   strcpy(name, name_);
   strcpy(surname, surname_);
   strcpy(patronymic, patronymic_);
   strcpy(address, address_);

   for(j=0; j<3; j++)
    {
     estimation[j]=estimation_[j];
    }

 };

 Abitur::~Abitur(){
 };

 Abitur_::Abitur_(const char *name_, const char *surname_, const char *patronymic_, const char *address_ , const int *estimation_, const char *medal_, int place_):Abitur(name_,surname_,patronymic_,address_,estimation)
 {
   strcpy(name, name_);
   strcpy(surname, surname_);
   strcpy(patronymic, patronymic_);
   strcpy(address, address_);
   strcpy(medal, medal_);
   place=place_;
   for(j=0; j<3; j++)
    {
     estimation[j]=estimation_[j];
    }
 }

 Abitur_::~Abitur_(){
 };


 int inMoscow(Abitur *abitur[]){
  int n=0;;
   for(int i=0; i<3; i++){
    if(strcmp(abitur[i]->address, "Moscow")==0){ n++;};
   }
  return n;
};



 void main(){
 int i, j, r;
 Abitur *abitur[6];

 char name[21];
 char surname[21];
 char patronymic[21];
 char address[21];
 int estimation[3];
 char medal[7];
 int place;



 cout<<"Enter the data on the student:"<<endl;

 for(i=0; i<3; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try{
    abitur[i] = new Abitur(name, surname, patronymic, address, estimation);
  } catch(bad_alloc ba){
   cout<<"Mistake at allocation of memory";
   getch();
   return;
  }
 }

 for(i=3; i<6; i++){
   cout<<"Name :";
   cin>>name;
   cout<<endl;
   cout<<"Surname :";
   cin>>surname;
   cout<<endl;
   cout<<"patronymic :";
   cin>>patronymic;
   cout<<endl;
   cout<<"Address :";
   cin>>address;
   cout<<endl;
   cout<<"Medal :";
   cin>>medal;
   cout<<endl;
   cout<<"Place :";
   cin>>place;
   cout<<endl;
   cout<<"Estimation :";
   cout<<endl;
   for(j=0; j<3; j++){
    cout << "estimation number"<< j+1 <<": ";
    cin>>estimation[j];
    cout<<endl;
   }
   try
   {
       abitur[i] = new Abitur_(name, surname, patronymic, address, estimation, medal, place);    
   }
   catch(bad_alloc ba)
   {
   cout<<"Mistake at allocation of memory";
   getch();
   return;
  }
 }


 cout<<"Enter number of the student:";
 cin>>r;
 if(r<0 || r>6){cout<<"Not the correct data"<<endl;}
 else{r--; abitur[r]->print_() ;}

 cout<<inMoscow(abitur)<<" students live at Moscow"<<endl;

 for (int i = 0; i < 6; ++i)
 {
     delete abitur[i];
 }
 getch();
}

Xenon,

Добавлено через 4 минуты и 20 секунд
Kuvaldis, 
Цитата

Потому что у тебя это 666-е сообщение 

Исправил smile

Автор: Xenon 1.4.2007, 21:16
Ну и что ты опять делаешь? Ты в списке иницилизации конструктора _Abitur уже базовому классу Abitur передаешь имена и прочее и ЕГО конкструктор Abitur(const char *name_,const char *surname_, const char *patronymic_, const char *address_ , const int *estimation); инициализирует значения этих переменных. В производном классе ты должен иницилизировать только те поля, что были добавлены в произвоном относительно базового - таковыми являются медаль и place.

Автор: GIK 1.4.2007, 21:42
Тфу, блин, я и не заметил твоего добавления в конструкторе: 
:Abitur(name_,surname_,patronymic_,address_,estimation)
Уже было собрался втирать тебе что и как происходит, ладно вовремя заметил..... smile

Автор: Xenon 1.4.2007, 21:43
GIK, Бывает smile Это ж ООП

Автор: GIK 1.4.2007, 21:44
Ты хоть предупреждай что ли в следующий раз, а то я опять облажался бы smile

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