cout<<**i<<endl
Kontenery projekt na zajecia
- Rejestracja: dni
- Ostatnio: dni
- Rejestracja: dni
- Ostatnio: dni
Dzięki... jednak nadal mam problem z tym iteratorem.
for (std::list <Customer*>::iterator it = customers.begin(); it != customers.end(); it++)
{
if (customers[*it]->customer);
}
Chcę porównać nr podany przez użytkownika z numerem wewnątrz obiektu (tego obiektu na który wskazuje wskaźnik w liście)
- Rejestracja: dni
- Ostatnio: dni
Wytłumacz mi po kego ci tworzenie kopii iteratora?
- Rejestracja: dni
- Ostatnio: dni
Nie chcę tworzyć kopii... Chcę porównać dwie wartości tak jak wyżej wspomniałem :
for (std::list <Customer*>::iterator it = customers.begin(); it != customers.end(); it++)
{
if (customers[*it]->*customer->id_person == customer_id)
{
}
}
Rozumiem, że jest to źle, ale nie mam pojęcia jak to zrobić i kombinuję
- Rejestracja: dni
- Ostatnio: dni
Takie coś też jest źle ale może chociaż zbliżam się do poprawnej wersji:
if ((*it)->*customer->id_person == customer_id)
- Rejestracja: dni
- Ostatnio: dni
if ((*it)->id_person == customer_id)
- Rejestracja: dni
- Ostatnio: dni
Dzięki... mam nadzięję, że nie rozłoszczę cię kolejnymi pytaniami...
for (std::list <Customer*>::iterator it = customers.begin(); it != customers.end(); it++)
{
if ((*it)->id_person == customer_id)
{
for (std::list <Vehicle*>::iterator it = cars.begin(); it != cars.end(); it++)
{
if ((*it)->id_car == car_id)
{
bought_cars.splice (bought_cars.begin(), cars, it);
}
}
}
}
Wyskakuje mi że bought_cars jest niezadeklarowany. Jest on zadeklarowany jako private w klasie Customer i klasa ta ma dodane friend class Bureau. Jednak nie załatwia to sprawy
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
tak tylko zapytam, czy masz bana na używanie std::vector/std::map i c++11?
- Rejestracja: dni
- Ostatnio: dni
Nie mam bana... @_13th_Dragon poradził mi żeby skorzystać z listy, stwierdziłem więc, że jest bardziej doświadczony. Na vectorze nie mogłem kopiować elementu z jednego konteneru do drugiego... Map nie używałem, i jako że jest to projekt na zajęcia więc boję się go komplikować...
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
ja bym to zrobił tak
30 min
nie wklejam kodu tylko link bo trochę długie to:
- Rejestracja: dni
- Ostatnio: dni
Myślę, że docieram do końca projektu (jeszcze menu zrobić)... Wszystkie metody działają oprócz dwóch. Metoda change_car_price() po zmianie ceny powinna wyświetlać mi informacje o samochodzi (żeby pokazać zmianę ceny) jednak wyświetla mi na ekran dwukrotnie dane. Następny problem jest w metodzie buy_car(). Nie mogę sobie w niej poradzić z przeniesiem elementu z cars do bought_cars. Wrzucę cały kod, może ktoś znajdzie moje błędy.
Bureau.h
#ifndef BUREAU_H
#define BUREAU_H
#include <list>
#include "Seller.h"
#include "Customer.h"
#include "Vehicle.h"
class Bureau
{
private:
std::list <Vehicle*> cars;
std::list <Seller*> sellers;
std::list <Customer*> customers;
Vehicle * car;
Seller * seller;
Customer * customer;
public:
Bureau();
~Bureau();
void menu();
void add_car();
void show_cars();
void buy_car();
void add_customer();
void show_customers();
void add_seller();
void show_sellers();
void delete_car();
void delete_customer();
void delete_seller();
void change_car_price();
};
#endif // BUREAU_H
Bureau.cpp
#include "Bureau.h"
#include <iostream>
Bureau::Bureau()
{
}
Bureau::~Bureau()
{
std::cout << "Zwolnienie pamieci i zakonczenie programu" << std::endl;
}
void Bureau::add_car()
{
std::string c_marque, c_model;
int c_year;
double c_price;
std::cout << "Podaj marke: ";
std::cin >> c_marque;
std::cout << "Podaj model: ";
std::cin >> c_model;
std::cout << "Podaj rok produkcji: ";
std::cin >> c_year;
std::cout << "Podaj cene: ";
std::cin >> c_price;
car = new Vehicle(c_marque, c_model, c_year, c_price);
cars.push_back(car);
//std::cout << "Przydzielono nr id: " << Vehicle::id_car << std::endl;
}
std::ostream &operator << (std::ostream &os, Vehicle &u) {
return os << u.to_string();
}
void Bureau::show_cars()
{
for (auto it = cars.begin(); it != cars.end(); it++) // użycie automatycznej dedukcji typów z C++11, zamiast: std::list <Vehicle*>::iterator it = cars.begin(); it != cars.end(); it++
{
std::cout << **it << std::endl;
}
}
void Bureau::buy_car()
{
int customer_id = 0, car_id = 0;
double money = 0;
std::cout << "Wybierz klienta po numerze id: ";
std::cin >> customer_id;
std::cout << "Wybierz samochod po nr id: ";
std::cin >> car_id;
for (auto it = customers.begin(); it != customers.end(); it++)
{
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price > money)
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
else
{
//bought_cars.splice (bought_cars.begin(), cars, it);
}
}
}
(*it)->balance = (*it)->balance - money;
}
}
}
void Bureau::add_customer()
{
std::string c_name, c_surname;
double c_balance;
std::cout << "Podaj imie: ";
std::cin >> c_name;
std::cout << "Podaj nazwisko: ";
std::cin >> c_surname;
std::cout << "Podaj stan konta: ";
std::cin >> c_balance;
customer = new Customer(c_name, c_surname, c_balance);
customers.push_back(customer);
}
std::ostream &operator << (std::ostream &os, Person &u) {
return os << u.to_string();
}
void Bureau::show_customers()
{
for (auto it = customers.begin(); it != customers.end(); it++)
{
std::cout << **it << std::endl;
}
}
void Bureau::add_seller()
{
std::string s_name, s_surname;
double s_balance;
std::cout << "Podaj imie: ";
std::cin >> s_name;
std::cout << "Podaj nazwisko: ";
std::cin >> s_surname;
std::cout << "Podaj stan konta: ";
std::cin >> s_balance;
seller = new Seller(s_name, s_surname, s_balance);
sellers.push_back(seller);
}
void Bureau::show_sellers()
{
for (auto it = sellers.begin(); it != sellers.end(); it++)
{
std::cout << **it << std::endl;
}
}
void Bureau::delete_car()
{
int car_id;
std::cout << "Podaj nr id samochodu do usunięcia: ";
std::cin >> car_id;
for (auto it = cars.begin(); it != cars.end(); it++)
{
if ((*it)->id_car == car_id)
{
cars.erase(it);
}
}
}
void Bureau::delete_customer()
{
int customer_id;
std::cout << "Podaj nr id klienta do usunięcia: ";
std::cin >> customer_id;
for (auto it = customers.begin(); it != customers.end(); it++)
{
if ((*it)->id_person == customer_id)
{
customers.erase(it);
}
}
}
void Bureau::delete_seller()
{
int seller_id;
std::cout << "Podaj nr id sprzedawcy do usunięcia: ";
std::cin >> seller_id;
for (auto it = sellers.begin(); it != sellers.end(); it++)
{
if ((*it)->id_person == seller_id)
{
sellers.erase(it);
}
}
}
void Bureau::change_car_price()
{
int car_id,car_price;
std::cout << "Podaj id pojazdu dla ktorego chcesz zmienic cene: ";
std::cin >> car_id;
for (auto it = cars.begin(); it != cars.end(); it++)
{
if ((*it)->id_car == car_id)
{
std::cout << "Aktualna cena pojazdu: " << (*it)->price << " Podaj nowa cene samochodu: ";
std::cin >> car_price;
(*it)->price = car_price;
std::cout << "Cena zostala zmieniona, oto nowe dane samochodu:\n";
std::cout << **it << std::endl;
}
}
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Person.h"
#include "Vehicle.h"
#include <list>
class Customer : public Person
{
private:
std::list <Vehicle*> bought_cars;
Vehicle * bought_car;
public:
Customer();
Customer(std::string, std::string, double);
virtual ~Customer();
std::string to_string() const;
friend class Bureau;
};
#endif // CUSTOMER_H
Customer.cpp
#include "Customer.h"
#include <iostream>
#include <sstream>
Customer::Customer()
{
}
Customer::Customer(std::string name, std::string surname, double balance) : Person(name, surname, balance)
{
std::cout << "Utworzono klienta i przypisano mu nr id: " << id_person << std::endl;
}
Customer::~Customer()
{
}
std::string Customer::to_string() const
{
std::stringstream ss;
std::string strings;
ss << name << " " << surname << " nr id to " << id_person << " stan konta " << balance;
strings = ss.str();
return strings;
}
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
class Person
{
private:
static int id_person_seed;
int id_person;
std::string name;
std::string surname;
double balance;
public:
Person();
Person(std::string, std::string, double);
~Person();
virtual std::string to_string() const = 0;
friend class Customer;
friend class Seller;
friend class Bureau;
};
#endif // PERSON_H
Person.cpp
#include "Person.h"
int Person::id_person_seed = 0;
Person::Person()
{
}
Person::Person(std::string name, std::string surname, double balance) : id_person(++id_person_seed)
{
this->id_person=id_person;
this->name=name;
this->surname=surname;
this->balance=balance;
}
Person::~Person()
{
}
Seller.h
#ifndef SELLER_H
#define SELLER_H
#include "Person.h"
class Seller : public Person
{
public:
Seller();
Seller(std::string, std::string, double);
~Seller();
std::string to_string() const;
};
#endif // SELLER_H
Seller.cpp
#include "Seller.h"
#include <iostream>
#include <sstream>
Seller::Seller()
{
}
Seller::Seller(std::string name, std::string surname, double balance) : Person(name, surname, balance)
{
std::cout << "Utworzono sprzedawce i przypisano mu nr id: " << id_person << std::endl;
}
Seller::~Seller()
{
}
std::string Seller::to_string() const
{
std::stringstream ss;
std::string strings;
ss << name << " " << surname << " nr id to " << id_person << " stan konta " << balance;
strings = ss.str();
return strings;
}
Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
class Vehicle
{
private:
static int id_car_seed;
int id_car;
int year;
std::string marque;
std::string model;
double price;
public:
Vehicle();
Vehicle(std::string, std::string, int, double);
~Vehicle();
std::string to_string();
friend class Bureau;
};
#endif // VEHICLE_H
Vehicle.cpp
#include "Vehicle.h"
#include <iostream>
#include <sstream>
int Vehicle::id_car_seed = 0;
Vehicle::Vehicle()
{
}
Vehicle::Vehicle(std::string marque, std::string model, int year, double price) : id_car(++id_car_seed)
{
this->id_car=id_car;
this->marque=marque;
this->model=model;
this->year=year;
this->price=price;
std::cout << "Utworzono pojazd i przypisano mu nr id: " << id_car << std::endl;
}
Vehicle::~Vehicle()
{
}
std::string Vehicle::to_string()
{
std::stringstream ss;
std::string strings;
ss << id_car << " " << marque << " " << model << " " << year << " " << price;
strings = ss.str();
return strings;
}
main
#include <iostream>
#include <memory>
#include "Bureau.h"
int main()
{
std::cout << "(2b || !(2b)) == question" << std::endl;
std::unique_ptr <Bureau> bureau(new Bureau()); // unique_ptr z C++11 i nie trzeba martwić się o zwolnienie pamięci
return 0;
}
- Rejestracja: dni
- Ostatnio: dni
@gośćabc przejrzałem twoje program i muszę przyznać, że jest lepiej zrealizowany. Jednak pozostanę przy swoim (nad którym się wiele namęczyłem). Czy użycie unique_ptr z listą też jest możliwe? Próbowałem ale się poddałem
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
jest, tylko musisz move'ować obiekty, jeżeli chcesz zmienić ownership;
ale to chyba jest trudne zagadnienie dla początkujących, używaj tego co masz
funkcja void Customer::buyCar(std::vector<Dealer*>& dealers, Car const& car) przedstawia ten problem
- Rejestracja: dni
- Ostatnio: dni
Masz rację. Wygląda mi to na trochę skomplikowane... A czy mógłbyś sprawdzić moją metodę buy_car ?
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
(*it)->balance = (*it)->balance - money;
zgarniasz mu kasę z konta nawet, jeżeli klienta nie stać na auto
i to jeszcze jak, zerujesz mu konto
bo money to jest (*it)-> ballance;
- Rejestracja: dni
- Ostatnio: dni
Zmieniłem... teraz powinno być lepiej
{
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price < money)
{
(*it)->balance = (*it)->balance - money;
//bought_cars.splice (bought_cars.begin(), cars, it);
}
else
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
}
}
}
}
Tylko nadal mam problem z dostępem do bought_cars
- Rejestracja: dni
- Ostatnio: dni
Zmieniłem... teraz powinno być lepiej
{
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price < money)
{
(*it)->balance = (*it)->balance - money;
//bought_cars.splice (bought_cars.begin(), cars, it);
}
else
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
}
}
}
}
Tylko nadal mam problem z dostępem do bought_cars
- Rejestracja: dni
- Ostatnio: dni
Zmieniłem... teraz powinno być lepiej
{
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price < money)
{
(*it)->balance = (*it)->balance - money;
//bought_cars.splice (bought_cars.begin(), cars, it);
}
else
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
}
}
}
}
Tylko nadal mam problem z dostępem do bought_cars
- Rejestracja: dni
- Ostatnio: dni
- Ten kod zdecydowanie cie przerasta, odłóż go i zajmij się podstawami
(*it)->bought_cars.splice((*it)->bought_cars.begin(), cars, cars.begin());
- Zamiast tej litanii
(*it)->utwórz metodę która dostaje przez parametrPerson &p
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
i dlaczego masz listę i wskaźnik osobno w Customer
private:
std::list <Vehicle*> bought_cars;
Vehicle * bought_car;
nie wiem jak to ma działać
to załatwia szukanie klienta, pozbywasz się pierwszego fora;
auto it = std::find_if(std::begin(customers), std::end(customers), [&customer_id](decltype(customers)::value_type const& current) -> bool {
return current->id_person == customer_id;
});
if(it != std::end(customers) {
// tutaj logika dla samochodów
}
edit:
szukasz samochodu analogicznie;
wyznaczasz iterator; wrzucasz wskaźnik do listy, którą trzyma customer, Twoje it (musisz dopisać funkcję składową Customer, która to uczyni); na końcu kasujesz wskaźnik z samochodów zwyczajnym erase;
naturalnie musisz potem na końcu zwolnić pamięć na liście Customera
- Rejestracja: dni
- Ostatnio: dni
Faktycznie straciłem panowanie nad programem... Brakuje mi jednak tej jednej metody i będę mógł zakończyć (mam nadzieję) ten semestr...
@_13th_Dragon kod który podałeś zmieniłem na końcu z cars.begin() na i... Iterator i jest ustawiony na odpowiedni obiekt w cars... Program się kompiluje jednak przy użyciu metody buy_car następuje crash.
@gośćabc w Customer jest lista żeby należała do tej klasy... Twój kod jest dla mnie ciężki do zrozumienia... Wątpię żebym sobie poradził z nim...
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price < money)
{
(*it)->balance = (*it)->balance - (*i)->price;
it->addBoughtCar(*i); // dopisz tę metodę do customer.
cars.erase(i);
return;
}
else
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
}
}
}
void Customer::addBoughtCar(Car* car)
{
boughtCars.push_back(car);
}
jakoś tak
- Rejestracja: dni
- Ostatnio: dni
Oto mój końcowy kod:
Bureau.h
#ifndef BUREAU_H
#define BUREAU_H
#include <list>
#include "Seller.h"
#include "Customer.h"
#include "Vehicle.h"
class Bureau
{
private:
std::list <Vehicle*> cars;
std::list <Seller*> sellers;
std::list <Customer*> customers;
Vehicle * car;
Seller * seller;
Customer * customer;
public:
Bureau();
~Bureau();
void menu();
void add_car();
void show_cars();
void buy_car();
void add_customer();
void show_customers();
void add_seller();
void show_sellers();
void delete_car();
void delete_customer();
void delete_seller();
void change_car_price();
};
#endif // BUREAU_H
Bureau.cpp
#include "Bureau.h"
#include <iostream>
Bureau::Bureau()
{
}
Bureau::~Bureau()
{
std::cout << "Zwolnienie pamieci i zakonczenie programu" << std::endl;
}
void Bureau::add_car()
{
std::string c_marque, c_model;
int c_year;
double c_price;
std::cout << "Podaj marke: ";
std::cin >> c_marque;
std::cout << "Podaj model: ";
std::cin >> c_model;
std::cout << "Podaj rok produkcji: ";
std::cin >> c_year;
std::cout << "Podaj cene: ";
std::cin >> c_price;
car = new Vehicle(c_marque, c_model, c_year, c_price);
cars.push_back(car);
//std::cout << "Przydzielono nr id: " << Vehicle::id_car << std::endl;
}
std::ostream &operator << (std::ostream &os, Vehicle &u) {
return os << u.to_string();
}
void Bureau::show_cars()
{
for (auto it = cars.begin(); it != cars.end(); it++) // użycie automatycznej dedukcji typów z C++11, zamiast: std::list <Vehicle*>::iterator it = cars.begin(); it != cars.end(); it++
{
std::cout << **it << std::endl;
}
}
void Bureau::buy_car()
{
int customer_id = 0, car_id = 0;
double money = 0;
std::cout << "Wybierz klienta po numerze id: ";
std::cin >> customer_id;
std::cout << "Wybierz samochod po nr id: ";
std::cin >> car_id;
for (auto it = customers.begin(); it != customers.end(); it++)
{
if ((*it)->id_person == customer_id)
{
money = (*it)->balance;
for (auto i = cars.begin(); i != cars.end(); i++)
{
if ((*i)->id_car == car_id)
{
if ((*i)->price < money)
{
(*it)->balance = (*it)->balance - (*i)->price;
(*it)->bought_cars.splice((*it)->bought_cars.begin(), cars, i);
//bought_cars.splice (bought_cars.begin(), cars, it);
}
else
{
std::cout << "Masz za malo pieniedzy!" << std::endl;
money = 0;
}
}
}
}
}
}
void Bureau::add_customer()
{
std::string c_name, c_surname;
double c_balance;
std::cout << "Podaj imie: ";
std::cin >> c_name;
std::cout << "Podaj nazwisko: ";
std::cin >> c_surname;
std::cout << "Podaj stan konta: ";
std::cin >> c_balance;
customer = new Customer(c_name, c_surname, c_balance);
customers.push_back(customer);
}
std::ostream &operator << (std::ostream &os, Person &u) {
return os << u.to_string();
}
void Bureau::show_customers()
{
for (auto it = customers.begin(); it != customers.end(); it++)
{
std::cout << **it << std::endl;
}
}
void Bureau::add_seller()
{
std::string s_name, s_surname;
double s_balance;
std::cout << "Podaj imie: ";
std::cin >> s_name;
std::cout << "Podaj nazwisko: ";
std::cin >> s_surname;
std::cout << "Podaj stan konta: ";
std::cin >> s_balance;
seller = new Seller(s_name, s_surname, s_balance);
sellers.push_back(seller);
}
void Bureau::show_sellers()
{
for (auto it = sellers.begin(); it != sellers.end(); it++)
{
std::cout << **it << std::endl;
}
}
void Bureau::delete_car()
{
int car_id;
std::cout << "Podaj nr id samochodu do usuniecia: ";
std::cin >> car_id;
for (auto it = cars.begin(); it != cars.end(); it++)
{
if ((*it)->id_car == car_id)
{
cars.erase(it);
}
}
}
void Bureau::delete_customer()
{
int customer_id;
std::cout << "Podaj nr id klienta do usuniecia: ";
std::cin >> customer_id;
for (auto it = customers.begin(); it != customers.end(); it++)
{
if ((*it)->id_person == customer_id)
{
customers.erase(it);
}
}
}
void Bureau::delete_seller()
{
int seller_id;
std::cout << "Podaj nr id sprzedawcy do usuniecia: ";
std::cin >> seller_id;
for (auto it = sellers.begin(); it != sellers.end(); it++)
{
if ((*it)->id_person == seller_id)
{
sellers.erase(it);
}
}
}
void Bureau::change_car_price()
{
int car_id,car_price;
std::cout << "Podaj id pojazdu dla ktorego chcesz zmienic cene: ";
std::cin >> car_id;
for (auto it = cars.begin(); it != cars.end(); it++)
{
if ((*it)->id_car == car_id)
{
std::cout << "Aktualna cena pojazdu: " << (*it)->price << " Podaj nowa cene samochodu: ";
std::cin >> car_price;
(*it)->price = car_price;
std::cout << "Cena zostala zmieniona, oto nowe dane samochodu:\n";
std::cout << **it << std::endl;
}
}
}
void Bureau::menu()
{
char repeat;
do {
std::cout << "Co chcesz zrobic? \n";
std::cout << "1.Dodac samochod \n"
<< "2.Dodaj klienta \n"
<< "3.Dodac sprzedawce \n"
<< "4.Wyswietlic samochody \n"
<< "5.Wyswietlic klientow \n"
<< "6.Wyswietlic sprzedawcow \n"
<< "7.Zmienic cene samochodu \n"
<< "8.Kupic samochod \n"
<< "9.Usunac samochod \n"
<< "10.Usunac klienta \n"
<< "11.Usunac sprzedawce " << std::endl;
int choice = 0;
again: std::cin >> choice;
switch (choice) {
case 1:
add_car();
break;
case 2:
add_customer();
break;
case 3:
add_seller();
break;
case 4:
show_cars();
break;
case 5:
show_customers();
break;
case 6:
show_sellers();
break;
case 7:
change_car_price();
break;
case 8:
buy_car();
break;
case 9:
delete_car();
break;
case 10:
delete_customer();
break;
case 11:
delete_seller();
break;
default:
std::cout << "Nic nie wybrałeś. Sprobuj ponownie" << std::endl;
goto again;
break;
}
repetition: std::cout << "Powtorzyc? Y/N: ";
std::cin >> repeat;
std::cout << std::endl;
if (repeat != 'y' && repeat != 'n') goto repetition;
}
while (repeat = 'y' && repeat != 'n');
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Person.h"
#include "Vehicle.h"
#include <list>
class Customer : public Person
{
private:
std::list <Vehicle*> bought_cars;
Vehicle * bought_car;
public:
Customer();
Customer(std::string, std::string, double);
virtual ~Customer();
std::string to_string() const;
friend class Bureau;
};
#endif // CUSTOMER_H
Customer.cpp
#include "Customer.h"
#include <iostream>
#include <sstream>
Customer::Customer()
{
}
Customer::Customer(std::string name, std::string surname, double balance) : Person(name, surname, balance)
{
std::cout << "Utworzono klienta i przypisano mu nr id: " << id_person << std::endl;
}
Customer::~Customer()
{
}
std::string Customer::to_string() const
{
std::stringstream ss;
std::string strings;
ss << name << " " << surname << " nr id to " << id_person << " stan konta " << balance;
strings = ss.str();
return strings;
}
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
class Person
{
private:
static int id_person_seed;
int id_person;
std::string name;
std::string surname;
double balance;
public:
Person();
Person(std::string, std::string, double);
~Person();
virtual std::string to_string() const = 0;
friend class Customer;
friend class Seller;
friend class Bureau;
};
#endif // PERSON_H
Person.cpp
#include "Person.h"
int Person::id_person_seed = 0;
Person::Person()
{
}
Person::Person(std::string name, std::string surname, double balance) : id_person(++id_person_seed)
{
this->id_person=id_person;
this->name=name;
this->surname=surname;
this->balance=balance;
}
Person::~Person()
{
}
Seller.h
#ifndef SELLER_H
#define SELLER_H
#include "Person.h"
class Seller : public Person
{
public:
Seller();
Seller(std::string, std::string, double);
~Seller();
std::string to_string() const;
};
#endif // SELLER_H
Seller.cpp
#include "Seller.h"
#include <iostream>
#include <sstream>
Seller::Seller()
{
}
Seller::Seller(std::string name, std::string surname, double balance) : Person(name, surname, balance)
{
std::cout << "Utworzono sprzedawce i przypisano mu nr id: " << id_person << std::endl;
}
Seller::~Seller()
{
}
std::string Seller::to_string() const
{
std::stringstream ss;
std::string strings;
ss << name << " " << surname << " nr id to " << id_person << " stan konta " << balance;
strings = ss.str();
return strings;
}
Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
class Vehicle
{
private:
static int id_car_seed;
int id_car;
int year;
std::string marque;
std::string model;
double price;
public:
Vehicle();
Vehicle(std::string, std::string, int, double);
~Vehicle();
std::string to_string();
friend class Bureau;
};
#endif // VEHICLE_H
Vehicle.cpp
#include "Vehicle.h"
#include <iostream>
#include <sstream>
int Vehicle::id_car_seed = 0;
Vehicle::Vehicle()
{
}
Vehicle::Vehicle(std::string marque, std::string model, int year, double price) : id_car(++id_car_seed)
{
this->id_car=id_car;
this->marque=marque;
this->model=model;
this->year=year;
this->price=price;
std::cout << "Utworzono pojazd i przypisano mu nr id: " << id_car << std::endl;
}
Vehicle::~Vehicle()
{
}
std::string Vehicle::to_string()
{
std::stringstream ss;
std::string strings;
ss << id_car << " " << marque << " " << model << " " << year << " " << price;
strings = ss.str();
return strings;
}
main
#include <iostream>
#include <memory>
#include "Bureau.h"
int main()
{
std::cout << "(2b || !(2b)) == question" << std::endl;
std::unique_ptr <Bureau> bureau(new Bureau()); // unique_ptr z C++11 i nie trzeba martwić się o zwolnienie pamięci
bureau->menu();
return 0;
}
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
to nie ma prawa się skompilować, dlaczego wklejasz taki kod
zobacz co napisałem w poście powyżej
edit:
aha ok widzę mnóstwo friend deklaracji, dawno czegoś takiego nie widziałem, ale ok jak Ci działa to super
odmeldowuję się
- Rejestracja: dni
- Ostatnio: dni
Nie zauważyłem twojego postu... Teraz poprawiłem zgodnie z twoim kodem. Zmieniłem tylko it->add... na (*it)->add i działa... Dzięki... A z tą przyjaźnią to nie mialem pomysłu co zrobić żeby jedna klasa miała dostęp do drugiej...
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
jeżeli to była Twoja pierwsza praca to i tak super, że działa, bo się pewnie napracowałeś pozdrawiam
- Rejestracja: dni
- Ostatnio: dni
Dzięki... Nie daje mi spokoju jednak Twoja uwaga na temat przyjaźni... Mógłbyś powiedzieć co jest nie tak z tym?
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Szczecin
- Postów: 500
na tym to polega, że klasa zaprzyjaźniona ma public dostęp do private elementów danej klasy
zamiast pisać funkcje składowe obsługujące logikę na danym obiekcie, dałeś sobie dostep z Bureau
przykladowe rozwiązanie to np. funckja, którą Ci poradziłem na końcu, zamiast bezpośrednio odwoływać się do boughtCars odwołujesz się do niego poprzez car->funkcja(parametry) i w niej rozwiązujesz swoje problemy
- Rejestracja: dni
- Ostatnio: dni
Aha... Czyli potrzeba dużo pracy żeby ogarnąć co ja zrobiłem... Masakra... A jeszcze zapytam czy np lista.clear() rozwiązuje sprawę pamięci?