Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #include <list>
  4. #include <memory>
  5. class OneDim{
  6. public:
  7.     int x;
  8.     OneDim(int x):x(x){};
  9.     OneDim operator +(OneDim &lhs);
  10.     virtual void print(ostream &o)const;
  11. };
  12.  
  13. OneDim OneDim::operator+(OneDim &r) {
  14.     return OneDim(this->x+r.x);
  15. }
  16.  
  17. void OneDim::print(ostream & o) const {
  18.     o<<x;
  19. }
  20. ostream& operator<<(ostream &o,OneDim& x){
  21.     x.print(o);
  22.     return o;
  23. }
  24. class TwoDim:public OneDim{
  25. public:
  26.     int y;
  27.     TwoDim(int x,int y):OneDim(x),y(y){};
  28.     TwoDim operator+(TwoDim &r);
  29.     void print(ostream &o)const;
  30. };
  31.  
  32. TwoDim TwoDim::operator+(TwoDim &r) {
  33.     return TwoDim(this->x+r.x, this->y+r.y);
  34. }
  35.  
  36. void TwoDim::print(ostream &o) const {
  37.     OneDim::print(o);
  38.     o<<" "<<y;
  39. }
  40.  
  41. class ThreeDim:public TwoDim{
  42. public:
  43.     int z;
  44.     ThreeDim(int x,int y, int z):TwoDim(x,y),z(z){};
  45.     ThreeDim operator+(ThreeDim &r);
  46.     void print(ostream & o)const;
  47. };
  48.  
  49. ThreeDim ThreeDim::operator+(ThreeDim &r) {
  50.     return ThreeDim(this->x+r.x, this->y+r.y, this->z+r.z);
  51. }
  52.  
  53. void ThreeDim::print(ostream &o) const {
  54.     TwoDim::print(o);
  55.     o<<" "<<z;
  56. }
  57.  
  58. int main() {
  59.     ThreeDim *p=new ThreeDim(1,2,3);
  60.     list<unique_ptr<OneDim>> l;
  61.     l.push_back(make_unique<OneDim>(5));
  62.     l.push_back(make_unique<TwoDim>(6,3));
  63.     l.push_back(make_unique<ThreeDim>(8,5,4));
  64.     for(auto&x:l){
  65.         cout<<*x;
  66.     }
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement