Parent-Child implementation of CDialog classes - c++

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

Related

Virtual method misconception in C++

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.

access protected variable - complicated situation with inheritance and sub-classes

Hmm... I'm trying to break down my problem...
There is a library with some classes that do almost what I want. I can't change classes of the library so I want to derive them and change what I need.
In this case there is a derived class in the library with two subclasses. Now I derive the class and the subclasses.
In the second sub-class there is a virtual method witch modifies a protected variable from the first sub-class.
I want to override the virtual method with a new virtual method which calls the old virtual wethod an then modify the protected variable again.
Why am I getting the error in mySubClass2 while accessing fResponse?
How can I solve my problem?
class libraryClass : pulic someLibraryBaseClass {
protected:
libraryClass::librarySubClass2 lookUpFunction(int ID) {
//some magic to find the obj
return obj;
}
public:
class librarySubClass2;
class librarySubClass1 {
public:
librarySubClass1(libraryClass baseObj) {
myBaseObj = baseObj;
}
void someCallingFunction(int ID) {
libraryClass::librarySubClass2 obj = myBaseObj->lookUpFunction(ID)
obj->someHandleFunction(this)
cout << fResponse;
}
protected:
friend class librarySubClass2;
unsigned char fResponse[200];
private:
libraryClass myBaseObj;
};
class librarySubClass2 {
protected:
virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
snprintf((char*)obj->fResponse, sizeof obj->fResponse, "Some Text...\r\n"
}
};
};
class myDerivedClass : public libraryClass {
public:
class mySubClass2 : public libraryClass::librarySubClass2;
class mySubClass1 : public libraryClass::librarySubClass1 {
protected:
friend class mySubClass2;
};
class mySubClass2 : public libraryClass::librarySubClass2 {
protected:
virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
libraryClass:librarySubClass2::someHandleFuntion(obj);
snprintf((char*)obj->fResponse, sizeof obj->fResponse, "Add some more Text...\r\n"
}
};
};
Edit: Forgot * in Method of mySubClass2
Possible solution:
class mySubClass2 : public libraryClass::librarySubClass2 {
protected:
virtual void someHandleFunction(libraryClass::librarySubClass1* obj) {
libraryClass:librarySubClass2::someHandleFuntion(obj);
myDerivedClass::mySubClass1* nowMyObj = (myDerivedClass::mySubClass*) obj;
snprintf((char*)nowMyObj->fResponse, sizeof nowMyObj->fResponse, "Add some more Text...\r\n"
}
};
Now I derive the class and the subclasses.
In your example code, you're only deriving the main class and not the subclass. You have to inherit also the subclass:
class libraryClass : pulic someLibraryBaseClass
{
class librarySubClass1 : public someLibraryBaseClass::someLibrarySubClass1 { };
// ....
};
But that can be done only if the subclass is accessible (protected/public).
As far as I can tell you wonder why you can't access obj->fResponse in
void mySubClass2::someHandleFunction(libraryClass::librarySubClass1 obj) { ... }
Well, obj is of type librarySubClass1 which inherits its share of fResponse from the common ancestor. However, that is the share of a relative of mySubClass2, not yours as you are mySubClass2! You can only access the fResponse member of objects which are known to be of type mySubClass which actually happens to be known to be not the case for a librarySubClass1 object.
Getting access to librarySubClass::fResponse is as if you got free access to your uncle's inheritance from your grandparents. Unless you have a very unusual family sharing its wealth freely among all family members, you probably won't have access to your uncle's inheritance either.
Because fResponse in mySubClass2 is treated as protected and at that point it is outside of libraryClass, it only worked on librarySubClass2 because it is inside libraryClass.

Multi-level class inheritance with inconsistent constructors

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.

WinRT inheritance and common code

I want to extract common code from a few WinRT components to one base class so I don't need to copy&past it. I have the following base class:
[Windows::Foundation::Metadata::WebHostHidden]
ref class ExpandableView : public Windows::UI::Xaml::DependencyObject
{
public:
static void onIsExpandedChanged(Windows::UI::Xaml::DependencyObject^ object,
Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ arguments);
public:
property bool IsExpanded
{
bool get(){return (bool)GetValue(IsExpandedProperty);}
void set(bool value){SetValue(IsExpandedProperty, value);}
}
static property Windows::UI::Xaml::DependencyProperty^ IsExpandedProperty
{
Windows::UI::Xaml::DependencyProperty^ get(){return _IsExpandedProperty;}
}
protected:
ExpandableView();
virtual void viewExpanded();
virtual void viewCollapsed();
private:
void _expand();
void _collapse();
private:
static Windows::UI::Xaml::DependencyProperty^ _IsExpandedProperty;
};
And I create a few User Controls which should be somehow inherited from this base class. And it is not possible to do it the way I want because winrt class can inherit only one ref class and other should be interfaces. But I need this very class which has dependency property which has some logic when it is set and I don't want to copy&past this property across all my classes.
So the question is: how to achieve it with WinRT?
Have you tried using a template and inheritance of the specific class needed:
template<typename BaseClass>
ref class ExpandableView : public BaseClass;
Now the subclasses reusing ExpandableView can inherit whatever they need, not only Windows::UI::Xaml::DependencyObject.

public inherited class not able to access overloaded non-virtual public method of base class?

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);
};