pure virtual functions - c++

In the C++ program:
#include<iostream.h>
class A
{
public: virtual void func()=0;
};
class B:public A
{
public: void show()
{
func();
}
};
void B::func()
{
cout<<"In B"<<endl;
}
int main()
{
B b;
b.show();
}
If the virtual function, func() is redefined within body of the class B, there is no error. But when using the scope resolution operator, the compiler throws an error.
Why is that?

This is not directly related to func being virtual, you always need to declare it in the class:
class B:public A
{
public: void show()
{
func();
}
void func(); // add this
};
void B::func()
{
cout<<"In B"<<endl;
}

You have to declare that you redefine the member function func() in class B.
class B:public A
{
virtual void func();
public:
void show() {func(); }
};

Related

Is it possible to unhide only specific overloaded method of Base class

#include <iostream>
class B
{
public:
virtual void f() {std::cout<<"Fi";}
virtual void f(int) {std::cout<<"GI";}
};
class A : public B
{
public:
void f(double) {std::cout<<"HI";}
using B::f;//but I want to use only f(), not f(int)
};
int main () {
A a;
a.f();
a.f(10);
return 0;
}
Is it possible in derived class A unhide only f() overload? So a.f(10); would call A::f(double)
You can replace the using statement with a function that calls the base class method:
void f() { B::f(); }
For what you are attempting, make A use protected or private inheritance instead so B's methods are not public in A. Then A can declare its own methods that call B's methods internally.
#include <iostream>
class B
{
public:
virtual void f() {std::cout<<"Fi";}
virtual void f(int) {std::cout<<"GI";}
};
class A : protected B
{
public:
void f(double) {std::cout<<"HI";}
void f() { B::f(); }
};
int main () {
A a;
a.f();
a.f(10);
return 0;
}
Output:
FiHI
Live Demo

Why override under private inheritance?

class Base {
public:
virtual void f() {}
};
class Derived : private Base {
public:
void f() override {}
};
My question is there any use to such override? Private inheritance implies that you can not store Derived in Base pointer and thus it will never be needed to dynamically dispatch f to the correct type.
Just one example: A function of Derived::f1() can call a (public or protected) functions of Base::f2(), which in turn can call f(). In this case, dynamic dispatch is needed.
Here is an example code:
#include "iostream"
using namespace std;
class Base {
public:
virtual void f() {
cout << "Base::f() called.\n";
}
void f2() {
f(); // Here, a dynamic dispatch is done!
}
};
class Derived:private Base {
public:
void f() override {
cout << "Derived::f() called.\n";
}
void f1() {
Base::f2();
}
};
int main() {
Derived D;
D.f1();
Base B;
B.f2();
}
Output:
Derived::f() called
Base::f() called

Inheritance method, the derived method invokes not the base

I have class A and class B, for instance this is the code
class A{
void show(){.....}}
class B:public A{
void show(){....}}
void main()
{ int arr[2];
arr[0]=new A();
arr[1]=new B();
arr[1]->show();
}
in this situation I want the show of class B to action, but no matters what I did it always go to ths 'show' of the parent..
what can I do?
You must make it a virtual method in the base class, also the array should be A* in your case not int. Then these two point will solve your problem:
struct A
{
virtual void show() {}
//^^^^^^^
};
struct B : public A
{
void show() {}
/* virtual void show() override {} */
};
int main()
{
A *arr[2];
arr[0] = new A;
arr[1] = new B;
arr[1]->show();
}
Last word, avoid bare pointers, you can use smart pointers such as unique_ptr or shared_ptr:
struct A
{
virtual void show() {};
};
struct B : public A
{
void show() {}
/* virtual void show() override {} */
};
int main()
{
unique_ptr<A> arr[2];
arr[0].reset(new A);
arr[1].reset(new B);
arr[1]->show();
}

How to call a base class's virtual function that is an input argument to a function

using C++, I have
struct Base {
virtual void stuff(/*base stuff*/);
};
struct Derived : public Base {
void stuff(/*derived stuff*/);
};
void function1(Derived& obj){
obj.stuff();
}
In this scenario, function1 will use Derived's do() function. What if in function1, I want to call the Base class's do() function instead ? Will it work if I call function1 as
function1(dynamic_cast<Base*>(derived_obj_ptr))?
After correcting the multitude of errors in your code, this is indeed achievable:
#include <iostream>
class Base {
public:
virtual void foo() { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
void foo() { std::cout << "Derived\n"; }
};
void function1(Derived *p) {
p->Base::foo(); // <<<<< Here is the magic >>>>>
}
int main() {
Derived d;
function1(&d);
}
Output:
Base
(see http://ideone.com/FKFj8)

Is it function overloading or overriding?

Is it function overloading or overriding or something else ? ( hello function )
class A {
public:
void hello(int x) { cout << "A" << endl; }
};
class B : public A {
public:
void hello() { cout << "B" << endl; }
};
void main() {
B obj;
obj.hello();
}
It's neither, it's function hiding.
Declaring a non-virtual function with the same name (even if the signature is different) in a derived class hides the base class implementation completely.
To still have access to A::hello, you can do the following:
class B : public A {
public:
using A::hello;
void hello() { cout << "B" << endl; }
};
Overriding:
struct A
{
virtual void foo() {}
};
struct B : public A
{
/*virtual*/ void foo() {}
};
Overloading:
struct A
{
void foo() {}
void foo(int) {}
};
Overriding:
struct Base {
virtual void Foo() { std::cout << "Base\n"; };
};
struct Derived : Base {
virtual void Foo() { std::cout << "Derived\n"; };
// same name and signature as a virtual function in a base class,
// therefore this overrides that function. 'virtual' is optional in
// the derived class (but you should use it).
};
C++11 adds a way to ensure your function overrides:
struct Derived : Base {
virtual void Foo() override { std::cout << "Derived\n"; };
};
Now if the method Foo is not overriding something then you will get an error.
struct Base {
void Foo();
};
struct Derived : Base {
virtual void Foo() override; // error, Derived::Foo is hiding Base::Foo, not overriding
};
This is neither.
Overriding is a function with the same signature (same name, parameters and return value) in a subclass. Overloading is a function with the same name but different parameters.
It is not Overloaded Function because in Function Overloading either number of parameters or type of parameters should differ.For example :
void show(int a,int b);
void show(int a);
void show(char a,char b);
And neither it is Function Overriding because in Overridden function prototype must be same.For example :
Base class and Derived class having function
void show();
//function prototype must be same