I have 3 classes that derive from one another - GameScreen is the base class to which MenuScreen is derived from. I then have a third class 'TitleScreen' which derives from 'MenuScreen'.
The flow is basically from the base class: 'GameScreen' -> 'MenuScreen' -> 'TitleScreen'
The base class 'GameScreen' has no parameters in it's constructor, like wise with 'TitleScreen', however I need a parameter for 'MenuScreen'. I currently have the header files as:
GameScreen.h
class GameScreen
{
public:
GameScreen();
}
MenuScreen.h
class MenuScreen : public GameScreen
{
public:
MenuScreen(std::string title);
}
TitleScreen.h
class TitleScreen : public MenuScreen
{
public:
TitleScreen(std::string title) : MenuScreen(title);
}
What I'm having difficulty trying to understand is if this is possible in C++ (I'm following a C# sample for Game State Management which does this). Reading through class inheritance in some books I have it only covers parameters inherited from the base class, were as my sample base class has no parameters.
You are missing ; after each class declaration.
If you write TitleScreen(std::string title) : MenuScreen(title) you are defining the body of the method but the body is missing... so you should put just declaration to your TitleScreen.h :
class TitleScreen : public MenuScreen
{
public:
TitleScreen(std::string title);
};
and then place the body of the constructor to TitleScreen.cpp:
#include "TitleScreen.h"
TitleScreen::TitleScreen(std::string title) : MenuScreen(title)
{
// ..
}
Edit: fixed the terminology accordint to this question.
Related
Scenario: I Want to use the Parent methods in child. Is it possible to create a solution with two dialog classes as shown below?
//Parent is created using class wizard(inherited from CDialog)
class CDlgParent : public CDialog
//Child class created using class wizard(inherited from CDialog) and then
//changed the inheritance
class CDlgChild : public CDlgParent
just to exemplify
class A
{
private:
void privateMethod(){}
protected:
void protectedMethod(){}
public:
void publicMethod(){}
};
class B : public A
{
void methodB()
{
//privateMethod();
protectedMethod();
publicMethod();
}
};
just copy this in your code and you will see that it will compile.
If you uncomment the line, it will not compile anymore, giving an error like:
cannot access private member declared in class 'A'
So the only methods that you cannot use from B, that inherits from A, are the private methods, all the others can just be used normally
I am not experienced in OOP. I am developing an application using C++ and Qt. I have implemented 2 classes, base one and the one that inherits from it. Then I have added virtual methods for both and everything worked. But then I realized that I don't think it should... Here is the example:
This is my base class :
namespace Ui {
class CGenericProject;
}
class CGenericProject : public QDialog
{
Q_OBJECT
public:
explicit CGenericProject(QWidget *parent = 0);
~CGenericProject();
EMeasures_t type();
private:
Ui::CGenericProject *ui;
virtual void initPlot();
protected:
QCustomPlot* customPlot;
QVector<double> m_x;
QVector<double> m_y;
EMeasures_t m_type;
};
It has a virtual method called initPlot and it looks like this:
void CGenericProject::initPlot()
{
customPlot = ui->workPlot;
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes );
customPlot->setFocusPolicy(Qt::ClickFocus);
customPlot->xAxis->setAutoTickStep(false);
customPlot->yAxis->setAutoTickStep(false);
customPlot->xAxis->setTickStep(100);
customPlot->yAxis->setTickStep(100);
customPlot->xAxis->setRange(0, 1000);
customPlot->yAxis->setRange(0, 1000);
}
And then i have a class that derives it:
class CEisProject : public CGenericProject
{
public:
CEisProject();
~CEisProject();
private:
virtual void initPlot();
void exampleEisMethod();
};
its initPlot is here:
void CEisProject::initPlot()
{
// give the axes some labels:
customPlot->xAxis->setLabel("Re [Ohm]");
customPlot->yAxis->setLabel("- Im [Ohm]");
customPlot->replot();
}
This is how i create the object:
CGenericProject* test = new CEisProject();
Now, when the initPlot() method is called, first the initPlot() from base class CGenericProject is called and then initPlot() from CEisProject is called. I wanted this functionality, where I can predefine some stuff in generic class and then add specific stuff in the childs.
But when I think of it, shouldn't initPlot() be calles exclusevily? I mean, shouldn't the method be called from base class or child class, instead of both, one after another? I have noticed this after reading this answer.
Constructors:
CGenericProject::CGenericProject(QWidget *parent) :
QDialog(parent),
ui(new Ui::CGenericProject)
{
ui->setupUi(this);
initPlot();
m_x.clear();
m_y.clear();
}
CEisProject::CEisProject()
{
m_type = EMeasures_t::eEIS;
initPlot();
}
You did not show the definition of the constructors, just their declaration. But I'm pretty sure the constructor definitions contain the answer to your question.
You may not be aware that the derived class constructor calls the base class constructor before directing virtual functions to the derived class. So a virtual function called in the base class construction (of an object which will soon be derived class) gets the base class definition of that virtual function.
Also, your constructor should be like:
// File .h
CEisProject(QWidget *parent = 0);
// File .cpp
CEisProject::CEisProject(QWidget *parent) : CGenericProject(parent)
{
...
}
or you won't be able to parent your derived widgets.
I have created a class in Qt that has a number of public properties lets say
class items
{
public:
QString name;
QString description;
How can I create a new class that inherits all the variables and methods from this class?
Your question is about c++ not Qt. You can search c++ documents to learn about c++ inheritance.
You can inherit another class in this way:
class myClass : public items
{
myClass() : Item()
{
//
}
}
I have the following project, in which I define a main class called frame, and from that class I derive multiple type of classes. The main class "frame" has a protected variable defined as:
class frame {
protected:
char header[4];
}
And in the derived classes I want the array header to have different size, as the following:
class dervied_frame : public frame {
protected:
char header[8];
}
So my question is it possible to override the protected variable in the derived classes? and how to do that?
Note: I don't want to define the header as a pointer and in then in the constructor I define the size that I want through dynamic allocation.
You could use a template like this:
template <int headerSize>
class frame {
protected:
char header[headerSize];
};
class dervied_frame : public frame<8> {
};
But then every sub-class will have a unique base class so you won't really be able to do anything with a frame *. Depending on what you are using this class for, that restriction could be a deal breaker. You can partially get around this by adding another super-class:
class frame {
public:
void otherMethodsHere();
};
template <int headerSize>
class frameHeader : public frame {
protected:
char header[headerSize];
};
class dervied_frame : public frameHeader<8> {
};
My compiler says:
error C2660: 'UberMaterial::Initialize' : function does not take 2 arguments
When I write this:
#include "BaseMaterial.h"
#include "UberMaterial.h"
UberMaterial* m_pGameLevelMaterial;
m_pGameLevelMaterial->Initialize(m_pContentManager, m_pLevel->GetDevice());
The base class:
class BaseMaterial
{
public:
BaseMaterial(tstring shaderFilename);
virtual ~BaseMaterial(){}
void Initialize(ContentManager *pContentManager, ID3D10Device *pD3DDevice);
//[More Code...]
protected:
virtual void Initialize(ContentManager *pContentManager) = 0;
//[More Code...]
};
The inherited class:
#include "BaseMaterial.h"
class UberMaterial:public BaseMaterial
{
//[More Code...]
protected:
virtual void Initialize(ContentManager *pContentManager);
//[More Code...]
};
Can anyone tell me what the problem is?
If you need more code, just comment and I'll post it. But the whole thing is rather large, so I didn't include it at this point.
Yes, by default, an overload in a derived class will hide a different overload from a base class. You can re-expose the base-class overload with using:
class UberMaterial : public BaseMaterial
{
...
public:
using BaseMaterial::Initialize;
virtual void Initialize(ContentManager *pContentManager);
};