Zdravím, jsem začátečník v C++ a chtěl bych Vás poprosit o radu
Mým úkolem je vytvořit zásobník pomocí tříd, a poté do něj pomocí funkce push(...) vkládat instance objektu Clovek. Pro představu: vytvořím si člověka s ID, jménem a příjmením. Tyto lidi budu postupně vkládat do zásobníku. Pomocí funkce top() zjištovat aktuálního člověka na vrcholu zásobníku, pomocí pop() zase odebírat tyto lidi.
Nedaří se mi tyto lidi nastrkat do zásobníku a pracovat s nimi třeba jako s čísly.
U funkce top() a pop() mám problémy s návratovou hodnotou. Nevím, co a hlavně proč mám použít. Pokud jsem vkládal pouze císla(int), tak nebyl problém. S OOP teprve začínám, tudíž bohůžel nevím, jak na to
Kod, který jsme vytvořil:
#####################################
Osoba.h
#include <iostream>
#include <string>
using namespace std;
class Osoba
{
private:
string jmeno;
string prijmeni;
int ID;
public:
void setOsoba(int n_ID, string n_jmeno, string n_prijmeni);
void getOsoba();
};
#####################################
Osoba.cpp
#include "Osoba.h"
void Osoba::setOsoba(int n_ID, string n_jmeno, string n_prijmeni)
{
ID = n_ID;
jmeno = n_jmeno;
prijmeni = n_prijmeni;
}
void Osoba::getOsoba()
{
cout <<"ID: " << ID << endl;
cout <<"Jmeno: " << jmeno << endl;
cout <<"Prijmeni: " << prijmeni << endl;
}
#####################################
Zasobnik.h
#include <iostream>
#include <string>
#include "Osoba.h"
using namespace std;
const int N = 100;
class Zasobnik
{
private:
int index;
Osoba pole[N];
public:
bool isEmpty();
bool isFull();
void push(Osoba);
/*Osoba pop();
Osoba top();*/
Zasobnik();
};
####################################
Zasobnik.cpp
#include "Zasobnik.h"
Zasobnik::Zasobnik()
{
index = -1;
}
bool Zasobnik::isEmpty()
{
return (index == -1);
}
bool Zasobnik::isFull()
{
return (index == N);
}
void Zasobnik::push(Osoba jedna)
{
if( ! isFull() )
{
index++;
pole[index] = jedna;
}
else
cout <<"Preteceni" <<endl;
}
Osoba Zasobnik::top()
{
if( !isEmpty() )
return pole[index];
else
{
cout <<"Podteceni" << endl;
}
}
Osoba Zasobnik::pop()
{
if( ! isEmpty() )
{
Osoba pomocna = top();
index--;
return pomocna;
}
else
{
cout <<"Podteceni" << endl;
}
}
##################################
##################################
##################################
Hlavni.cpp
#include <iostream>
#include <string>
#include "Osoba.h"
#include "Zasobnik.h"
using namespace std;
int main()
{
Zasobnik muj;
Osoba jedna;
Osoba dve;
jedna.setOsoba(1, "Michal", "Novak");
muj.push(jedna);
dve.setOsoba(2, "Karel", "Pribyl");
dve.getOsoba();
cout <<"Je prazdny: " << muj.isEmpty() <<endl;
cout <<"Je plny: " << muj.isFull() <<endl;
cout <<endl <<endl;
muj.push(dve);
cout <<"Je prazdny: " << muj.isEmpty() <<endl;
cout <<"Je plny: " << muj.isFull() <<endl;
cout <<endl <<endl;
getchar();
}
Předem děkuji za Vaši ochotu a radu.
Fórum › C / C++
Problém s vložením objektu do zásobníku C++

Určitě by ses měl podívat na přetěžování operátorů. Protože u třídy osoba musíš přetížit operátor přiřazení a ještě kopírovací konstruktor. Pak by možná bylo dobrý vyhodit výjimku místo těch chybovejch hlášení podtečení atd. Takhle ti to akorát napíše a vrátíš bůh ví co...

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 259 hostů
Podobná vlákna
Problém s vložením do databáze — založil stollba
Tkinter pomoc s vlozenim Scrollbar — založil Jarda
Problem se serazenim objektu — založil Vavat9
Problém se zapouzdřením objektů — založil Josef
BIG problém s importováním objektů — založil Biondo
Moderátoři diskuze