Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++]Wskaźnik na obiekt innej klasy

Ostatnio zmodyfikowano 2016-05-22 07:29
Autor Wiadomość
mamba908
Temat założony przez niniejszego użytkownika
[C++]Wskaźnik na obiekt innej klasy
» 2016-05-21 22:36:56
Witam serdecznie. Ostatnio jestem zajęty pisaniem swojego projektu na studia, którym ma być program obsługujący zakłady bukmacherskie. Moja dotychczasowa koncepcja to: klasa Bet, która odpowiada za zakład, klasa BetList, która odpowiada za obsługę listy zakładów. Aktualnie chcę dodać klasę UserBet, która jak sama nazwa wskazuje, będzie zakładem użytkownika (jednym z zakładów zawartych w obiekcie klasy BetList, na który użytkownik zdecydował się postawić pieniądze). Chciałbym, aby obiekt klasy UserBet był wskaźnikiem na obiekt klasy Bet (to w miarę logiczne w moim odczuciu, użytkownik wybiera sobie mecz z listy tych dostępnych na serwerze, stawia na niego pieniądze i oczekuje na wynk). W tym miejscu rodzi się pytanie: w jaki sposób zrealizować ideę wyżej wspomnianego wskaźnika? Jeśli wystarczy jedno pole wśród atrybutów prywatnych klasy UserBet, które będzie wskaźnikiem na obiekt klasy Bet (np. Bet* myBet), to w jaki sposób przypisać takiemu wskaźnikowi adres istniejącego już w pamięci zakładu? Dla rozjaśnienia powyższych zawiłości dorzucam kody źródłowe:
Bet
C/C++
#pragma once

#include <iostream>
#include <string>

class Bet;

using namespace std;

class Bet
{
private:
    string firstTeamName;
    string secondTeamName;
    string matchStatus;
   
    double firstTeamCoefficient;
    double secondTeamCoefficient;
    double drawCoefficient;
    int firstTeamGoals;
    int secondTeamGoals;
   
    Bet * next;
public:
    Bet( string name1, string name2, string status, double coefficient1, double coefficient2, double coefficientDraw, int goals1, int goals2 );
    ~Bet();
   
    string get_first_team_name();
    string get_second_team_name();
    string get_match_status();
   
    double get_first_team_coefficient();
    double get_second_team_coefficient();
    int get_first_team_goals();
    int get_second_team_goals();
   
    Bet * get_next();
   
    void set_match_status( string status );
    void set_first_team_goals( int goals );
    void set_second_team_goals( int goals );
   
    void set_next_bet( Bet * );
   
    void print_bet();
   
    friend ostream & operator <<( ostream &, Bet * );
   
    bool checkStatus();
};

Bet::Bet( string name1, string name2, string status, double coefficient1, double coefficient2, double coefficientDraw, int goals1, int goals2 )
{
    this->firstTeamName = name1;
   
    this->secondTeamName = name2;
   
    this->matchStatus = status;
   
    this->firstTeamCoefficient = coefficient1;
   
    this->secondTeamCoefficient = coefficient2;
   
    this->drawCoefficient = coefficientDraw;
   
    this->firstTeamGoals = goals1;
   
    this->secondTeamGoals = goals2;
}


Bet::~Bet()
{
    cout << endl;
    cout << "***REMOVING A MATCH***" << endl;
    cout << "MATCH: " << this->firstTeamName << " " << "vs" << " " << this->secondTeamName << " " << "REMOVED" << endl;
}

string Bet::get_first_team_name()
{
    return this->firstTeamName;
}

string Bet::get_second_team_name()
{
    return this->secondTeamName;
}

string Bet::get_match_status()
{
    return this->matchStatus;
}

double Bet::get_first_team_coefficient()
{
    return this->firstTeamCoefficient;
}

double Bet::get_second_team_coefficient()
{
    return this->secondTeamCoefficient;
}

int Bet::get_first_team_goals()
{
    return this->firstTeamGoals;
}

int Bet::get_second_team_goals()
{
    return this->secondTeamGoals;
}

Bet * Bet::get_next()
{
    return this->next;
}

void Bet::set_match_status( string status )
{
    this->matchStatus = status;
}

void Bet::set_first_team_goals( int goals )
{
    this->firstTeamGoals = goals;
}

void Bet::set_second_team_goals( int goals )
{
    this->secondTeamGoals = goals;
}

void Bet::set_next_bet( Bet * following )
{
    this->next = following;
}

void Bet::print_bet()
{
    cout << this;
}

bool Bet::checkStatus()
{
    if( this->matchStatus == "OPEN" )
    {
        return true;
    }
    else if( this->matchStatus == "CLOSED" )
    {
        return false;
    }
}

ostream & operator <<( ostream & output, Bet * object )
{
    if( object == nullptr )
    {
        return output;
    }
   
    output << endl;
   
    output << "*********" << "INFORMATION ABOUT MATCH" << "*********" << endl;
   
    output << object->firstTeamName << " vs " << object->secondTeamName << " " << "SCORE: " << " " << object->firstTeamGoals << " : " << object->secondTeamGoals << endl;
   
    output << "MATCH STATUS: " << object->matchStatus << endl;
   
    output << "PICK A WINNER" << endl;
   
    output << object->firstTeamName << " " << object->firstTeamCoefficient << " " << "or" << " " << object->secondTeamName << " " << object->secondTeamCoefficient << endl;
   
    output << "DRAW" << "\t" << object->drawCoefficient << endl;
   
    output << "*********" << "***********************" << "*********" << endl;
   
    output << endl;
   
    return output;
}

BetList
C/C++
#pragma once

class BetList;

#include "Bet.h"

class BetList
{
private:
    Bet * head;
public:
    BetList();
    ~BetList();
   
    void add_bet( string, string, string, double, double, double, int, int );
    void remove_bet( string, string );
    void close_bet_set_score( string, string, int, int );
   
    friend ostream & operator <<( ostream & output, BetList & listToPrint );
   
    void print_list();
};

#include "BetList.h"

BetList::BetList()
{
    head = nullptr;
}


BetList::~BetList()
{
}

void BetList::add_bet( string name1, string name2, string status, double coefficient1, double coefficient2, double coefficientDraw, int goals1, int goals2 )
{
    if( status != "OPEN" )
    {
        cout << endl;
       
        cout << "***WARNING: FORBIDDEN ACTION***" << endl;
       
        cout << "An attempt to create a match using a status different than \"OPEN\" DETECTED!!!" << endl;
       
        return;
    }
   
    if( goals1 != 0 || goals2 != 0 )
    {
        cout << endl;
       
        cout << "***WARNING: FORBIDDEN ACTION***" << endl;
       
        cout << "An attempt to create a match using fixed score DETECTED!!!" << endl;
       
        return;
    }
   
    Bet * actual = this->head;
   
    Bet * adder = nullptr;
   
    Bet * helper = actual;
   
    if( actual == nullptr )
    {
        adder = new Bet( name1, name2, status, coefficient1, coefficient2, coefficientDraw, goals1, goals2 );
       
        this->head = adder;
       
        return;
    }
   
    while( actual != nullptr )
    {
        if( actual->get_first_team_name() == name1 && actual->get_second_team_name() == name2 )
        {
            cout << endl;
           
            cout << "***WARNING: FORBIDDEN ACTION***" << endl;
           
            cout << "An attempt to add the bet which is already on the list DETECTED!!!" << endl;
           
            return;
        }
        else
        {
            actual = actual->get_next();
        }
    }
   
    adder = new Bet( name1, name2, status, coefficient1, coefficient2, coefficientDraw, goals1, goals2 );
   
    this->head = adder;
   
    adder->set_next_bet( helper );
}

void BetList::remove_bet( string name1, string name2 )
{
    Bet * deleter = this->head;
   
    Bet * helper = nullptr;
   
    while( deleter != nullptr )
    {
        if( deleter->get_first_team_name() == name1 && deleter->get_second_team_name() == name2 )
        {
            if( deleter->checkStatus() == false )
            {
                if( deleter == this->head )
                {
                    this->head = deleter->get_next();
                }
                else
                {
                    helper->set_next_bet( deleter->get_next() );
                }
                delete deleter;
               
                return;
            }
            else
            {
                cout << endl;
               
                cout << "***WARNING: FORBIDDEN ACTION***" << endl;
               
                cout << "An attempt to remove an OPEN bet DETECTED!!!" << endl;
               
                return;
            }
        }
       
        helper = deleter;
       
        deleter = deleter->get_next();
    }
   
    cout << endl << "***ERROR: ID OF THE MATCH TO BE REMOVED NOT FOUND***" << endl;
}

void BetList::close_bet_set_score( string name1, string name2, int goals1, int goals2 )
{
    Bet * closer = this->head;
   
    while( closer != nullptr )
    {
        if( closer->get_first_team_name() == name1 && closer->get_second_team_name() == name2 )
        {
            if( closer->get_match_status() == "CLOSED" )
            {
                cout << endl;
               
                cout << "MATCH: " << closer->get_first_team_name() << " " << "vs" << " " << closer->get_second_team_name();
               
                cout << " " << "is already finished" << endl;
               
                cout << "SCORE\t" << closer->get_first_team_goals() << " : " << closer->get_second_team_goals() << endl;
            }
            else
            {
                cout << endl;
               
                cout << "***CLOSING THE MATCH***" << endl;
               
                cout << closer->get_first_team_name() << " vs " << closer->get_second_team_name() << " SCORE ";
               
                cout << goals1 << " : " << goals2 << endl;
               
                closer->set_first_team_goals( goals1 );
               
                closer->set_second_team_goals( goals2 );
               
                closer->set_match_status( "CLOSED" );
               
                return;
            }
        }
       
        closer = closer->get_next();
    }
}

ostream & operator <<( ostream & output, BetList & listToPrint )
{
    output << endl << "Current list of Bets:" << endl;
   
    Bet * printer = listToPrint.head;
   
    while( printer != nullptr )
    {
        printer->print_bet();
       
        printer = printer->get_next();
    }
   
    return output;
}

void BetList::print_list()
{
    cout << * this << endl;
}

Przepraszam za brak komentarzy w kodzie (zbieram się do tego jak pies do jeża, mam nadzieję, że nazwy funkcji będą w jakiś sposób sugestią), a także za niejasność postawionych pytań, ale starałem się opisać mój problem najkrócej jak tylko się da).
P-148476
Roman_Podraza
Podpowiedź
» 2016-05-21 23:04:31
Szanowny Panie,

spodziewam się, że przewiduje Pan scenariusz dodania klas UserList oraz UserBet. W takim wypadku dodanie obiektu klasy UserBet do listy betów może wymagać przesunięcia funkcji odpowiedzialnej za znalezienie rzeczonego obiektu klasy Bet poziom wyżej tam, jak mniemam, znajduje się główna klasa, której zadaniem jest zarządzanie pozostałymi i wywoływanie funkcji z użyciem parametryzacji (np. AddBet w głównej klasie wywoła AddBet w BetList określonej odpowiednim parametrem).

Pozdrawiam.
P-148478
mateczek
» 2016-05-21 23:55:56
1 pytanie czym się różni klasa "userbet" od "bet"
2 czy musisz męczyć się z listą własnej produkcji ?? nie wystarczą klasy vector i list z biblioteki standardowej??

3 ogólna odpowiedz na zadane pytanie to dziedziczenie i funkcje wirtualne
C/C++
#include<iostream>
#include<string>
#include <vector>
using namespace std;
class czlowiek {
public:
    string imie;
    int wiek;
    czlowiek( string im, int w ) {
        imie = im;
        wiek = w;
    }
    virtual void przedstawSie() {
        cout << "jestem czlowiek mam lat: " << wiek << endl;
    }
};

//pracownik to człowiek powiększony o składową "stazPracy"
class pracownik
    : public czlowiek
{
public:
    int stazPracy;
    pracownik( string im, int w, int s )
        : czlowiek( im, w )
    {
        stazPracy = s;
    }
    virtual void przedstawSie() {
        cout << "jestem pracownik mam lat: " << wiek << " pracuję lat: " << stazPracy << endl; // będzie się przedstawiał inaczej niż człowiek
    }
};
using namespace std;
int main()
{
    vector < czlowiek *> lista;
    lista.push_back( new czlowiek( "marek", 20 ) );
    lista.push_back( new pracownik( "piotrek", 35, 8 ) );
   
    // chyba o to ci chodziło
   
    for( czlowiek * gosciu: lista ) {
        gosciu->przedstawSie();
    }
   
   
}
P-148479
mamba908
Temat założony przez niniejszego użytkownika
» 2016-05-22 01:29:12
UserBet to zakład, który obstawił użytkownik. Przykładowo, ma do wyboru 10 meczów (betów) ligi angielskiej, obstawia tylko jeden (robi UserBet). Co do drugiego pytania, to niestety nie mogę korzystać z list stl-a - taki wymóg prowadzącego przedmiot. Dziedziczenie i funkcje wirtualne średnio mnie ustawiają - UserBet dziedziczący z Betu to zły pomysł; użytkownik dostaje wtedy dostęp do metod, dzięki którym może np. ustawiać wyniki meczów.
P-148482
mateczek
» 2016-05-22 07:29:30
skoro z klas userBet i Bet nie jesteś w stanie wyodrębnić wspólnej części, po której dziedziczyły by obie klasy, to nie możesz ich trzymać w jednej liście. Więc zrób dwie listy. Albo napisz listę szablonową, jeśli nie chcesz klepać prawie tego samego kodu listy 2 razy.



UserBet to zakład, który obstawił użytkownik.
 mi to stwierdzenie niewiele mówi. Bo brak kodu klasy a po opisie przynajmniej mi trudno się domyślić.
 
P-148485
« 1 »
  Strona 1 z 1