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

[C++]Początki z programowaniem obiektowym

Ostatnio zmodyfikowano 2013-07-28 13:39
Autor Wiadomość
marcin107
Temat założony przez niniejszego użytkownika
[C++]Początki z programowaniem obiektowym
» 2013-07-28 12:15:32
Witam, zacząłem przepisywać swoją grę konsolową z proceduralnego na obiektowe i... utknąłem.
C/C++
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


class hero {
private:
    int herotype;
    int lvl;
    int exp;
    int skillpoints;
    int life;
    int maxlife;
    int mana;
    int maxmana;
    int ap;
    int ad;
    int armor;
    int reduce;
    int critic;
    int avoid;
    int qspell_lvl;
    int wspell_lvl;
    int espell_lvl;
    int rspell_lvl;
    int gold;
    int lifesteal;
    int manaregen;
public:
    string choice;
    void setentrystats( int q1, int q2, int q3, int q4, int q5, int q6, int q7, int q8, int q9, int q10, int q11, int q12, int q13, int q14, int q15, int q16, int q17, int q18, int q19, int q20, int q21 );
   
};
class menu {
private:
    int shortdelay = 1000;
    int mediumdelay = 1500;
    int longdelay = 2000;
public:
    string choice;
    void begin();
    void mainmenu();
    void heropick();
   
};

void menu::begin()
{
    system( "CLS" );
    cout << "Witaj w grze Gladiatorzy!\n";
    cout << "1. Rozpocznij gre\n";
    cout << "2. Wyjdz\n";
    cout << "\nAutor gry: Marcin Hajduk\n";
    cout << "Wersja gry: None\n";
    cin >> choice;
    if( choice == "1" )
         heropick();
    else if( choice == "2" )
         system( "exit" );
    else
    {
        cout << "Nie ma takiej opcji!\n";
        Sleep( shortdelay );
        begin();
    }
}


void menu::heropick()
{
    system( "CLS" );
    cout << "Wybierz swojego bohatera!\n\n";
    cout << "Wojownik\n";
    cout << "Zycia - srednie\n";
    cout << "Mana - niska\n";
    cout << "Atak fizyczny - silny\n";
    cout << "Atak magiczny - slaby\n";
    cout << "Pancerz - sredni\n\n";
    cout << "DODATKOWE: +10% szans na krytyczne uderzenie\n\n";
    cout << "1. WYBIERAM!\n";
    cout << "9. Dalej\n";
    cin >> choice;
    if( choice == "1" )
         setentrystats( 1, 1, 0, 1, 1000, 1000, 100, 100, 76, 0, 26, 0, 10, 0, 0, 0, 0, 0, 200, 0, 20 );
    else
    {
        cout << "Nie ma takiej opcji!\n";
        Sleep( mediumdelay );
        heropick();
    }
    break;
}

void hero::setentrystats( int q1, int q2, int q3, int q4, int q5, int q6, int q7, int q8, int q9, int q10, int q11, int q12, int q13, int q14, int q15, int q16, int q17, int q18, int q19, int q20, int q21 )
{
    hero stats1;
    stats1.herotype = q1;
    stats1.lvl = q2;
    stats1.exp = q3;
    stats1.skillpoints = q4;
    stats1.life = q5;
    stats1.maxlife = q6;
    stats1.mana = q7;
    stats1.maxmana = q8;
    stats1.ad = q9;
    stats1.ap = q10;
    stats1.armor = q11;
    stats1.reduce = q12;
    stats1.critic = q13;
    stats1.avoid = q14;
    stats1.qspell_lvl = q15;
    stats1.wspell_lvl = q16;
    stats1.espell_lvl = q17;
    stats1.rspell_lvl = q18;
    stats1.gold = q19;
    stats1.lifesteal = q20;
    stats1.manaregen = q21;
}

int main()
{
    menu k;
    k.begin();
    return 0;
}
Pytania do powyższego kodu:
1) Czy jest coś takiego jak obiekt globalny? tzn. po utworzeniu obiektu w main już go nie widać w klasach, więc nie będę mógł używać swojego obiektu stats1 w całym programie kiedy chcę.
2) Czy utworzenie klasy hero ma sens? tzn. w obiektowym tworzy się klasę w niej np. statystyki bohatera a potem obiekt, który przechowuje te dane?(Czyli np. jakiś stworek w grze to obiekt klasy?)
3) Jak wywoływać funkcje, która jest w klasie A, przez funkcję w klasie B?
4) Czy zawsze musi być obiekt klasy nawet jeśli chce zrobić jak tu w przypadku z menu::begin() ?
5) Jak poprawić kod ze stronki?
 
Proszę o pomoc.
P-88925
pekfos
» 2013-07-28 12:25:13
1) Czy jest coś takiego jak obiekt globalny?
Jest. Tak samo jak zmienna globalna.

3) Jak wywoływać funkcje, która jest w klasie A, przez funkcję w klasie B?
Jeśli chodzi o niestatyczną metodę, to musisz mieć dostęp do danego obiektu klasy A.

4) Czy zawsze musi być obiekt klasy nawet jeśli chce zrobić jak tu w przypadku z menu::begin() ?
Do wywołania metod niestatycznych musisz mieć obiekt.

5) Jak poprawić kod ze stronki?
A co nie działa? Strzelam, wywal tę linię:
C/C++
hero stats1;
i wszystkie "stats1." z metody setentrystats()

Początki z programowaniem obiektowym
Początki lepiej zacząć od kursu, a nie od naklepania na ślepo kodu i pytania o podstawowe rzeczy.
P-88927
marcin107
Temat założony przez niniejszego użytkownika
» 2013-07-28 12:29:54
C:\...\object\main.cpp|41|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\...\object\main.cpp|42|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\...\object\main.cpp|43|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\...\object\main.cpp||In member function 'void menu::heropick()':|
C:\...\object\main.cpp|89|error: 'setentrystats' was not declared in this scope|
||=== Build finished: 1 errors, 3 warnings (0 minutes, 0 seconds) ===|
Jak się robi coś a potem rodzą się pytania, na które uzyskuję się odpowiedź to moim zdaniem najlepszy sposób nauki, jednak stawiając się na waszej sytuacji gdyby każdy miał tak pytać, to faktycznie jest to trochę niewygodne.
P-88928
pekfos
» 2013-07-28 12:34:02
C/C++
int shortdelay = 1000;
int mediumdelay = 1500;
int longdelay = 2000;
Nie możesz tak zrobić w C++03.
Nie możesz tak wywołać setentrystats(), nie możesz tak użyć break. Przeczytaj to, co dopisałem do postu wyżej.
P-88929
marcin107
Temat założony przez niniejszego użytkownika
» 2013-07-28 12:35:15
break to wypadek przy skracaniu kody przed wrzuceniem na forum, a co do reszty to zaraz się z tym pobawię.

Delaye wywaliłem zostało tylko to:
C:\...\object\main.cpp||In member function 'void menu::heropick()':|
C:\...\object\main.cpp|85|error: 'setentrystats' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
P-88930
pekfos
» 2013-07-28 12:39:47
Nie możesz tak wywołać setentrystats(). Musisz mieć obiekt hero.
P-88933
marcin107
Temat założony przez niniejszego użytkownika
» 2013-07-28 12:49:33
C/C++
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


class hero {
private:
    int herotype;
    int lvl;
    int exp;
    int skillpoints;
    int life;
    int maxlife;
    int mana;
    int maxmana;
    int ap;
    int ad;
    int armor;
    int reduce;
    int critic;
    int avoid;
    int qspell_lvl;
    int wspell_lvl;
    int espell_lvl;
    int rspell_lvl;
    int gold;
    int lifesteal;
    int manaregen;
public:
    string choice;
    void heropick();
    void setentrystats( int q1, int q2, int q3, int q4, int q5, int q6, int q7, int q8, int q9, int q10, int q11, int q12, int q13, int q14, int q15, int q16, int q17, int q18, int q19, int q20, int q21 );
    void mainmenu();
    void begin();
};

void hero::begin()
{
    system( "CLS" );
    cout << "Witaj w grze Gladiatorzy!\n";
    cout << "1. Rozpocznij gre\n";
    cout << "2. Wyjdz\n";
    cout << "\nAutor gry: Marcin Hajduk\n";
    cout << "Wersja gry: None\n";
    cin >> choice;
    if( choice == "1" )
         heropick();
    else if( choice == "2" )
         system( "exit" );
    else
    {
        cout << "Nie ma takiej opcji!\n";
        Sleep( 1000 );
        begin();
    }
}


void hero::heropick()
{
    system( "CLS" );
    cout << "Wybierz swojego bohatera!\n\n";
    cout << "Wojownik\n";
    cout << "Zycia - srednie\n";
    cout << "Mana - niska\n";
    cout << "Atak fizyczny - silny\n";
    cout << "Atak magiczny - slaby\n";
    cout << "Pancerz - sredni\n\n";
    cout << "DODATKOWE: +10% szans na krytyczne uderzenie\n\n";
    cout << "1. WYBIERAM!\n";
    cout << "9. Dalej\n";
    cin >> choice;
    if( choice == "1" )
         setentrystats( 1, 1, 0, 1, 1000, 1000, 100, 100, 76, 0, 26, 0, 10, 0, 0, 0, 0, 0, 200, 0, 20 );
    else
    {
        cout << "Nie ma takiej opcji!\n";
        Sleep( 1500 );
        heropick();
    }
}

void hero::setentrystats( int q1, int q2, int q3, int q4, int q5, int q6, int q7, int q8, int q9, int q10, int q11, int q12, int q13, int q14, int q15, int q16, int q17, int q18, int q19, int q20, int q21 )
{
    hero stats1;
    stats1.herotype = q1;
    stats1.lvl = q2;
    stats1.exp = q3;
    stats1.skillpoints = q4;
    stats1.life = q5;
    stats1.maxlife = q6;
    stats1.mana = q7;
    stats1.maxmana = q8;
    stats1.ad = q9;
    stats1.ap = q10;
    stats1.armor = q11;
    stats1.reduce = q12;
    stats1.critic = q13;
    stats1.avoid = q14;
    stats1.qspell_lvl = q15;
    stats1.wspell_lvl = q16;
    stats1.espell_lvl = q17;
    stats1.rspell_lvl = q18;
    stats1.gold = q19;
    stats1.lifesteal = q20;
    stats1.manaregen = q21;
}

int main()
{
    hero k;
    k.begin();
    return 0;
}

Teraz się niby kompiluje, ale:
1) Wszystko co ma ze sobą jakiś związek nawet najmniejszy jak przechodzenie z jednego menu do drugiego musi być w jednej klasie? Jeśli nie, to jak to zrobić? Jakiś rozdział gdzie jest to wyjaśnione...
2) Kiedy obiekt jest stworzony w hero::setentrystats, nie będę mógł go użyć nigdzie indziej? Jeśli tak, to jak to zglobalizować?
P-88934
pekfos
» 2013-07-28 12:54:28
1) Rozdział o robieniu konkretnego menu obiektowo? No nie żartuj.. Jak zrobisz, tak będzie (o ile zrobisz poprawnie)

2)
wywal tę linię:
C/C++
hero stats1;
i wszystkie "stats1." z metody setentrystats()
P-88935
« 1 » 2
  Strona 1 z 2 Następna strona