Abractic class for copying the object and index for it



  • i.e. base class is abstract, the index shall be used to create the facility. void show() { cout << ptr[i] } removes the address of the element, not its meaning. In an attempt to screen *ptr[i] naturally there is a mistake:

    Invalid operands to binary expression ('ostream'(aka 'basic_ostream') and 'Obj')

    How can you lead the type?

    #include <iostream>
    #include <string>
    

    using namespace std;

    class Obj { public: virtual Obj* clone() = 0; };

    class IntObj : public Obj {
    int element;
    public:
    IntObj(){};
    IntObj (int temp) { element = temp; }
    Obj* clone() { return new IntObj(element); }
    };

    class DoubleObj : public Obj {
    double element;
    public:
    DoubleObj(){};
    DoubleObj (double temp) { element = temp; }
    Obj* clone() { return new DoubleObj(element); }
    };

    class StringObj : public Obj {
    string element;
    public:
    StringObj(){};
    StringObj (string temp) { element = temp; }
    Obj* clone() { return new StringObj(element); }
    };

    class Stack {
    Obj *ptr;
    int size = 1;
    public:
    Stack() { ptr = new Obj
    [size]; }

    Stack(const Stack &amp;s) {
        size = s.getSize();
        for(int i = 0; i &lt; size; i++)
            *ptr[i] = s.getPtr()[i];
    }
    
    ~Stack() { delete [] ptr; }
    
    Obj *getPtr() const { return *ptr; }
    int getSize() const { return size; }
    
    void push(Obj *obj) {
        Obj** ptrTemp = new Obj*[size];
    
        ptr[size - 1] = obj-&gt;clone();
    
        for (int i = 0; i &lt; size; i++)
            ptrTemp[i] = ptr[i];
    
        delete [] ptr;
    
        size++;
    
        ptr = new Obj*[size];
    
        for (int i = 0; i &lt; size - 1; i++)
            ptr[i] = ptrTemp[i];
    
        delete [] ptrTemp;
    }
    
    void del () {
        Obj** ptrTemp = new Obj*[size];
    
        for (int i = 0; i &lt; size - 1; i++)
            ptrTemp[i] = ptr[i];
    
        delete [] ptr;
    
        size--;
    
        ptr = new Obj*[size];
    
        for (int i = 0; i &lt; size - 1; i++)
            ptr[i] = ptrTemp[i];
    
        delete [] ptrTemp;
    }
    
    Obj* pop() {
        if (size &gt; 1) {
            Obj *temp;
            temp = ptr[size - 2];
            del();
            return temp;
        } else  {
            cout &lt;&lt; "Стек был пуст.\n";
            delete [] ptr;
            exit(EXIT_FAILURE);
        }
    }
    
    void show() {
        if (size &gt; 1) {
            cout &lt;&lt; "Печать стека: ";
    
            for (int i = size - 1; i &gt;= 0; i--)
                cout &lt;&lt; ptr[i] &lt;&lt; " ";
    
            cout &lt;&lt; "\n";
        } else
            cout &lt;&lt; "Стек пуст.\n";
    }
    

    };

    int main() {
    Stack i;
    Obj *ai = new IntObj(5);
    IntObj bi = dynamic_cast<IntObj>(ai);
    i.push(bi);
    i.show();

    Stack d;
    Obj *ad = new DoubleObj(5.0);
    DoubleObj *bd = dynamic_cast&lt;DoubleObj*&gt;(ad);
    d.push(bd);
    d.show();
    
    Stack s;
    Obj *as = new StringObj("aaa");
    StringObj *bs = dynamic_cast&lt;StringObj*&gt;(as);
    s.push(bs);
    s.show();
    
    return 0;
    

    }



  • You can declare virtual derivative functions as follows:

    IntObj* clone() { return new IntObj(element); }
    DoubleObj* clone() { return new DoubleObj(element); }
    StringObj* clone() { return new StringObj(element); }
    

    And then you won't have a problem if you still declare virtual the functions that you're going to call for each facility.

    Here's a demonstration program you can use as a model for your program.

    #include <iostream>
    #include <string>
    

    class Obj
    {
    public:
    virtual Obj* clone() const = 0;
    virtual std::ostream & out( std::ostream & ) const = 0;
    virtual ~Obj() {}
    };

    class IntObj : public Obj
    {
    int element;
    public:
    IntObj(){};
    IntObj (int temp) { element = temp; }
    IntObj* clone() const { return new IntObj(element); }
    std::ostream & out( std::ostream &os ) const
    {
    return os << element;
    }
    };

    class DoubleObj : public Obj {
    double element;
    public:
    DoubleObj(){};
    DoubleObj (double temp) { element = temp; }
    DoubleObj* clone() const { return new DoubleObj(element); }
    std::ostream & out( std::ostream &os ) const
    {
    return os << element;
    }
    };

    class StringObj : public Obj {
    std::string element;
    public:
    StringObj(){};
    StringObj ( const std::string &temp ) { element = temp; }
    StringObj* clone() const { return new StringObj(element); }
    std::ostream & out( std::ostream &os ) const
    {
    return os << element;
    }
    };

    std::ostream & operator <<( std::ostream &os, const Obj &obj )
    {
    return obj.out( os );
    }

    int main()
    {
    Obj * a[3];

    a[0] = IntObj( 10 ).clone();
    a[1] = DoubleObj( 10 ).clone();
    a[2] = StringObj( "10" ).clone();
    
    for ( Obj *p : a ) std::cout &lt;&lt; *p &lt;&lt; std::endl;
    
    delete [] a;
    
    return 0;
    

    }

    Its conclusion to the console:

    10
    10
    10

    All you have left is to correctly implement your glass.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2