polymorphism - Call a derived method from a object in a base vector c++ -
i have doubt polymorphism in c++. have following structure:
quaternions.h
#ifndef quaternions_h #define quaternions_h #include <math.h> #include <ostream> using namespace std; class quaternions { private: float z; float y; protected: float w; float x; public: quaternions(); quaternions(float w, float x, float y, float z); float module() const; quaternions conjugate(); quaternions operator +(const quaternions quat); quaternions operator -(const quaternions quat); quaternions operator *(const quaternions quat); quaternions operator /(const quaternions quat); friend ostream& operator <<(ostream& os, const quaternions& quat); float getx() const; float getw() const; void setx(float x); void setw(float w); float gety() const; float getz() const; void sety(float y); void setz(float z); ~quaternions(); }; #endif
quaternions.cpp
#include "quaternions.h" quaternions::quaternions(){ x = 0; y = 0; z = 0; w = 0; } quaternions::quaternions(float w, float x, float y, float z) : x(x), y(y), z(z), w(w){ } float quaternions::module() const { return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2) + pow(w, 2)); } quaternions quaternions::conjugate(){ quaternions conj; conj.setx(-x); conj.sety(-y); conj.setz(-z); conj.setw(w); return conj; } quaternions quaternions::operator +(const quaternions quat){ quaternions sum; sum.setx(x + quat.getx()); sum.sety(y + quat.gety()); sum.setz(z + quat.getz()); sum.setw(w + quat.getw()); return sum; } quaternions quaternions::operator -(const quaternions quat){ quaternions sub; sub.setx(x - quat.getx()); sub.sety(y - quat.gety()); sub.setz(z - quat.getz()); sub.setw(w - quat.getw()); return sub; } quaternions quaternions::operator *(const quaternions quat){ quaternions mult; mult.setx(w * quat.getx() + x * quat.getw() + y * quat.getx() - z * quat.gety()); mult.sety(w * quat.gety() - x * quat.getz() + y * quat.getw() + z * quat.getx()); mult.setz(w * quat.getz() + x * quat.gety() - y * quat.getx() + z * quat.getw()); mult.setw(w * quat.getw() - x * quat.getx() - y * quat.gety() - z * quat.getz()); return mult; } quaternions quaternions::operator /(const quaternions quat){ quaternions div; div.setx((w * quat.getw() + x * quat.getx() + y * quat.gety() + z * quat.getz()) / (pow(quat.getw(), 2) + pow(quat.getx(), 2) + pow(quat.gety(), 2) + pow(quat.getz(), 2))); div.sety((x * quat.getw() - w * quat.getx() - z * quat.gety() + y * quat.getz()) / (pow(quat.getw(), 2) + pow(quat.getx(), 2) + pow(quat.gety(), 2) + pow(quat.getz(), 2))); div.setz((y * quat.getw() + z * quat.getx() - w * quat.gety() - x * quat.getz()) / (pow(quat.getw(), 2) + pow(quat.getx(), 2) + pow(quat.gety(), 2) + pow(quat.getz(), 2))); div.setw((z * quat.getw() - y * quat.getx() - x * quat.gety() - w * quat.getz()) / (pow(quat.getw(), 2) + pow(quat.getx(), 2) + pow(quat.gety(), 2) + pow(quat.getz(), 2))); return div; } ostream& operator <<(ostream& os, const quaternions& quat){ return os << "q = " << quat.getx() << "i + " << quat.gety() << "j + " << quat.getz() << "k + " << quat.getw(); } float quaternions::getx() const{ return x; } float quaternions::gety() const{ return y; } float quaternions::getz() const{ return z; } float quaternions::getw() const{ return w; } void quaternions::setx(float x) { this->x = x; } void quaternions::sety(float y) { this->y = y; } void quaternions::setz(float z) { this->z = z; } void quaternions::setw(float w) { this->w = w; } quaternions::~quaternions() { }
complex.h
#ifndef complex_h #define complex_h #include "quaternions.h" class complex : public quaternions { public: complex(); complex(float x, float y); complex conjugate(); float module() const; complex operator +(const complex comp); complex operator -(const complex comp); complex operator *(const complex comp); complex operator /(const complex comp); friend ostream& operator <<(ostream& os, const complex& comp); ~complex(); }; #endif
complex.cpp
#include "complex.h" complex::complex() : quaternions(0.0, 0.0, 0.0, 0.0) { } complex::complex(float x, float y) : quaternions(x, y, 0.0, 0.0) { } complex complex::conjugate(){ quaternions a(getw(), getx(), 0.0, 0.0); = a.conjugate(); return complex(a.getw(), a.getx()); } float complex::module() const{ return quaternions(getw(), getx(), 0.0, 0.0).module(); } complex complex::operator +(const complex comp){ quaternions a(getw(), getx(), 0.0, 0.0); quaternions b(comp.getw(), comp.getx(), 0.0, 0.0); quaternions soma = + b; return complex(soma.getw(), soma.getx()); } complex complex::operator -(const complex comp){ quaternions a(getw(), getx(), 0.0, 0.0); quaternions b(comp.getw(), comp.getx(), 0.0, 0.0); quaternions sub = - b; return complex(sub.getw(), sub.getx()); } complex complex::operator *(const complex comp){ quaternions a(getw(), getx(), 0.0, 0.0); quaternions b(comp.getw(), comp.getx(), 0.0, 0.0); quaternions mult = * b; return complex(mult.getw(), mult.getx()); } complex complex::operator /(const complex comp){ quaternions a(getw(), getx(), 0.0, 0.0); quaternions b(comp.getw(), comp.getx(), 0.0, 0.0); quaternions mult = / b; return complex(mult.getw(), mult.getx()); } ostream& operator <<(ostream& os, const complex& comp){ return os << "c = " << comp.getw() << " + " << comp.getx() << "i"; } complex::~complex() { }
and qstore
#include "qstore.h" qstore::qstore() : size(0), count(0) { qstore = null; } qstore::qstore(int size) : size(size), count(0) { qstore = new quaternions[size]; } void qstore::add(quaternions *quat){ if (count < size){ qstore[count++] = *quat; } } void qstore::list() { (int = 0; i<size; i++) { cout << qstore[i] << endl; } } qstore::~qstore() { delete[] qstore; }
what interests overload operators <<. need store several quaternions , complexes inside of vector in qstore , after list objects stored. can see complex inherits quaternions , on method add in qstore receive quaternions. happening when insert objects , show them, quaternions showed on screen. question is, how doing distinguish them?
thank , sorry poor english. phsil
i solved problem. started using vector, found how solve without vectors. new class qstore is:
#ifndef qstore_h #define qstore_h #include "quaternions.h" #include <vector> class qstore { private: int count; std::vector<quaternions*> qstore; public: qstore(); void add(quaternions *quat); void list(); ~qstore(); }; #endif
the problem created vector of qstore follows:
quaternions *qstore;
and needed create in form:
quaternions **qstore;
because in method:
void qstore::list() { (int = 0; i< count; i++) { qstore[i]->tostring(); cout << endl; } }
i need point correct instance of quaternion 1 (complex or quaternion) , need pointer of pointer able access tostring of complex instance stored in vector of quaternions , not time access tostring of quaternion one, initial problem.
thank trying me.
:d
Comments
Post a Comment