Skip to content

Instantly share code, notes, and snippets.

@banan314
Created February 8, 2017 17:33
Show Gist options
  • Save banan314/95b48dabd91239db9614a85bdddec2d8 to your computer and use it in GitHub Desktop.
Save banan314/95b48dabd91239db9614a85bdddec2d8 to your computer and use it in GitHub Desktop.
shallow/deep copy
#include <iostream>
#include <string>
using namespace std;
class Pojazd {
private:
string lusterko[3] = {"lewe", "prawe"};
protected:
int id = 0;
public:
void setId(int id);
public:
int getId() const;
public:
friend class Samochod;
Pojazd& operator=(const Pojazd&);
};
Pojazd &Pojazd::operator=(const Pojazd& pojazd) {
return *this;
}
class Samochod : Pojazd {
public:
Samochod& operator=(const Samochod&);
};
int Pojazd::getId() const {
return id;
}
void Pojazd::setId(int id) {
Pojazd::id = id;
}
Samochod &Samochod::operator=(const Samochod &wzor) {
if (this != &wzor)
{
(Pojazd&)*this = wzor;
this->lusterko[2] = "wsteczne";
}
return *this;
}
int main() {
Pojazd p1;
p1.setId(2);
Pojazd p2 = p1, p3;
p3.setId(4);
p3 = p1;
cout << "p1: " << p1.getId() << endl;
cout << "p2: " << p2.getId() << endl;
cout << "p3: " << p3.getId() << endl;
return 0;
}
@banan314
Copy link
Author

banan314 commented Feb 8, 2017

out:
p1: 2
p2: 2
p3: 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment