Implementing pure virtual method in subclass - c++

I am implementing a pure virtual function from a parent class in a subclass.
When I try to instantiate the subclass in eclipse it says
The type 'derived' must implement the inherited pure virtual method 'Base::compareTo'
I am pretty sure I did so. My base class is..
base.h
#ifndef BASE_H_
#define BASE
class Base{
public:
Base();
virtual ~Base();
virtual int compareTo(void* compare)=0;
};
#endif /* BASE*/
Then my derived.h
#ifndef DERIVED_H_
#define DERIVED_H_
#include "Base.h"
class Derived : public Base {
public:
int x;
Derived(int y);
virtual ~Derived();
int compareTo(void* compare);
};
#endif /* DERIVED_H_ */
Derived.cpp
#include "Derived.h"
#include "Base.h"
Derived::Derived(int y) {
// TODO Auto-generated constructor stub
x=y;
}
Derived::~Derived() {
// TODO Auto-generated destructor stub
}
int Derived::compareTo(void* compare) {
Derived* compared;
int result=0;
if(compared=dynamic_cast<Derived*>(compare))
{
if(x<compared->x)
{
result=-1;
}
else
{
result=1;
}
}
return result;
}

I'm assuming this message is from eclipses code analyzer and not from your compiler. The code analyzer is wrong and you are correct. You have correctly implemented the pure virtual method from the base class in Derived. If you try to instantiate Derived, the code should compile.
Might your CDT version be less than 8.2.1? If so, you may be encountering this bug which should be fixed in 8.2.1.
There is another bug in your code though. You can't dynamic_cast a void pointer.

try this code of Base class:
#ifndef BASE_H_
#define BASE_H_
class Base{
public:
//Base (); not needed in the virtual class
virtual ~Base() {};
virtual int compareTo(void* compare)=0;
};
#endif /* BASE*/
#ifndef DERIVED_H_
#define DERIVED_H_
#include "Base.h"
class Derived : public Base {
public:
int x;
Derived(int y);
virtual ~Derived();
int compareTo(void* compare) override/*C++11*/;
};
#endif /* DERIVED_H_ */
#include "Derived.h"
//#include "Base.h" Not needed
Derived::Derived(int y) {
// TODO Auto-generated constructor stub
x=y;
}
Derived::~Derived() {
// TODO Auto-generated destructor stub
}
int Derived::compareTo(void* compare) {
Derived* compared;
int result=0;
if(compared=dynamic_cast<Derived*>(compare))
{
if(x<compared->x)
{
result=-1;
}
else
{
result=1;
}
}
return result;
}

Related

How to seperate definition and implementation of a derived class constructor?

I would like to learn how to define a derived class constructor in one file so that I could implement it in another file.
public:
Derived(std::string name) : Base(name);
~Derived();
Destructor works as expected, however with constructor I either add {} at the end (instead of a semicolon) and then get redefinition of 'Derived' error or I get asked to add {} instead of a semicolon. What is a way to separate definition and implementation in this case?
Base.h
#include <string>
class Base
{
protected:
std::string name;
...
public:
Base(std::string name);
virtual ~Derived();
...
};
Base.cpp
#include "Base.h"
Base::Base(std::string name)
: name(name)
{
...
}
Base::~Base()
{
...
}
Derived.h
#include "Base.h"
class Derived : public Base {
...
public:
Derived(std::string name);
~Derived();
...
};
Derived.cpp
#include "Derived.h"
Derived::Derived(std::string name)
: Base(name)
{
...
}
Derived::~Derived()
{
...
}
You do it the same way as for any other member function of the class. For example,
base.h
#pragma once
#include <string>
class Base
{
std::string name;
public:
Base() = default;
Base(std::string pname);//declaration
//other members
};
base.cpp
#include "base.h"
#include <iostream>
//definition
Base::Base(std::string pname): name(pname)
{
std::cout<<"Base constructor ran"<<std::endl;
}
derived.h
#pragma once
#include "base.h"
class Derived : public Base
{
public:
Derived(std::string pname);//declaration
};
derived.cpp
#include "derived.h"
#include <iostream>
//definition
Derived::Derived(std::string pname): Base(pname)
{
std::cout<<"Derived constructor ran"<<std::endl;
}
main.cpp
#include <iostream>
#include "derived.h"
#include "base.h"
int main()
{
Derived d("anoop");
return 0;
}
You can separate declaration and definition like so:
class Derived : public Base {
public:
Derived(std::string name); // declaration
// ... other members here
};
Then, elsewhere:
// definition
Derived::Derived(std::string name) : Base(name) {
// ...
}

Unit test noncopyable object

I have a noncopyable monster base class, I also have a IView class.
I have a hobgoblin class that inherits from both monster an IView ,
I have a controller that takes a pointer to IView as a parameter.
Basically I want to check if hobgoblin exploded.
I'm using gmock / gtest
I keep getting
Actual function call count doesn't match EXPECT_CALL(h, Explode())...
Expected: to be called at least once
Actual: never called - unsatisfied and active
when i use the mock object. What am i missing?
Monster Base
#ifndef MONSTER_H
#define MONSTER_H
#include <string>
// interface for all monsters
class monster {
public:
virtual ~monster();
// forbid copying
monster(monster const &) = delete;
monster & operator=(monster const &) = delete;
void receive_damage(double damage);
void interact_with_chainsaw();
std::string name() const;
protected:
// allow construction for child classes only
monster();
private:
virtual void do_receive_damage(double damage) = 0;
virtual void do_interact_with_chainsaw() = 0;
virtual std::string do_name() const = 0;
};
#endif // MONSTER_H
IView
#ifndef IVIEW_H
#define IVIEW_H
class IView
{
public:
virtual void Explode() = 0;
virtual ~IView(){}
};
#endif // IVIEW_H
Hobgoblin
#ifndef HOBGOBLIN_H
#define HOBGOBLIN_H
#include "monster.h"
#include "iview.h"
class hobgoblin : public monster, public IView
{
public:
hobgoblin();
void Explode();
virtual ~hobgoblin();
private:
void do_receive_damage(double damage) final;
void do_interact_with_chainsaw() final;
std::string do_name() const final;
double health_;
};
#endif // HOBGOBLIN_H
#include "hobgoblin.h"
#include <QDebug>
hobgoblin::hobgoblin() :
health_(100.0)
{
}
hobgoblin::~hobgoblin()
{
}
void hobgoblin::Explode()
{
health_ = 0;
qDebug() << "Health is 0";
}
void hobgoblin::do_receive_damage(double damage)
{
health_ -= damage;
}
void hobgoblin::do_interact_with_chainsaw()
{
// imagine horrible, gory things here such as
// having to deal with a singleton
}
std::string hobgoblin::do_name() const
{
static std::string const name("Furry hobgoblin of nitwittery +5");
return name;
}
Controller
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "iview.h"
class Controller
{
public:
Controller(IView *view);
void Explode();
~Controller();
private:
IView *m_View;
};
#endif // CONTROLLER_H
#include "controller.h"
#include <QDebug>
Controller::Controller(IView *view):
m_View(view)
{
}
void Controller::Explode()
{
m_View->Explode();
}
Controller::~Controller()
{
}
Unit Test
class mockmonster : public IView
{
public:
MOCK_METHOD0(Explode,void());
virtual ~mockmonster(){}
};
TEST(MockMonster,Explode)
{
// this is not calling explode as expected.
mockmonster h;
Controller c(&h);
c.Explode();
}
TEST(HobGoblin,Explode)
{
// this calls explode fine
hobgoblin h;
Controller c(&h);
c.Explode();
}
Well, shouldn't your Explode function be virtual?
By the looks of it, your mockmonster is shadowing IView's function. Since Controller is taking a pointer to IView, and Explode is non-virtual, it will invoke IView's version.
As a side-note, I doubt if either of your classes being non-copyable matters here. When using gmock, non-copyable classes are problematic when setting up expectations/assertions (i.e. you expect a function to be called with a specific object - this object would have to be copied internally by gmock, and that might fail).

Requiring derived class to define a method

I am no doubt overlooking something basic but my implementation is obviously flawed.
I am trying to require a derived classes to implement a method being called in a base class.
class IClock
{
public:
virtual void OnTimeExpired() = 0;
}
class Clock : public IClock
{
... // ABC not implemented
}
class Application : public Clock
{
... // ABC not implemented
}
class DerivedApp : public Application
{
public:
virtual void OnTimeExpired() { ... }
}
I rarely use pure ABCs, so I thought by not defining the pure virtual method in Clock and Application, it would require all derivatives of Application to define the OnTimeExpired() method.
I discovered this will compile and link (MSVS-2017) and if DerivedApp does not implement the method, the Clock object will call an undefined method and crash.
Why does this compile without the pure virtual method being implemented?
How do I force derived Application classes to implement the OnTimeExpired() method?
EDIT: The crash was due to unrelated error - I apologize. Nevertheless the questions I ask are still applicable.
As requested here is a complete, buildable, minimal example:
IClock.h:
#pragma once
class IClock
{
public:
virtual void OnClockTime() = 0;
};
Clock.h:
#pragma once
#include "IClock.h"
class Clock : public IClock
{
public:
Clock();
virtual ~Clock();
void ClockUpdate();
virtual void OnClockTime();
private:
float elapsed_time;
};
Clock.cpp:
#include "Clock.h"
Clock::Clock()
: elapsed_time(0.0f)
{
}
Clock::~Clock()
{
}
void Clock::ClockUpdate()
{
elapsed_time += 0.0000001f; // small ticks for testing
if (elapsed_time >= 1.0f) {
OnClockTime();
elapsed_time -= 1.0f;
}
}
void Clock::OnClockTime()
{}
ApplicationBase.h
#pragma once
#include "Clock.h"
class ApplicationBase : public Clock
{
public:
ApplicationBase();
virtual ~ApplicationBase();
virtual void Init(){}
virtual void Run(){}
protected:
bool app_run;
};
ApplicationBase.cpp:
#include "ApplicationBase.h"
ApplicationBase::ApplicationBase()
: app_run(false)
{
}
ApplicationBase::~ApplicationBase()
{
}
DerivedApp.h:
#pragma once
#include "ApplicationBase.h"
class DerivedApp : public ApplicationBase
{
public:
DerivedApp();
virtual ~DerivedApp();
virtual void Init() {}
virtual void Run();
//virtual void OnClockTime();
};
DerivedApp.cpp:
#include "DerivedApp.h"
#include <iostream>
DerivedApp::DerivedApp()
{
}
DerivedApp::~DerivedApp()
{
}
void DerivedApp::Run()
{
app_run = true;
while (app_run) {
ClockUpdate();
}
}
//void DerivedApp::OnClockTime()
//{
// static int counts(0);
// std::cout << "Tick..." << std::endl;
// counts++;
// if (counts >= 10)
// app_run = false;
//}
main.cpp
#include "DerivedApp.h"
class App : public DerivedApp
{
public:
App(){}
~App(){}
};
int wmain(int argc, wchar_t * argv[])
{
App *app = new App();
app->Init();
app->Run();
delete app;
}
Thanks to those who requested a minimal working example, I built it and it works exactly as I had hoped. The complier will complain about no instantiation of the ABC in the App class. If I remove the comments from DerivedApp::OnClockTime() it compiles and runs the way I wish. Obviously my actual code is not following this model as I thought, so now I need to reexamine where I went wrong. Thanks.
There is no keyword in C++ that forces a class to override some method. However, by making OnTimeExpired() pure virtual you're making IClock an abstract class. Any classes deriving from IClock that do not implement OnTimeExpired() will automatically become an abstract class too, thus not allowing you to create objects of these classes. This means that your code as-is is completely legal unless you try to make objects of these classes
class AbstractBase {
public:
virtual void someFunc() = 0; // Purely Virtual
};
class AbstractDerived : public AbstractBase {
public:
void someOtherFunc();
// Still abstract because the following is not declared-defined
// void someFunc() override { ... }
};
class NonAbstractDerivedA : public AbstractBase { // Derived From Base
public:
void someFunc() override { /* do this class's implementation*/ }
};
class NonAbstractDerivedB : public AbstractDerived { // Derived From AbstractDerived
public:
void someFunc() override { /* do this class's implementation*/ }
};
uses:
#include "above"
int main() {
AbstractBase base; // compiler error
AbstractDerived derived; // compiler error
NonAbstractDerivedA derivedA; // should be okay
NonAbstractDerivedB derivedB; // should be okay
return 0;
}

Derivative use base function instead of own function

I have a base class :
base.cpp:
#include "base.h"
base::base()
{
}
base::~base() {
}
void base::baseMethod(int a)
{
std::cout<<"base::baseMethod : "<<a<<std::endl;
}
base.h
#ifndef BASE_H
#define BASE_H
#include <iostream>
class base {
public:
base();
base(const base& orig);
virtual ~base();
void baseMethod(int);
private:
};
#endif /* BASE_H */
And I have derivative class which derive from base
derivative.cpp
#include "derivative.h"
derivative::derivative() : base(){
}
derivative::~derivative() {
}
void derivative::baseMethod(int a)
{
std::cout<<"derivative::baseMethod : "<<a<<std::endl;
}
void derivative::derivativeMethod(int a)
{
baseMethod(a);
derivative::baseMethod(a);
}
derivative.h
#ifndef DERIVATIVE_H
#define DERIVATIVE_H
#include "base.h"
class derivative : public base{
public:
derivative();
derivative(const derivative& orig);
virtual ~derivative();
void derivativeMethod(int);
void baseMethod(int);
private:
};
#endif /* DERIVATIVE_H */
main.cpp
derivative t;
t.baseMethod(1);
t.derivativeMethod(2);
and output is :
derivative::baseMethod : 1
base::baseMethod : 2
base::baseMethod : 2
When I call baseMethod with derivative class object, actually I am using baseMethod of derivative class . But when I call derivetiveMethod, I am using baseMethod of base class. Why is that ? and how can I call baseMethod of derivative class ?
Thanks.
I am using Netbeans 8.2, Windows 7 x64, g++ 5.3.0 (mingw)
You need to make baseMethod virtual in the base class:
virtual void baseMethod(int);
You don't need to "re-affirm" the virtualness in the child classes, but some folk do that for clarity. (That also includes the destructor in the child class).

C++ dynamic_cast exception

Please help me to understand strange behavior:
I use dynamic_cast from MyObject to MyLogicObject when a destructor ~MyLogicObject() in processing, but compiler throw an exception: non_rtti_object.
I'm sure that object MyObject is a polymorph type. Where am I wrong?
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <string>
class A
{
int a;
};
class B
{
int b;
};
class MyObject: public A,
public B// if comment this row, and don't use multi inheritable, everything will be fine
{
private: std::string name;
private: bool singleshot;
public: MyObject(void);
public: virtual ~MyObject(void);
protected: void Destroying(void);
public: std::string GetName(void);
public: virtual bool Rename(std::string _newName);
};
#endif
#include "MyObject.h"
#include "MyLogicObject.h"
MyObject::MyObject(void): singleshot(true)
{}
MyObject::~MyObject(void)
{
printf("\n~my object\n");
Destroying();
}
void MyObject::Destroying(void)
{
if(singleshot)
{
printf("\nexception!\n");
dynamic_cast<MyLogicObject*>(this);// exception: non_rtti_object
singleshot = false;
}
}
std::string MyObject::GetName(void)
{
return name;
}
bool MyObject::Rename(std::string _newName)
{
name = _newName;
return true;
}
#ifndef MYLOGICOBJECT_H
#define MYLOGICOBJECT_H
#include "MyObject.h"
class MyLogicObject: public virtual MyObject // if not use virtual inheritance (instead, use the standard inheritance), everything will be fine
{
public: MyLogicObject(void);
public: virtual ~MyLogicObject(void);
public: virtual void Update(float _delta = 0.0f);
// if reimplement virtual method of base class, everything will be fine
/*
public: virtual bool Rename(std::string _newName)
{
return MyObject::Rename(_newName);
}
*/
};
#endif
#include "MyLogicObject.h"
MyLogicObject::MyLogicObject(void)
{}
MyLogicObject::~MyLogicObject(void)
{
printf("\n~my logic object\n");
Destroying();
}
void MyLogicObject::Update(float _delta)
{}
#include <conio.h>
#include <stdio.h>
#include "MyLogicScene.h"
class C
{
int c;
};
class DerivedObject: public MyLogicObject,
public C// if comment this row, and don't use multi inheritable, everything will be fine
{
public: DerivedObject(void)
{}
public: virtual ~DerivedObject(void)
{
printf("~derived object: %s\n", GetName().c_str());
//Destroying(); // if call Destroying in this place, overything will be file
}
};
int main()
{
DerivedObject* object1 = new DerivedObject();
object1->Rename("object1");
printf("delete object1...\n");
delete object1;
getch();
return 0;
}
You are trying to dynamic cast an object of a base class (MyObject) type to the derived class (MyLogicObject). And this conversion is not allowed with dynamic_cast unless the base class is polymorphic and rtti is enabled. See this for reference.
So you basically need to enable rtti in your compiler options.
Once that is done make sure that object1 is a complete object of the derived class (MyLogicObject) for the cast to work without raising an exception.
It would work in the opposite scenario too. If you were trying for example to dynamic cast an object of a derived class (MyLogicObject) type to the base class (MyObject).