Nightmare1
@Nightmare1
Программист

Как правильно применить абстрактный класc здесь?

Vector2.h

#ifndef __VECTOR2_H_
#define __VECTOR2_H_

#pragma once

#include "../BaseGUISegmentClass.hpp"

#define DVector(name) Vector ## name ## D 

#include "../../Interfaces/IVector.hpp"

#include <cmath>


template<typename T>
class DVector(2) : public IVector<DVector(2)<T>, T>, public BaseGUISegmentClass
{
/**
  * @brief Класс вектора с двумя членами.
  * 
  */

    ParameterGetSetTyped(T, x);
    ParameterGetSetTyped(T, y);

public:
   ~DVector(2)();

    /**
     * @brief Construct a new Vector 2 object
     * 
     * @param x Параметр Y дублируется из параметра Х.
     */
    DVector(2)(T x);
    
    /**
     * @brief Construct a new Vector 2 object
     * 
     * @param x Значение параметра Х
     * @param y Значение параметра У
     */
    DVector(2)(T x, T y);


public:
    virtual float length();
    virtual DVector(2) add_by_vector(const DVector(2)&) const;
    virtual DVector(2) add_by_number(T);
    virtual DVector(2) sub_by_number(T);
    virtual DVector(2) sub_by_vector(const DVector(2)&);
    virtual DVector(2) multiple_by_number(T);
    virtual DVector(2) divide_by_number(T);
    virtual DVector(2) multiple_by_vector(const DVector(2)&);
    virtual DVector(2) divide_by_vector(const DVector(2)&);
    virtual float calculate_angle(const DVector(2) &);
    virtual float calculate_projection(const DVector(2)&);
    virtual bool equals_to(const DVector(2)&);
    virtual float distance_to(const DVector(2)&);
    virtual T scalar_multiply(const DVector(2)&);
};


#endif // __VECTOR2_H_


Vector2
Vector2.cpp
#include "Vector2.h"

template<typename T> DVector(2)<T>::~DVector(2)(){}

template<typename T> DVector(2)<T>::DVector(2)(T x) :DVector(2)(x, x){}

template<typename T> DVector(2)<T>::DVector(2)(T x, T y)
{
    set_x(x);
    set_y(y);
}


#include <cmath>

template<typename T> float DVector(2)<T>::length()
{
    return sqrt(this->x()^2 + this->y()^2);
}

template<typename T>
DVector(2)<T> DVector(2)<T>::add_by_vector(const DVector(2)<T>& vector2d) const
{
    return DVector(2)<T>(this->get_x() + vector2d.get_x(), this->get_y() + vector2d.get_y());
}

template<typename T>
DVector(2)<T> DVector(2)<T>::sub_by_vector(const DVector(2)<T>& vector2d)
{
    return DVector(2)<T>(this->x() - vector2d.x(), this->y() - vector2d.y());
}

template<typename T>
DVector(2)<T> DVector(2)<T>::add_by_number(T value)
{
    return DVector(2)<T>(this->x() + value, this->y() + value);
}

template<typename T> DVector(2)<T> DVector(2)<T>::sub_by_number(T value)
{
    return DVector(2)<T>(this->x() - value, this->y() - value);
}

template<typename T>
DVector(2)<T> DVector(2)<T>::multiple_by_number(T value)
{
    return DVector(2)<T>(this->x() * value, this->y() * value);
}

template<typename T>
DVector(2)<T> DVector(2)<T>::divide_by_number(T value)
{
    return DVector(2)<T>(this->x() / value, this->y() / value);
}

template<typename T> 
DVector(2)<T> DVector(2)<T>::multiple_by_vector(const DVector(2)<T>& vector2d)
{
    return DVector(2)<T>(this->x() * vector2d.x(), this->y() * vector2d.y());
}

template<typename T> 
DVector(2)<T> DVector(2)<T>::divide_by_vector(const DVector(2)<T>& vector2d)
{
    return DVector(2)<T>(this->x() / vector2d.x(), this->y() / vector2d.y());
}

template<typename T> 
float DVector(2)<T>::calculate_angle(const DVector(2)<T> & vector2d)
{
    return scalar_multiply(vector2d) / (this->modulus() * vector2d.modulus());
}

template<typename T> 
float DVector(2)<T>::calculate_projection(const DVector(2)<T>& vector2d)
{
    return scalar_multiply(vector2d) / vector2d.modulus();
}

template<typename T> 
bool DVector(2)<T>::equals_to(const DVector(2)<T>& vector2d)
{
    return this->x() == vector2d.x() && this->y() == vector2d.y();
}

template<typename T> 
float DVector(2)<T>::distance_to(const DVector(2)<T>& vector2d)
{
    return this->sub_by_vector(vector2d).length();
}

template<typename T> 
T DVector(2)<T>::scalar_multiply(const DVector(2)<T>& vector2d)
{
    return this->x() * vector2d.x() + this->y() * vector2d.y();
}


template class DVector(2)<int>;
// template class DVector(2)<double>;
// template class DVector(2)<float>;
// template class DVector(2)<char>;
// template class DVector(2)<short>;
// template class DVector(2)<long long>;
//template class DVector(2)<unsigned long long>;


IVector
IVector
#ifndef _IVECTOR_HPP__
#define _IVECTOR_HPP__

template<class C, typename T> class IVector {
    /**
     * @brief Интерфейс класса вектора.
     * Содержит в себе необходимые определения для реализации Вектора.
     */

public:

    /**
     * @brief Рассчёт и возврат длины вектора.
     * 
     * @return unsigned long long 
     */
    virtual float length() = 0;

    /**
     * @brief Возвратить экземпляр класса, результат суммы двух векторов.
     * 
     * @param b 
     * @return C 
     */
    virtual C add_by_vector(const C&) = 0;

    /**
     * @brief Добавить значение в вектор. (сложение вектора и числа)
     * 
     * @param value 
     * @return C 
     */
    virtual C add_by_number(T) = 0;

    /**
     * @brief Умножение вектора на число.
     * 
     * @param value Число.
     * @return C
     */
    virtual C multiple_by_number(T) = 0;

    /**
     * @brief Деление вектора на число.
     * 
     * @param value 
     * @return C 
     */
    virtual C divide_by_number(T) = 0;

    /**
     * @brief Умножение на вектор.
     * 
     * @param value 
     * @return C 
     */
    virtual C multiple_by_vector( const C&) = 0;

    /**
     * @brief Деление на вектор.
     * 
     * @return C 
     */
    virtual C divide_by_vector(const C&) = 0;


    /**
     * @brief Получить угол между векторами.
     * 
     * @return float 
     */
    virtual float calculate_angle(const C&) = 0;


    /**
     * @brief Возвращает результат расчёта проекции на вектор.
     * 
     * @return float 
     */
    virtual float calculate_projection(const C&) = 0;

    /**
     * @brief Модуль вектора. (длинна)
     * 
     * @return float 
     */
    virtual float modulus(){return length();}


    /**
     * @brief Равенство векторов. Равно если координаты равны.
     * 
     * @return true 
     * @return false 
     */
    virtual bool equals_to( C&) = 0;

    /**
     * @brief Возвращает дистанцию до другого вектора в единицах.
     * 
     * @return float 
     */
    virtual float distance_to( C&) = 0;


    /**
     * @brief Скалярное произведение.s
     * 
     * @return T 
     */
    virtual T scalar_multiply( C&) = 0;

    /**
     * @brief Вычитание вектора по числу.
     * 
     * @param value 
     * @return C 
     */
    virtual C sub_by_number(T value) = 0;

    /**
     * @brief Вычитание по вектору.
     * 
     * @return C 
     */
    virtual C sub_by_vector( C&) = 0;
};

#endif //_IVECTOR_HPP__
  • Вопрос задан
  • 143 просмотра
Пригласить эксперта
Ответы на вопрос 1
wataru
@wataru Куратор тега C++
Разработчик на С++, экс-олимпиадник.
Пометьте все методы DVector(2) в Vector2.h override. Посмотрите на ошибки компилятора.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы