how to call base class method from derive class in c++? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to call an object's base class method that's overriden? (C++)
First question is calling super() constructor in java same as initializing the super class constructor first in c++ like.
sub() : super(){}
is there a way to call super class method in c++ like in java
ex.
public sub(){
super.someMethod();
}

To call the base constructor of a class, you call it as BaseClassName(args). For example:
class A
{
public:
A() { }
virtual void Foo() { std::cout << "A's foo" << std::endl; }
};
class B : public A
{
public:
B() : A() { }
void Foo();
};
To call the base class version of a method, you do BaseClassName::MethodName:
void B::Foo()
{
std::cout << "B's foo" << std::endl;
A::Foo();
}

Related

Why inheritance doesn’t work inside methods? [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 1 year ago.
This does not compile:
struct Base
{
void something( int a ) { }
};
struct Derived : public Base
{
static void something()
{
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->something( 11 );
}
};
It’s possible to fix with using Base::something but still, is it possible to make inheritance work as advertised even inside methods?
By using the same name for the function in the derived class you hide the symbol from the base class.
You can solve it by pulling in the name from the base class with the using statement:
struct Derived : public Base
{
// Also use the symbol something from the Base class
using Base::something;
static void something()
{
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->something( 11 );
}
};
I'm not exactly sure what you are trying to accomplish. I added virtual, and changed the name of the Derived something class function, and put in two variants. One variant calls through virtual inheritance, the other calls Base class member function directly.
#include <iostream>
using std::cout;
namespace {
struct Base {
virtual ~Base();
virtual void something(int a) { std::cout << "Base a:" << a << "\n"; }
};
Base::~Base() = default;
struct Derived : Base {
void something(int b) override { std::cout << "Derived b:" << b << "\n"; }
static void action() {
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->something(11);
}
static void other_action() {
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->Base::something(11);
}
};
} // anon
int main() {
Derived::action();
Derived::other_action();
}

`virtual` `override` destructor [duplicate]

This question already has answers here:
Is the 'override' keyword just a check for a overridden virtual method?
(6 answers)
Closed 3 years ago.
In the following example:
class A {
public:
virtual ~A() { std::cout << "~A" << std::endl; }
};
class B : public A {
public:
virtual ~B() override { std::cout << "~B" << std::endl; }
};
class C : public B {
public:
~C() override { std::cout << "~C" << std::endl; }
};
clang-tidy gives the following warning for class B:
'virtual' is redundant since this function is already declared 'override'
Removing the virtual keyword from class B seems to allow all destructors in the chain to be called, but I want to make sure that I'm not missing anything.
Removing virtual from function that has override does not change the meaning of the program in any way. That is what it means for the keyword to be redundant (in that context). The removal doesn't allow anything that isn't allowed without the removal.

A protected method can become a private method when inherited in c++? [duplicate]

This question already has answers here:
Changing Function Access Mode in Derived Class
(4 answers)
Closed 3 years ago.
I've been playing around with inheritance and I've tried this code:
#include <iostream>
#include <string>
class Foo
{
public:
virtual void func() = 0;
protected:
virtual void doSum() const = 0;
};
class Bar : public Foo
{
public:
void func() {
doSum();
}
protected:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
};
int main()
{
Foo* ptr = new Bar();
ptr->func();
return 0;
}
So I've also tried replacing the protected keyword in the class Bar with private like this :
private:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
and the code happened to work just the same...
So my question is, is there any difference if I declare a protected method private when implementing a derived class? If so, what are they? Am I even allowed to do this?
So my question is, is there any difference if I declare a protected method private when implementing a derived class?
Yes.
If so, what are they?
That will prevent the next level of derived class from being able to call the derived class's implementation.
class Foo
{
protected:
virtual void doSum() const = 0;
};
class Bar : public Foo
{
private:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
};
class Baz : public Bar
{
public:
void doSum() const
{
//===========================
Bar::doSum(); // NOT ALLOWED
//===========================
}
};
Am I even allowed to do this?
Yes.
So my question is, is there any difference if I declare a protected method private when implementing a derived class?
No. There is no difference. Unfortunately, C++ standard does not impose any requirement on the derived class to place the overriding virtual function within any particular accessibility scope. This means, the base class could declare a virtual method protected, and the derived class could implement the method in public/protected/private scope and the code will still be legal and will work.

Inheritance and Overriding - Call Overriding SubMethod from BaseClass [duplicate]

This question already has answers here:
Calling a virtual function from the constructor
(3 answers)
Closed 5 years ago.
#include <iostream>
class Base {
public:
virtual void method() {
std::cout << "Base" << std::endl;
}
Base() {
method();
}
};
class Sub : public Base {
public:
virtual void method() {
std::cout << "Sub" << std::endl;
}
Sub() : Base() {
}
};
int main(void) {
Base *b = new Sub();
delete b;
system("PAUSE");
return 0;
}
Output: "Base"
What do i have to change to make the Base call the Sub Method instead of the Base?
This is probably a duplicate and a beginner-question, but i couldn't find an answer to this problem.
Also a suggestion for a better title is welcome as the current one might be wrong.
a) because the standard says so.
b) philosophically, because Sub at this point has not had its constructor called (inherited objects are constructed in a depth-first manner), so calling Subs version of method would surprise the author of Sub.

C++ virtual method overriding [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
main.cpp
#include <iostream>
class BaseClass {
public:
BaseClass() {
init();
}
virtual ~BaseClass() {
deinit();
}
virtual void init() {
std::cout << "BaseClass::init()\n";
}
virtual void deinit() {
std::cout << "BaseClass::deinit()\n";
}
};
class SubClass : public BaseClass {
public:
virtual void init() {
std::cout << "SubClass::init()\n";
}
virtual void deinit() {
std::cout << "SubClass::deinit()\n";
}
};
int main() {
SubClass* cls = new SubClass;
delete cls;
return 0;
}
Why is init() and deinit() not properly overriden and the BaseClasses' methods are called instead of the SubClasses ones? What are the requirements to make it work?
BaseClass::init()
BaseClass::deinit()
Because you are calling a virtual method inside a constructor. While constructing Base class the derived one (SubClass) isn't still constructed, so actually it doesn't still exist.
It's generally a good practice to avoid calling virtual methods inside constructors.
They are overridden just fine.
But you've invoked them from the base constructor, and when the base constructor is executing, the derived part of the object does not yet exist.
So this is a largely a safety feature, and it's mandated by the C++ standard.