I have two base classes, valuationFunction and SimulationEngine where I believe I have created a circular reference that I'm not sure how to sort out. Originally I only had SimulationEngine include ValuationFunction, but then I created the enum RiskFactor in SimulationEngine that I need ValuationFunction to recognize, and that's where I think my trouble stems from. Just looking at this it obviously seems wrong since the two header filers are including each other, but how else would I get the ValuationFunction (and all it's inherited classes) to be able to take an object of type RiskFactor as input?
SimulationEngine :
#pragma once
#define SIMULATION_ENGINE_H
#include "valuationFunction.h"
#include "Wrapper.h"
class SimulationEngine
{
public:
enum RiskFactor { interest_rate, equity, volatility, FX_rate };
SimulationEngine(double horizon, Wrapper<valuationFunction> theFunction_, RiskFactor simulatedRiskFactor);
virtual void DoOnePath(double vol, double normvariate) = 0;
virtual SimulationEngine* clone() const = 0;
const virtual double GetHorizon();
Wrapper<valuationFunction>& GetFunction();
RiskFactor simulatedRiskFactor;
protected:
double horizon;
Wrapper<valuationFunction> theFunction;
};
ValuationFunction:
#pragma once
#define VALUATION_FUNCTION_H
#include "SimulationEngine.h"
class valuationFunction
{
public:
valuationFunction(double TTM);
virtual void ValueInstrument() = 0;
virtual double GetValue() const;
virtual void RiskFactorAdd(double increment, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void UpdateTTM(double timeStep);
virtual valuationFunction* clone() const = 0;
virtual ~valuationFunction() {}
private:
protected:
double f;
double TTM;
};
Create RiskFactor.h. Stick the enum in there. Include it in both headers.
Related
I'm wondering if the following is possible. I wanted my derived class dA to change func() to be pure virtual so that any classes derived from dA must implement func().
Code similar to below compiles without complaint under MSVC even though ddA does not implement func().
The compiler does complain about the below code (see comments). So my question now becomes: is this a standards-compliant way to achieve what I want?
class A {
public:
virtual void func() { /* Some base implementation. */ }
}
class dA : public A {
public:
void func() override = 0; // Is this valid?
}
class ddA : public dA {
}
Pure virtual function should be declared in parent class. Consider the code below:
'''
class Shape {
public:
Shape(int init_x, int init_y) : x(init_x), y(init_y) {}
virtual void scale(int s) = 0;
virtual void print() = 0;
protected:
int x;
int y;
};
These functions should be implemented in children:
class Rect : public Shape {
public:
Rect(int init_x, int init_y, int w, int h);
virtual void scale(int s) { //implementation }
virtual void print() { //implementation }
private:
int width;
int height;
};
However you don't have to implement virtual functions in subclasses since it had been implemented in super class:
Pure virtual function make your class abstract so you can't use its instances.
You see I've been trying to create a std::vector which contains the Entity class inside the IState class. Both classes are interfaces.
The error is
'Entity' was not declared in this scope
and it points to :
protected:
std::vector <Entity*> ent_map;
inside IState.h
I've been trying for hours now to solve it. Once I made a forward declaration inside IState.h but once I did and tried to use the vector it spews out that it's an incomplete class, so I was back to square one.
Any ideas?
Entity.h
#ifdef __ENTITY__
#define __ENTITY__
#include <iostream>
#include <SDL.h>
class Entity
{
public:
virtual ~Entity();
virtual void load(const char* fileName, std::string id, SDL_Renderer* pRenderer) = 0;
virtual void draw() = 0;
virtual void update() = 0 ;
virtual void clean() = 0;
/*void int getX() { return m_x;}
void int getY() { return m_y;}
void std::string getTexID {return textureID;}
*/
};
#endif // __ENTITY__
IState.h
#ifndef IState_
#define IState_
#include "Entity.h"
#include <vector>
class IState
{
public :
virtual ~IState();
virtual void update() = 0;
virtual void render(SDL_Renderer* renderTarget) = 0;
virtual bool onEnter() = 0;
virtual bool onExit() = 0;
virtual void handleEvents(bool* gameLoop,SDL_Event event) = 0;
virtual void resume() = 0;
virtual std::string getStateID() = 0;
virtual void setStateID(std::string id) = 0;
protected:
std::vector <Entity*> ent_map;
};
#endif // IState_
The content of "Entity.h" won't be included at all.
Change
#ifdef __ENTITY__
to
#ifndef __ENTITY__
BTW: The name contains double underscore or begins with an underscore followed by an uppercase letter is reserved in C++, you need to be careful about it.
studying for a final and decided to build a program which makes use of pure virtual functions and polymorphism. i am stuck on a really weird error maybe i am missing something.
This is the Shape abstract class
#ifndef Shape_hpp
#define Shape_hpp
#include <stdio.h>
#include <string.h>
class Shape{
const char* name;
public:
Shape(const char* abc);
virtual double getPerimeter()=0;
virtual double getArea()=0;
};
#endif /* Shape_hpp */
The Shape .cpp implementation file
#include "Shape.hpp"
Shape::Shape(const char *shape){
name = shape;
}
The Circle Header file
#ifndef Circle_hpp
#define Circle_hpp
#include "Shape.hpp"
#include <stdio.h>
class Circle:public Shape{
double m_radius;
public:
Circle(double rad);
double getRadius();
};
#endif /* Circle_hpp */
The circle .cpp implementation file
#include "Circle.hpp"
#include "Shape.hpp"
Circle::Circle(double rad):Shape("Circle"){
m_radius = rad;
}
double Circle::getRadius(){
return m_radius;
}
double Circle::getPerimeter(){
return (2 * 3.14 * m_radius);
}
double getArea(){
return 0;
}
I declared the two pure virtual functions in the abstract "shape" class and am accessing the public of shape class in circle header file, if i declare the pure virtual functions in the circle class it will make it abstract... the error says "Out-of-line definition of 'getPerimeter' does not match any declaration in 'Circle'"
Am i missing something or am i thinking about this the wrong way..
Help would be appreciated. Thanks!
You need to declare all member functions that you define. So in class Circle you need to add:
virtual double getPerimeter();
Or better in C++11:
double getPerimeter() override;
You're defining Circle::getPerimeter() in your .cpp file but there is no member function getPerimeter() in the Circle class declaration. All pure virtual functions need to be overriden in a derived class in order for the class to become concrete. So yes, virtual double getPerimeter(); and override if you're using C++11.
Also, it's good practice to declare simple getters const.
It should be done this way.
class Shape{
const char* name;
public:
Shape(const char* abc);
virtual ~Shape() {} // you should have virtual destructor here
virtual double getPerimeter()=0;
virtual double getArea()=0;
};
class Circle:public Shape{
double m_radius;
public:
Circle(double rad);
double getRadius();
virtual double getPerimeter(); // we need to re-declare it here
virtual double getArea(); // we need to re-declare it here
};
Here's a suggestion. Since Shape is an abstract class, we cannot create objects of the class; so get rid of its constructor. Since we are interested in area and parameter of shapes, define the functions as virtual.
So, here is a redeclaration of Shape class.
#ifndef __SHAPE__
#define __SHAPE__
namespace shape
{
class Shape
{
public:
virtual float getArea()=0;
virtual float getPerimeter()=0;
};
}
#endif
Now, redeclaration of Circle class
#ifndef __CIRCLE__
#define __CIRCLE__
#include "inc/Shape.hpp"
namespace shape
{
class Circle: public Shape
{
float radius;
public:
Circle(float=0.0);
float getArea();
float getPerimeter();
};
}
#endif
Now redefining Circle class
#include "inc/Circle.hpp"
namespace shape
{
Circle::Circle(float radius)
{
this->radius = radius;
}
float Circle::getArea()
{
return ((22/7) * (this->radius * this->radius));
}
float Circle::getPerimeter()
{
return (2 * (22/7) * this->radius);
}
}
Now, in the main class
#include <iostream>
#include "inc/Circle.hpp"
int main()
{
shape::Shape *circle = new shape::Circle(2.5);
std::cout << "Area: " << circle->getArea() << std::endl;
std::cout << "Perimeter: " << circle->getPerimeter() << std::endl;
return 0;
}
You may redeclare the classes without namespaces.
The point to note is that the type of object created should be of the parent class and the object itself should be a child class.
One last thing; all pure virtual functions in the abstract must be redeclared and redefined (overridden) in the derived classes.
Need help with implementing a pure abstract class via inheritance, using namespace to wrap all my classes to avoid conflict with others.
I have been able to build and run the code successfully if I remove namespace wrapper from my abstract class and all classes that inherit from my pure abstract class.
It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.
Any help would be much appreciated.
//IBaseClass.h
//forward declaration
class ConcreteClass;
//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
virtual bool Method001( const ConcreteClass &cc ) = 0;
//virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ParentClass.h
//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
int a;
public:
virtual ~ParentClass() { /*virtual destructor*/ }
//getter-setter implemented in ParentClass.cpp file...
void setA(const int aa);
const int getA() const;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.h
//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass
{
private:
int b;
public:
virtual ~ConcreteClass() { /*virtual destructor*/ }
//getter-setter...
void setB(const int bb);
const int getB() const;
bool Method001( const ConcreteClass &cc ); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.cpp
//namespace MyCustomNamespace
//{
void ConcreteClass::setB(const int bb) { this->b = bb; }
const int ConcreteClass::getB() const { return this->b; }
bool ConcreteClass::Method001( const ConcreteClass &cc )
{
//implementation code goes here...
return false;
}
//} /*NAMESPACE*/
The problem is that your forward class has been declared in another namespace (specifically, the global namespace). Thus, the virtual to override is a different symbol with a different parameter type.
As written, the compiler matches ConcreteClass to the forward declaration it sees in the global namespace when declared in the base:
virtual bool Method001( const ConcreteClass &cc ) = 0;
When you declare Method001 in ConcreteClass:
virtual bool Method001( const ConcreteClass &cc );
the compiler matches cc to MyCustomNamespace::ConcreteClass because that is a more accurate match.
To resolve the issue, just place your forward declaration in the proper namespace:
namespace MyCustomNamespace {
class ConcreteClass;
}
I want to expose only the CreateSort() for the client. it was to create an object for the implementation of the sort class i.e imSort then return it to the client.but the compiler says that it cannot create an object of an abstract class eventhough all the functions have been defined in the derived class.
/////sort.h
class __declspec(dllexport) Sort {
public:
virtual int* BSort() const=0;
virtual void getdata() const=0;
};
extern "C" Sort *CreateSort();
/////imSort.h
#include "Sort.h"
class imSort : public Sort{
private:
int i,j,num;
int temp;
int *a;
public:
imSort();
int* BSort();
void getdata();
}
/////imSort.cpp
#include <iostream>
#include "imSort.h"
Sort *CreateSort()
{
return new imSort(); /* object of abstract class type "imSort" is not allowed: */
}
imSort::imSort()
{
i=j=num=0;
*a=0;
}
void imSort::getdata()
{
std::cout<<"\nEnter the number of elements..";
std::cin>>num;
for(i=0;i<num;i++)
{
std::cin>>*a;
*(a++);
}
}
int* imSort::BSort()
{
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
{
if(*(a+i)<*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
return a;
}
Your base class has:
virtual int* BSort() const=0;
virtual void getdata() const=0;
But your derived class has:
int* BSort();
void getdata();
Repeating the virtual keyword is optional, but without the const these are separate functions, unrelated to the virtual base functions.
As a result, those pure virtual functions remain un-overridden in the derived class, and so imSort (silly name for a type if you ask me) is still abstract.
Your fixed derived class definition is thus:
class imSort : public Sort {
private:
int i, j, num;
int temp;
int* a;
public:
imSort();
int* BSort() const; // <--- const
void getdata() const; // <--- const
}; // <--- ;
(Notice how indentation improves the legibility of your code? And you forgot the ; at the end of your class definition.)
Please write a fully-formed question next time, and reduce your problem to a minimal testcase.
If the virtual functions in the abstract Sort class are declared const, so should the implementations in the imSort class, but they are not.
So just add const here and there...