Hej! Robię zadanko na studia z obiektówki. Prosta sprawa, natomiast odrobinę się chyba sam zgubiłem co nie działa (a właściwie dlaczego). Czy jest tutaj jakaś dobra dusza, która pomoże rozwiązać problem? Nie jestem wirtuozem c++ niestety :/
Mam kilka plików, natomiast najważniejsze są 3, na których pracuję. Worker.h, Secretary.h i Main.cpp.
Problem polega na tym, że muszę wyświetlić dla każdej klasy konstruktor albo domyślny, albo sparametryzowany, albo kopiujący, który dziedziczy z innego konstruktora. U mnie wygląda to tak: konstruktor Worker odpawiada ogólnie za wszystkich pracowników, nadaje im ID i zwiększa licznik +1 żeby potem w destruktorze zmniejszyć go o -1. Działa mi wszystko na konstruktorach sparametryzowanych, natomiast próbując wrzucić konstruktor domyślny zaczynają mi się sypać błędy.
Worker.h
#pragma once
#include <iostream>
using namespace std;
int counter = 0;
int maxuid = 0;
class Worker
{
protected:
int uid;
double cash;
string name;
string surname;
public:
//KONSTRUKTOR DOMYSLNY
Worker(double cash = 2000, string name = " Pracownik ", string surname = " ")
{
cout << " Konstruktor domyslny " << endl << endl;
this->cash = cash;
this->name = name;
this->surname = surname;
};
//KONSTRUKTOR SPARAMETRYZOWANY
Worker(double cash, string name, string surname) : name(name), surname(surname), cash(cash), uid(maxuid)
{
maxuid++;
counter++;
cout << " KONSTRUKTOR sparametryzowany " << endl << endl;
cout << " UID: " << maxuid << endl;
};
//DESTRUKTOR
~Worker()
{
cout << " Dzialjacy DESTRUKTOR " << endl;
cout << " Licznik PRZED destrukcja: " << counter << endl;
counter--;
cout << " Licznik PO destrukcji: " << counter << endl << endl;
cout << " -----------------------------" << endl << endl;
};
double getCash()
{
return cash;
}
void setCash(double cash)
{
this->cash = cash;
}
string getName()
{
return name;
}
void setName(string name)
{
this->name = name;
}
string getSurname()
{
return surname;
}
void setSurame(string surname)
{
this->surname = surname;
}
};
Secretary.h
#pragma once
#include "Worker.h"
#include "RandCash.h"
class Secretary : protected Worker
{
string ifexperience;
bool experience = 1;
public:
// TAKI ZAPIS NIE DZIAŁĄ, NATOMIAST NIE JESTEM PEWNY JEGO POPRAWNOSCI
Secretary() : Worker()
{
cout << " z klasy secretary " << endl << endl;
};
Secretary(double cash, string name, string surname) : Worker(cash * RandCost(1, 3), name, surname)
{
experience = RandCost(1, 2);
if (experience == true)
{
cash = cash + 1500 * RandCost(1, 3);
ifexperience = "TAK";
}
else
{
ifexperience = "NIE";
}
cout << name << " " << surname << endl;
cout << " Czy posiada doswiadczenie? " << ifexperience << endl;
cout << " W sumie zarabia: " << cash << " brutto" << endl << endl;
}
};
#include <iostream>
#include <ctime>
#include <windows.h>
#include "Boss.h"
#include "ItSpecialist.h"
#include "Secretary.h"
using namespace std;
int main()
{
srand(time(NULL));
Boss* boss = new Boss(6000, " Marek", "Goa");
delete boss;
Sleep(1000);
Secretary* secretary = new Secretary(6000, " Czarek", "Gob");
delete secretary;
Sleep(1000);
ItSpecialist* itspecialist = new ItSpecialist(6000," Darek", "Goc");
delete boss;
Sleep(1000);
// BEZ TEGO DZIALA
Secretary* sec = new Secretary();
delete sec;
Sleep(1000);
getchar();
return 0;
}
Dodałem w pliku Secretary i Main komentarze do miejsc, które psują program. Zdaję sobie sprawę, że problem najpewniej wynika z zapisu. Niestety moja wiedza tutaj się urywa wraz z pomysłami. :/