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 &s) { size = s.getSize(); for(int i = 0; i < 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->clone(); for (int i = 0; i < size; i++) ptrTemp[i] = ptr[i]; delete [] ptr; size++; ptr = new Obj*[size]; for (int i = 0; i < size - 1; i++) ptr[i] = ptrTemp[i]; delete [] ptrTemp; } void del () { Obj** ptrTemp = new Obj*[size]; for (int i = 0; i < size - 1; i++) ptrTemp[i] = ptr[i]; delete [] ptr; size--; ptr = new Obj*[size]; for (int i = 0; i < size - 1; i++) ptr[i] = ptrTemp[i]; delete [] ptrTemp; } Obj* pop() { if (size > 1) { Obj *temp; temp = ptr[size - 2]; del(); return temp; } else { cout << "Стек был пуст.\n"; delete [] ptr; exit(EXIT_FAILURE); } } void show() { if (size > 1) { cout << "Печать стека: "; for (int i = size - 1; i >= 0; i--) cout << ptr[i] << " "; cout << "\n"; } else cout << "Стек пуст.\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<DoubleObj*>(ad); d.push(bd); d.show(); Stack s; Obj *as = new StringObj("aaa"); StringObj *bs = dynamic_cast<StringObj*>(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 << *p << std::endl; delete [] a; return 0;
}
Its conclusion to the console:
10
10
10
All you have left is to correctly implement your glass.