Witam
Mam mały problemik z zadaniem 56 (https://sites.google.com/site/pjczaoczne201415/zajecia-8)
Mianowicie przy próbie dodania obiektu do tablicy.
s->add(s1);
chciałem wykorzystać konstruktor kopiujący z Klasy STUDENT. ale coś poszło nie tak.
studenci.cpp
#include "studenci.h"
Studenci::Studenci(int x){
liczba = 0;
student = new Student[x];
}
void Studenci::add(Student &A){
student[liczba].Student(A);
}
void Studenci::show(){
for(int i = 0; i<liczba;i++)
student[i]->show;
}
student.cpp
#include "student.h"
#include <iostream>
#include <cstring>
using namespace std;
Student::Student::Student(){
}
Student::Student(char* imie,char* nazwisko,char* PESEL,char* nrIndex,int wiek,int semestr,bool plec){
this->imie = new char[strlen(imie)+1];
this->imie = imie;
this->nazwisko = new char[strlen(nazwisko)+1];
this->nazwisko = nazwisko;
this->PESEL = new char[strlen(PESEL)+1];
this->PESEL = PESEL;
this->nrIndex = new char[strlen(nrIndex)+1];
this->nrIndex = nrIndex;
this->wiek = wiek;
this->semestr = semestr;
this->plec = plec;
}
Student::Student (const Student &A){
strcpy(imie,A.imie);
strcpy(nazwisko,A.nazwisko);
strcpy(PESEL,A.PESEL);
strcpy(nrIndex,A.nrIndex);
wiek = A.wiek;
semestr = A.semestr;
plec = A.plec;
}
Student::~Student(){
}
void Student::show(){
cout<<"Imie i nazwisko : "<<imie<<" "<<nazwisko<<endl;
cout<<"PESEL : "<<PESEL<<endl;
cout<<"Nr Index : "<<nrIndex<<endl;
cout<<"Wiek : "<<wiek<<endl;
cout<<"Semestr : "<<semestr<<endl;
if(plec==false)
cout<<"Plec : Mezczyzna " <<endl;
else
cout<<"Plec : Kobieta" <<endl;
}
void Student::showDifferences(const Student &A){
cout<<"Differences\t: Left (this) object \tRight (parameter) object"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
if (stricmp(imie,A.imie)!=0)
cout<<"Imie \t\t: "<<imie<<"\t\t\t"<<A.imie<<endl;
if(stricmp(nazwisko,A.nazwisko)!=0)
cout<<"Nazwisko \t: "<<nazwisko<<"\t\t\t"<<A.nazwisko<<endl;
if(stricmp(PESEL,A.PESEL)!=0)
cout<<"PESEL \t\t: "<<PESEL<<"\t\t"<<A.PESEL<<endl;
if(strcmp(nrIndex,A.nrIndex)!=0)
cout<<"Nr Index \t: "<<nrIndex<<"\t\t\t"<<A.nrIndex<<endl;
if(wiek!=A.wiek)
cout<<"Wiek \t\t: "<<wiek<<"\t\t\t"<<A.wiek<<endl;
if(semestr!=A.semestr)
cout<<"Semestr \t: "<<semestr<<"\t\t\t"<<A.semestr<<endl;
if(plec!=A.plec)
cout<<"Plec \t\t: "<<plec<<"\t\t\t"<<A.plec<<endl;
A i mam jeszcze jedno pytanko jak destruktor odpowiednio stworzyć w student ? próbowałem czegoś takiego ale niestety nie doszły do poprawnej wersji więc zrezygnowałem
~Student(){
delete []imie;
delete []nazwisko;
delete []PESEL;
delete []nrIndex;
delete wiek;
delete plec;
}