Virtual functions, function overloading, inheritance - c++

why its not printing 'doub'? Looking for a detailed explanation. Thanks for your time!
#include<iostream.h>
using namespace std;
class B{
public:
virtual int ft(int i) { cout <<"int"; return 0;}
};
class D: public B {
public:
double ft(double i){cout << "doub"; return 0.0;}
int ft(int i) { cout <<"intdoub"; return 0;}
};
int main(){
B *pB = new D;
pB->ft(2.3);
}
o/p is 'intdoub'

The variable pB is of type B* and does not know about the function double D::ft(double), only virtual int B::ft(int). The conversion of the double value 2.3 to int happens automatically, although you should have gotten a compiler warning.
Try:
dynamic_cast<D*>(pB)->ft(2.3);
dynamic_cast<D*>(pB)->B::ft(2.3);

Related

Please help me out why I am getting error?

I can't figure it out why the Derived class remains Abstract after overriding function fun().
Here is the error message:
error: invalid new-expression of abstract class type 'Derived' Base *t = new Derived(a);
error: no matching function for call to 'Base::fun(int&)'int i = t->fun(b);
#include <iostream>
using namespace std;
class Base
{
protected:
int s;
public:
Base(int i = 0) : s(i) {}
virtual ~Base() {}
virtual int fun() = 0;
};
class Derived: public Base
{
public:
Derived(int i) : Base(i) {}
~Derived() { cout << --s << " "; }
int fun(int x) { return s * x; }
};
class Wrapper
{
public:
void fun(int a, int b)
{
Base *t = new Derived(a);
int i = t->fun(b);
cout << i << " ";
delete t;
}
};
int main()
{
int i, j;
cin >> i >> j;
Wrapper w;
w.fun(i, j);
return 0;
}
The function has two different signatures between the base class and derived class
virtual int fun() = 0;
but then the derived class
int fun(int x) { return s * x; }
if you would add override it would alert you to this mistake
int fun(int x) override { return s * x; }
The problem is that
int fun(int x) { return s * x; }
does not override
virtual int fun() = 0;
because the argument list is different (no arguments vs. a single int). If you'd written
int fun(int x) override { return s * x; }
as you should since C++11, then the compiler would have given you an error about this.

Defining a static member

Consider the following code:
#include <iostream >
using namespace std;
class A
{
private:
int x;
public:
A(int _x) { x = _x; }
int get() { return x; }
};
class B
{
static A a;
public:
static int get()
{ return a.get(); }
};
A B::a(0);
int main(void)
{
B b;
cout << b.get();
return 0;
}
My book says:
If we do not use the line of code A B::a(0),there is a compiler error because static member a is not defined in B. To fix the error, we need to explicitly define a.
However, I thought of initializing object a as static A a(0); but it gives me a compiler error. Can someone explain why I can't initialize object a in the manner I described, and why it is necessary to initialize it as they had given it in book.
If you want to define a inline, you need to inline it, which is possible from C++17:
class B {
inline static A a{0}; // or inline static A a = 0;
public:
static int get() { return a.get(); }
};
Demo

Communication between class data members in C++

Looking for how to best access class B's queue through A but I am receiving a segmentation fault. Also I am looking for the best way to communicate between these two classes. Are accessor methods ok in this scenario? What design pattern could work? Thanks
class B {
public:
int get_int() { return qi.front(); }
void put_int(int i) { qi.push(i); }
private:
queue<int> qi;
};
class A
{
public:
void get_event() { cout << b->get_int() << endl; }
void put_event(int a) { b->put_int(a); }
private:
B *b;
};
int main() {
A a;
a.put_event(1);
return 0;
}
As mentioned in comment problem is undefined initialization
you can fix that by using constructor for initialization
#include<iostream>
#include<queue>
using namespace std;
class B {
public:
int get_int() { return qi.front(); }
void put_int(int i)
{
qi.push(i);
}
private:
queue<int> qi;
};
class A
{
public:
void get_event() { cout << b->get_int() << endl; }
void put_event(int a) { b->put_int(a); }
A()
{
b = new B();
}
~A() { delete b; }
private:
B *b;
};
int main() {
A a;
a.put_event(1);
a.get_event();
return 0;
}
Output
1
Program ended with exit code: 0
A a;
is an undefined reference, you have to initialize it with a costructor and since you didn't defined any, you must use the default one
A a=new A();
or better, write the costructors of the two classes as you prefer and use them.

c++ Conversion from 'B' to non-scalar type 'A' requested

I have two errors :
return type 'class A' is incomplete and conversion from 'B' to non-scalar type 'A' requested.
I'm not sure what I am doing wrong because I am not very good on writing classes in C++. Any help would be appreciated! Thanks.
This is the code :
#include <iostream>
using namespace std;
class A;
class B
{
int x;
public: B(int i=10) {x=i;}
operator A();
};
B::operator A() {return x;}
class A
{
int x;
public:A(int i=7) {x=i;}
int get_x() {return x;}
};
int main()
{
B b;
A a=b;
cout<<a.get_x();
return 0;
}
Class A needs to be fully defined before you can return it from the conversion operator (operator A();), which returns an instance of A by value. The fact that is returns it by value is key here, as this requires a type to be fully defined beforehand.
Your code would look like this:
#include <iostream>
using namespace std;
class A
{
int x;
public:A(int i=7) {x=i;}
int get_x() {return x;}
};
class B
{
int x;
public: B(int i=10) {x=i;}
operator A();
};
B::operator A() {return x;}
int main()
{
B b;
A a=b;
cout<<a.get_x();
return 0;
}
The corresponding output is:
10
Verified with Coliru.

What does "name::name" means in C++?

I would like someone to explain me the "name::name" syntax and how it is used on C++ programming. I have been looking through but I don't get it yet. Thanks for help.
Here is context code:
void UsbProSender::SendMessageHeader(byte label, int size) const {
Serial.write(0x7E);
Serial.write(label);
Serial.write(size);
Serial.write(size >> 8);
}
:: is the scope resolution operator.
std::cout is the name cout in the namespace std.
std::vector::push_back is the push_back method of std::vector.
In your code example:
void UsbProSender::SendMessageHeader(byte label, int size) const {
Serial.write(0x7E);
Serial.write(label);
Serial.write(size);
Serial.write(size >> 8);
}
UsbProSender::SendMessageHeader is providing the definition for the SendMessageHeader method of the UsbProSender class.
Another (more complete) example:
class Bar {
int foo(int i); // forward declaration
};
// the definition
int Bar::foo(int i) {
return i;
}
It is operator for scope resolution.
Consider that code
class A { public: void f(){} };
class B { public: void f(){} };
class C : public A, public B {};
int main(int argc, char *argv[])
{
C c;
// c.f(); // ambiguous: which one of two f() is called?
c.A::f(); // OK
c.B::f(); // OK
return 0;
}