[C++] dziedziczenie konstruktorów

niemo89
Użytkownik
Użytkownik
Posty: 16
Rejestracja: 20 sty 2009, o 18:41
Płeć: Mężczyzna
Podziękował: 2 razy

[C++] dziedziczenie konstruktorów

Post autor: niemo89 »

Witam. Napisałem program i mam 2błędy, siedzę nad tym i nie mogę wymyślić o co chodzi kompilatorowi... Może mi ktoś pomóc i poprawić te błędy tak, by wszystko śmigało jak należy?
Oto program:
#include <iostream>
#include <string>

using namespace std;

class Tstudent{
public:
string Nazwisko;
int Nr_indexu;
int Liczba_ocen;
double* Oceny;
Tstudent(const Tstudent &in);
Tstudent();
Tstudent(string nazw, int index);
Tstudent(string nazw, int index, int l_ocen, double *stopnie);
~Tstudent();
int Wypisz();
int operator== (const Tstudent &s);
void operator>> (const Tstudent &s);
void operator<< (const Tstudent &s);

};
int operator== (Tstudent &s, Tstudent &d){
if( (s.Nazwisko==d.Nazwisko) && (s.Nr_indexu==d.Nr_indexu) ){
return 0;
}
return 1;
};

void operator>> (Tstudent &s,int a){
cout << s.Nazwisko << " " << s.Nr_indexu << endl;
for(a=0;a<=s.Liczba_ocen;a++){
cout << s.Oceny[a] << " ";
}
cout << endl;
};

void operator<< (Tstudent &s, int a){
cout << "Podaj nazwisko : ";
cin >> s.Nazwisko;
cout << endl << "Podaj nr indexu : ";
cin >> s.Nr_indexu;
cout << endl << "Podaj liczbe ocen : ";
cin >> s.Liczba_ocen;
cout << endl;
};

Tstudent::Tstudent(){
Nazwisko="0";
Nr_indexu=0;
Liczba_ocen=0;
Oceny=NULL;
}

Tstudent::Tstudent(string nazw, int index){
Nazwisko=nazw;
Nr_indexu=index;
Liczba_ocen=0;
Oceny=NULL;
}

Tstudent::Tstudent(string nazw, int index, int l_ocen, double *stopnie){
Nazwisko=nazw;
Nr_indexu=index;
Liczba_ocen=l_ocen;
Oceny=new double[l_ocen];
for (int i=0; i<l_ocen; i++)
Oceny=stopnie;
}

Tstudent::Tstudent(const Tstudent &in) {
Nazwisko = in.Nazwisko;
Nr_indexu = in.Nr_indexu;
Liczba_ocen = in.Liczba_ocen;
if(Liczba_ocen != 0) {
Oceny = new double[Liczba_ocen];
for(int i = 0; i < Liczba_ocen; i++)
Oceny = in.Oceny;
}
}

int Tstudent::Wypisz(){
cout<<Nazwisko<<" "<<Nr_indexu<<" "<<Liczba_ocen;
for(int i=0;i<Liczba_ocen;i++){
cout<<" "<<Oceny;
}
cout<<"
";
}

Tstudent::~Tstudent(){
if (Oceny!=NULL) delete Oceny;
}
class Nowa_Klasa : public Tstudent{
string Imie;
string Wydzial;
Nowa_Klasa();
Nowa_Klasa(string nazw, string im);
Nowa_Klasa(string nazw, int index);
Nowa_Klasa(string nazw, string im, string wydz, int index);
};
Nowa_Klasa::Nowa_Klasa():Tstudent() {}
Nowa_Klasa::Nowa_Klasa(string nazw, string im):Tstudent(nazw) {Imie=im;}
Nowa_Klasa::Nowa_Klasa(string nazw, int index):Tstudent(nazw,index){}
Nowa_Klasa::Nowa_Klasa(string nazw, string im, string wydz, int index):Tstudent(nazw,index)
{Imie=im;Wydzial=wydz;}



int main(){
Tstudent Grupa1[20];
Tstudent Kazio ("Nowak", 2349);
double T[2]={3.5,4};
Tstudent Jasio ("Kowalski", 3480, 2, T);
Tstudent Wicio(Jasio);
Tstudent Stasio;
Stasio = Kazio;
int a;
a= Stasio==Kazio; //jeżeli 0 to te same dane, a jeżeli 1 to inne
Stasio<<a;
Kazio<<a;
Kazio>>a;
cin>>a;
cout << a << endl;
Jasio. Wypisz();
Stasio. Wypisz();
Kazio. Wypisz();
Wicio. Wypisz();
double O[3]={2,3,4.5};
Nowa_Klasa Zbyszko ("Michalski", 2345, 3, O, "Zbigniew", "W-4");
system("PAUSE");
return 0;
}
spajder
Użytkownik
Użytkownik
Posty: 735
Rejestracja: 7 lis 2005, o 23:56
Płeć: Mężczyzna
Lokalizacja: Łódź
Podziękował: 2 razy
Pomógł: 133 razy

[C++] dziedziczenie konstruktorów

Post autor: spajder »

1. STOSUJ WCIĘCIA, tego się nie da czytać!!!! Oddzielaj deklaracje od definicji.
2. Na następny raz podaj błędy, jakie wypluwa kompilator
3. Czemu operator porównania zwraca jako wartość int? Zdecydowanie powinien bool
4. Czemu operatory << oraz >> są składowymi klasy Tstudent? Czy chcesz go używać tak:

Kod: Zaznacz cały

Tstudent s1, s2;
s1 << s2;
to powinny być funkcje zaprzyjaźnione z klasą, dodatkowo dobrze by było, żeby zwracały ostreram& lub &istream. Jeśli tego nie chcesz zrobić lepiej zadowolić się metodami wypisz() i wczytaj().
ps. tak swoją drogą standardowo >> wczytuje dane, << je wypisuje, Ty masz odwrotnie. Nie, żeby to było zabronione, ale skutecznie utrudnia zrozumienie kodu


Co do błędów:

Kod: Zaznacz cały

Nowa_Klasa::Nowa_Klasa(string nazw, string im):Tstudent(nazw) {Imie=im;}
Tak jak wypluł kompilator: klasa Tstudent nie ma konstruktora, mającego jeden parametr - string. Gdyby był, wygladałby tak:

Kod: Zaznacz cały

Tstudent(const string&);
// lub ewentualnie tak
Tstudent(string);
problem jest też w tej linijce:

Kod: Zaznacz cały

Nowa_Klasa Zbyszko ("Michalski", 2345, 3, O, "Zbigniew", "W-4");
Gdyż używasz 6-argumentowego konstruktora klasy Nowa_klasa a takiego nie ma

Po zakomentowaniu tych dwóch linijek program się kompiluje (co nie znaczy, że działa, ot choćby dlatego, że nigdzie nie alokujesz tablicy ocen a próbujesz ją wypisać). Ogólnie polecam, przynajmniej na razie, zakomentować wypisywanie tych ocen (bo i tak nie są nigdzie wpisywane), dodasz je, jak reszta programu będzie działać.
ODPOWIEDZ