Using 'override' keyword - oop

My code gives the following error:
error C3668: 'B::getData': method with override specifier 'override' did not override any base class methods
#include<iostream>
#include <tuple>
using namespace std;
class A {
public:
int a;
int getData() {
return a;
}
};
class B : public A {
public:
int b;
B() {
b = 100;
}
int getData() override {
return b;
}
};
int main() {
B b;
cout << b.getData() << endl;
}
Why does error occurs and how can I resolve it ??

Your original function in A has to be virtual to be overrided.
class A {
public:
int a;
virtual int getData() {
return a;
}
};
More info about override is here. And related: info on virtual and final

Related

Hi, I have a question regarding classes and constructors

class Stack {
private:
int stackstore[100];
int SP;
public:
Stack(void) { SP = 0; }
void push(int value);
int pop(void) {
return stackstore[--SP];
}
};
void Stack::push(int value) {
stackstore[SP++] = value;
}
In this case I want to understand what difference it makes initializing SP in the constructor to doing it directly when declaring the variable. Why is it a better practice to do it the way depicted above rather than this way (without the constructor) ->:
class Stack {
private:
int stackstore[100];
int SP = 0;
public:
void push(int value);
int pop(void) {
return stackstore[--SP];
}
};
void Stack::push(int value) {
stackstore[SP++] = value;
}
Thank you in advance for any help :)

Singleton getinstance() method with void as return type

greetings.
I wrote the singleton class as shown below.
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool inst;
static Singleton * ptr;
Singleton()
{
cout<<"Singleton Private Constructor is called"<<endl;
}
public:
static Singleton * Create_Instance()
{
if(!inst)
{
ptr = new Singleton();
inst = true;
cout<<"New instance is created"<<endl;
}
return ptr;
}
};
bool Singleton::inst = false;
Singleton * Singleton::ptr = NULL;
int main()
{
Singleton * point = Singleton::Create_Instance();
return 0;
}
Here the Singleton instance is returned to main from Create_Instance() method.
Here my is what if the return value of Create_Instance() is void ? Meaning if the signature is going to be "static void Create_Instance()", then how we will get the instance of Singleton in main.
Please help me in this regard.

org.apache.avro.SchemaParseException: Undefined name:

For converting generics to avro schema i use the below annotation , but I get the exception after that
public class Test2<T>
{
#AvroSchema("{\"type\":\"array\",\"items\":[\"null\",\"Test4\"]}")
private Set<T> test4;
}
public class Test4 {
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public Test4(Integer x) {
this.x = x;
}
public Test4() {
}
private Integer x;
}
The error I get here is
org.apache.avro.SchemaParseException: Undefined name:Test4
This thing worked
#AvroSchema("{\"type\":\"array\",\"items\":{\"name\":\"Child\",\"type\":\"record\",\"fields\":[{\"name\":\"x\",\"type\":\"int\"}]}}")
Thanks btw.

how to check the an interface type in c++/cli

i want to convert that line from c# to c++/cli
Idocobj is IPart
IPart is an interface and Idocobj is an object.Are there any way to do this conversion.
i used this code :
Idocobj->GetType() == IPart::typeid
but it dosen't work
You can use dynamic_cast to check for "is". Here is an example:
using namespace System;
namespace NS
{
public interface class IFoo
{
void Test();
};
public ref class Foo : public IFoo
{
public: virtual void Test() {}
};
public ref class Bar
{
public: virtual void Test() {}
};
}
template<class T, class U>
bool isinst(U u) {
return dynamic_cast< T >(u) != nullptr;
}
int main()
{
NS::Foo^ f = gcnew NS::Foo();
NS::Bar^ b = gcnew NS::Bar();
if (isinst<NS::IFoo^>(f))
Console::WriteLine("f is IFoo");
if (isinst<NS::IFoo^>(b) == false)
Console::WriteLine("f is not IFoo");
Console::ReadKey();
}
But normally, you never use "is".... you always want to do something with the check... so normally you should use "as" which directly mapps to dynamic_cast:
NS::IFoo^ ifoo = dynamic_cast<NS::IFoo^>(f);
if (ifoo != nullptr)
{
// Do something...
ifoo->Test();
}

Problem with design in OOP (Virtual member call in constructor)

I am trying to achieve something like the following:
class Foo
{
public virtual int Number { get; set; }
public Foo(int n)
{
Number = n; //Virtual member call in constructor
}
public void Do() {
Console.WriteLine(Number);
}
}
class Bar : Foo
{
public override int Number
{
get
{
return x.Val;
}
set
{
x.Val = value;
}
}
Bar(int n) : base(n)
{
X x = new X();
x.Val = n;
}
public void F() {
x.Something(); //changes x.Val
}
}
The reason I am doing this is because I need to propagate the call to Do when called from a variable of type Bar.
Now, I can have objects that either inherit from Foo or Bar, thus Number needs to be the way it is now, ie directly expose the Val property of x. This is because I need to allow for the following code:
Bar b = new Bar(5);
b.F(); //changes the value of Val in x
b.Do(); //Needs to print the correct, modified value
The problem here is obviously in Foo, when assigning n to Number (Virtual member call in constructor) since x from the subclass would not have been initialized yet.
What do you think is a better way to structure such a design?
I can't use this current code because I am calling a virtual member from the constructor, which causes the Object Reference not set to an Instance of an Object exception since x in Number from Bar would not have been yet initialized.
My favorite quote:
Favor composition over inheritance
I would separate underlying data from operations:
interface IMyNumber
{
int Number { get; set; }
void Something();
}
class MyNumber : IMyNumber
{
public int Number { get; set; }
public MyNumber(int n)
{
Number = n;
}
void Something() {... }
}
class MyBoxedNumber : IMyNumber
{
int Number { get { ... } set {... } }
void Something() {... }
}
class Bar
{
private IMyNumber_foo;
Bar(IMyNumber foo)
{
_foo = foo;
}
public void F() {
_foo.Something(); //changes x.Val
}
public void Do() {
Console.WriteLine(...)
}
}