Dziedziczenie, nie widzi konstruktora klasy

0

Dzień dobry, mam problem z dziedziczeniem, naprościej wytłumaczę na kodzie:

 
class Person {
    string imie;
    string nazwisko;

public:
    Person(string i, string n);
    Person(const Person &);
    void Show();
};

Person::Person(string i, string n){
    imie = i;
    nazwisko = n;
}

Person::Person(const Person & p){
    imie = p.imie;
    nazwisko = p.nazwisko;
}

void Person::Show(){
    cout << "Imie=" << imie << "\nNazwisko=" << nazwisko << endl;
}

class Gunslinger : public Person
{
    double time;
    int shots;
public:
    Gunslinger(Person p, double t, int s);
    Gunslinger(string i, string n, double t, int s);
    double Draw();
    void Show();
};

Gunslinger::Gunslinger(Person p, double t, int s): Person(p){
    time = t;
    shots = s;
}

Gunslinger::Gunslinger(string i, string n, double t, int s): Person(i,n){
    time = t;
    shots = s;
}

double Gunslinger::Draw(){
    return time;
}

void Gunslinger::Show(){
Person::Show();
cout << "czas wyciagania=" << time << endl;
cout << "strzlay=" << shots << endl;
}

class PokerPlayer : virtual public Person
{
public:
    PokerPlayer(string i, string n);
    double Draw();
    void Show(){Person::Show(); cout << "\nkarta=" << Draw() << endl;};
};

PokerPlayer::PokerPlayer(string i,string n) : Person::Person(i,n){
}

double PokerPlayer::Draw(){
    return rand()%51 +1;
}

class BadDude : public Gunslinger
{
public:
    BadDude();
    void GDraw();
    void GCard();
    void Show();
};

BadDude::BadDude(){
    Gunslinger::Gunslinger("Bad","Dude",6.6,200); // wywala błąd : no matching function for call to 'Gunslinger::Gunslinger()
}

void BadDude::GDraw(){
    cout << "czas=" << Gunslinger::Draw() << endl;
}

void BadDude::GCard(){
    cout << "karta=" << PokerPlayer::Draw() << endl;
}

zaznaczyłem któe miejsce mu się na podoba, ale nie ma pojęcia dlaczego, przecież powinien widzieć konstruktor tej klasy, dodanie do listy inicjalizacyjnej nic nie zmienia.

Wiem że program jest bez snesu i pewnie posiada literówki, ale nie o to chodzi (takie zadanko znalzałem : ) ).

Dziękuję bardzo za pomoc : D

0
Gunslinger::Gunslinger(Person("Bad", "Dude" ), 6.6, 200); 

Poza tym może być potrzebny default constructor we wszystkich klasach.
Jeśli definiujesz copy constructor dopisz też copy assignment rule of three, five, zero

0

Pomyliłem działy, temat dotyczy C++ ( dziękuję za przeniesienie), Problem rozwiązany, tak wygląda kod:

 
#include <iostream>
#include <string>
#include <random>

using namespace std;

class Person {
    string imie;
    string nazwisko;

public:
    Person(){};
    Person(string i, string n);
    Person(const Person &);
    void Show();
};

Person::Person(string i, string n){
    imie = i;
    nazwisko = n;
}

Person::Person(const Person & p){
    imie = p.imie;
    nazwisko = p.nazwisko;
}

void Person::Show(){
    cout << "Imie=" << imie << "\nNazwisko=" << nazwisko << endl;
}

class Gunslinger : public Person
{
    double time;
    int shots;
public:
    Gunslinger();
    Gunslinger(Person p, double t, int s);
    Gunslinger(string i, string n, double t, int s);
    double Draw();
    void Show();
};

Gunslinger::Gunslinger(Person p, double t, int s): Person(p){
    time = t;
    shots = s;
}

Gunslinger::Gunslinger(string i, string n, double t, int s): Person(i,n){
    time = t;
    shots = s;
}

double Gunslinger::Draw(){
    return time;
}

void Gunslinger::Show(){
Person::Show();
cout << "czas wyciagania=" << time << endl;
cout << "strzlay=" << shots << endl;
}

class PokerPlayer : virtual public Person
{
public:
    PokerPlayer(){};
    PokerPlayer(string i, string n);
    double Draw();
    void Show(){Person::Show(); cout << "\nkarta=" << Draw() << endl;};
};

PokerPlayer::PokerPlayer(string i,string n) : Person::Person(i,n){
}

double PokerPlayer::Draw(){
    return rand()%51 +1;
}

class BadDude : public Gunslinger, public PokerPlayer
{
public:
    BadDude();
    void GDraw();
    void GCard();
    void Show();
};

BadDude::BadDude(): Gunslinger::Gunslinger("Bad", "Dude", 6.6, 200){
}

void BadDude::GDraw(){
    cout << "czas=" << Gunslinger::Draw() << endl;
}

void BadDude::GCard(){
    cout << "karta=" << PokerPlayer::Draw() << endl;
}

jednak czy jeżeli klasa class PokerPlayer : virtual public Person dziedziczy virtualnie, to czy nie powinno być tak że konstruktora klasy Person wywoływany jest tylko przez klase Gunslinger ? Jeżeli nie dodam konstruktora domyślnego klasy person to wywala mi błąd : (

0

Z tego co wyczytałem:
jeżeli klasa bazowa jest klasą która dziedziczy wirtualnie, muszę jawnie wywołać konstruktor klasy pierwszej (w tym przypadku Person) tak ?
Przeczytałem też że tak się nie klepie kodu bo jest zbyt zawiły, i dziedziczenia welokrotnego unikać jak ognia tak ? : )

1

jeżeli klasa bazowa jest klasą która dziedziczy wirtualnie, muszę jawnie wywołać konstruktor klasy pierwszej (w tym przypadku Person) tak ?
Pierwszy raz słyszę, gdzie to wyczytałeś?
Poza tym w Twoim przykładzie Gunslinger też powinien dziedziczyć wirtualnie po Person.

i dziedziczenia welokrotnego unikać jak ognia tak ?
Tak.
I przed znakiem zapytania się nie stawia spacji.

0

No to jak to jest dokładnie? Obecnie taką mam wizję:

jeżeli jest klasa bazowa A, i dziedziczą po niej klasy B i C, a po klasach B i C dziedziczy klasa D to konstruktor klasy D wywołuje konstruktory klasy B oraz C a te wywołują konstruktory klasy A ( każda, czyli konstruktor klasy A jest wywołany 2 razy ).

jeżeli jest klasa bazowa A, i dziedziczą po niej WIRTULANIE klasy B i C, a po klasach B i C dziedziczy klasa D to konstruktor klasy D wywołuje konstruktor klasy A (podany jawnie lub domyślny) a następnie wywołuje konstruktory klasy B i C (Czyli konstruktor klasy A jest wywołany 1 raz).

Tak? Ale dlaczego klasy B i C dziedziczą wirtualnie, skoro teoretycznie na tym etapie dziedziczenia nie wiadomo czy będzie jakaś klasa która dziedziczy po tych klasach?

1 użytkowników online, w tym zalogowanych: 0, gości: 1