Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <list>
- #include <memory>
- class OneDim{
- public:
- int x;
- OneDim(int x):x(x){};
- OneDim operator +(OneDim &lhs);
- virtual void print(ostream &o)const;
- };
- OneDim OneDim::operator+(OneDim &r) {
- return OneDim(this->x+r.x);
- }
- void OneDim::print(ostream & o) const {
- o<<x;
- }
- ostream& operator<<(ostream &o,OneDim& x){
- x.print(o);
- return o;
- }
- class TwoDim:public OneDim{
- public:
- int y;
- TwoDim(int x,int y):OneDim(x),y(y){};
- TwoDim operator+(TwoDim &r);
- void print(ostream &o)const;
- };
- TwoDim TwoDim::operator+(TwoDim &r) {
- return TwoDim(this->x+r.x, this->y+r.y);
- }
- void TwoDim::print(ostream &o) const {
- OneDim::print(o);
- o<<" "<<y;
- }
- class ThreeDim:public TwoDim{
- public:
- int z;
- ThreeDim(int x,int y, int z):TwoDim(x,y),z(z){};
- ThreeDim operator+(ThreeDim &r);
- void print(ostream & o)const;
- };
- ThreeDim ThreeDim::operator+(ThreeDim &r) {
- return ThreeDim(this->x+r.x, this->y+r.y, this->z+r.z);
- }
- void ThreeDim::print(ostream &o) const {
- TwoDim::print(o);
- o<<" "<<z;
- }
- int main() {
- ThreeDim *p=new ThreeDim(1,2,3);
- list<unique_ptr<OneDim>> l;
- l.push_back(make_unique<OneDim>(5));
- l.push_back(make_unique<TwoDim>(6,3));
- l.push_back(make_unique<ThreeDim>(8,5,4));
- for(auto&x:l){
- cout<<*x;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement