从基本向量c ++中的对象调用派生方法

时间:2014-04-06 16:09:08

标签: c++ polymorphism overloading virtual

我对c ++中的多态性存有疑问。我有以下结构:

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 = 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 = a + 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 = a - 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 = a * 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 = a / b;
    return Complex(mult.getW(), mult.getX());
}

ostream& operator <<(ostream& os, const Complex& comp){

    return os << "c = " << comp.getW() << " + " << comp.getX() << "i";
}

Complex::~Complex()
{
}

和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()
{
    for (int i = 0; i<size; i++)
    {
        cout << qstore[i] << endl;
    }
}

QStore::~QStore()
{
    delete[] qstore;
}

现在真正感兴趣的是重载运算符&lt;&lt;。 我需要在向量中存储几个四元数和复合体 在QStore中并列出所有存储的对象。正如你所见,复杂 继承四元数然后在我只收到的QStore方法中添加 四元。发生的事情是当我插入一些对象并显示它们时, 只是四元数显示在屏幕上。我的问题是,如何做 区分他们?

非常感谢,对不起的英语感到抱歉。 Phsil

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。实际上我开始使用 vector ,但我发现如何在没有矢量的情况下解决它。我的新课程QStore是:

#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

问题是之前我创建了一个QStore向量,如下所示:

Quaternions *qstore;

我需要以这种形式创建:

 Quaternions **qstore;

因为在这种方法中:

void QStore::list()
{
for (int i = 0; i< count; i++)
{
    qstore[i]->toString();
    cout << endl;
}
}

我需要指向Quaternion one(Complex或Quaternion)的正确实例,然后我需要一个指针指针,以便能够访问存储在Quaternions矢量中的Complex实例的toString而不是所有的时间只是访问Quaternion的toString,这是我最初的问题。

感谢您帮助我。

:d