jweinst1
8/16/2017 - 5:39 PM

example using operators and chain methods in C++

example using operators and chain methods in C++

#include <iostream>

//null pointer example

//base class
class Base
{
  private:
  int type;
  Base* next;
  Base* prev;
  public:
  ~Base()
  {
    delete next;
    delete prev;
  }
  int getType()
  {
    return type;
  }
  
  void setType(int t)
  {
    type = t;
  }
  //gets the next element
  Base* getNext()
  {
    return next;
  }
  
  Base* setNext(Base* b)
  {
    next = b;
    b->prev = this;
    return this;
  }
  
  //is null method
  bool nextIsNull()
  {
    return next == NULL;
  }
  
  void conNext(Base* b)
  {
    next = b; b->prev = this;
  }
};

class IntObj: public Base
{
  public:
  long value;
  //constructor with init list
  IntObj(long val): value(val)
  {
    setType(3);
  }
  
  long getValue()
  {
    return value;
  }
  
  void setValue(long value)
  {
    value = value;
  }
};

Base operator+(Base left, Base right)
{
  left.conNext(&right);
  return left;
}


int main() {
  //chaining call
  Base* b = new IntObj(8);
  Base* c = new IntObj(6);
  b->setNext(c)->setNext(new IntObj(4));
}