#include <iostream>
#include <string>
#include <fstream>
#include "osoba.h"
#include "bazadanych.h"
int Osoba::counter=0;
int BazaDanych::counter=0;
int main()
{
Osoba o1;
cout << "---- 1 ----" << endl;
cout << o1 << endl;
Osoba o2("Jan", "Kowalski", 11111111111);
cout << "---- 2 ----" << endl;
cout << o2 << endl;
cout << "---- 3 ----" << endl;
cout << boolalpha << (o1 == o2) << endl;
BazaDanych db1(5);
cout << "---- 4 ----" << endl;
cout << db1;
db1 = db1 + o2;
cout << "---- 5 ----" << endl;
cout << db1;
// ifstream plik_we("zadanie7.txt");
// if (!plik_we)
// {
// cout << "---- 6 ----" << endl;
// return 1;
// }
// while (plik_we >> o2)
// {
// db1 = db1 + o2;
// }
cout << "---- 7 ----" << endl;
cout << db1;
{
BazaDanych db2(db1);
cout << "---- 8 ----" << endl;
cout << db2;
}
cout << "---- 9 ----" << endl;
cout << db1;
BazaDanych db3;
db3 = db1 + db1;
cout << "---- 10 ----" << endl;
cout << db3;
BazaDanych db4;
db4 = db3 + db1;
cout << "---- 10 ----" << endl;
cout << db4;
/*
for(int i=0; i<db3.liczbaOsob(); ++i)
{
cout << db3[i] << endl;
}
/*
cout << "---- 11 ----" << endl;
db1 = db1 - o1;
cout << "---- 12 ----" << endl;
cout << db1;
cout << "---- 13 ----" << endl;
cout << db3;
cout << "---- 14 ----" << endl;
*/
;
cout<<string(40,'\\')+'\n'
<< Osoba::counter<<'\n'
<< BazaDanych::counter<<'\n'
<<string(40,'\\')<<endl;
return 0;
}
using namespace std;
class Osoba
{
private:
std::string imie_;
std::string nazwisko_;
long long int pesel_;
public:
static int counter;
// konstruktor parametryczny i konstruktor domyslny
Osoba (string imie = "Imie", string nazwisko = "Nazwisko", long long int pesel = 0):imie_ (imie), nazwisko_ (nazwisko),
pesel_
(pesel)
{
counter++;
cout << "konst counter=" << counter << endl;
}
~Osoba ()
{
counter--;
cout << "dest counter=" << counter << endl;
}
// operator <<
friend ostream & operator<< (ostream & os, const Osoba & o);
// operator >>
friend istream & operator>> (istream & os, Osoba & o);
// operator ==
friend bool operator== (const Osoba & a, const Osoba & b);
};
ostream & operator<< (ostream & os, const Osoba & o)
{
os << o.imie_ << " " << o.nazwisko_ << " " << o.pesel_ << endl;
return os;
}
istream & operator>> (istream & is, Osoba & o)
{
is >> o.imie_ >> o.nazwisko_ >> o.pesel_;
return is;
}
bool
operator== (const Osoba & a, const Osoba & b)
{
return (a.pesel_ == b.pesel_ && a.imie_ == b.imie_
&& a.nazwisko_ == b.nazwisko_);
}
class BazaDanych
{
private:
// tablica obiektow typu Osoba w pamieci dynamicznej
int liczbaOsob_ = 0;
// Osoba *osoby_ = new Osoba[liczbaOsob];
Osoba *osoby_ = nullptr;
public:
static int counter;
// konstruktor domyslny
BazaDanych (int liczbaOsob = 0):liczbaOsob_ (liczbaOsob)
{
if (liczbaOsob > 0)
osoby_ = new Osoba[liczbaOsob];
// liczbaOsob = 0;
{
counter++;
cout << "BD konst counter=" << counter << endl;
}
}
// konstruktor kopiujacy
BazaDanych (BazaDanych & b)
{
liczbaOsob_ = b.liczbaOsob_;
osoby_ = new Osoba[liczbaOsob_];
for (int i = 0; i < liczbaOsob_; ++i)
{
osoby_[i] = b.osoby_[i];
}
{
counter++;
cout << "BD konst counter=" << counter << endl;
}
}
// destruktor
~BazaDanych ()
{
// cout<<"osoby"<<osoby<<endl;
if (osoby_)
{
delete[]osoby_;
osoby_ = nullptr;
}
else
cout << "osoby" << osoby_ << "liczbaOsob_=" << liczbaOsob_ << endl;
{
counter--;
cout << "BD dest counter=" << counter << endl;
}
};
// operator <<
friend ostream & operator<< (ostream & os, const BazaDanych & b);
//operator przypisania kupujacego
BazaDanych & operator= (const BazaDanych & bb)
{
if (this != &bb)
{
delete[] osoby_;
liczbaOsob_ = bb.liczbaOsob_;
osoby_ = new Osoba[liczbaOsob_];
for (int i = 0; i < liczbaOsob_; ++i)
{
osoby_[i] = osoby_[i];
}
}
return *this;
}
// operator dodajacy osobe do bazy danych
BazaDanych &operator+(const Osoba &o)
{
Osoba *temp = new Osoba[liczbaOsob_ + 1];
for (int i = 0; i < liczbaOsob_; ++i)
{
temp[i] = osoby_[i];
}
temp[liczbaOsob_] = o;
delete[] osoby_;
osoby_ = temp;
liczbaOsob_++;
return *this;
}
//operator + dodajacy dwie bazy
friend BazaDanych operator+ (const BazaDanych & b1, const BazaDanych & b2);
friend ostream & operator<< (ostream & os, const BazaDanych & b);
};
ostream & operator<< (ostream & os, const BazaDanych & b)
{
for (int i = 0; i < b.liczbaOsob_; ++i)
{
os << b.osoby_[i];
}
return os;
}
BazaDanych
operator+ (const BazaDanych & b1, const BazaDanych & b2)
{
int liczbaOsob = b1.liczbaOsob_ + b2.liczbaOsob_;
int j = 0;
BazaDanych temp(liczbaOsob);
for (; j < b1.liczbaOsob_; j++)
{
temp.osoby_[j] = b1.osoby_[j];
}
for (int i = 0; i < b2.liczbaOsob_; j++, ++i)
{
temp.osoby_[j] = b1.osoby_[i];
}
return temp;
}