Created
February 8, 2017 17:33
-
-
Save camilajenny/95b48dabd91239db9614a85bdddec2d8 to your computer and use it in GitHub Desktop.
shallow/deep copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
out:
p1: 2
p2: 2
p3: 4