What the diff between a virtual method and an inheirited method

J

jlopes

There seems to bet no diff between a vitual method and an inheirited method.

class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};
 
S

Surendra Singhi

jlopes said:
There seems to bet no diff between a vitual method and an inheirited method.

class A_Base
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_A_Base : public A_Base
{
public:
void filter(){ /* could have used the filter of the base class */ }
};

class AnotherBase
{
public:
void filter(){ /* some code */ }
};

class D_of_A_Base : public AnotherBase
{
public:
filter(){ /* could have used the filter of the base class */ }
};

I didn't get what you mean by no difference. Do you understand what are
virtual functions? Do you understand polymorphism?

As far as C++ is considered they are chalk and cheese.
I guess in Java they are the same i.e., inherited functions are also
virtual. Correct me on this if I am wrong.

I will advise you to read the chapters on virtual function in Stroustrup
or Lippman's book.
 
C

Cheng Mo

Another question pops up in my mind about virtual funtion.
Call to inline member function will be replaced by the function body
during compilation; and virtual function is reference by VTABLE. So,
what about virtual inline member function?

class Foo
{
public:
virtual static print(const std::string &str)
{
std::cout << str << std::endl;
}
}

Is call to print replaced by function body? or "inline" is ignored if
the function is virtual?
 
N

Niels Dybdahl

Another question pops up in my mind about virtual funtion.
Call to inline member function will be replaced by the function body
during compilation; and virtual function is reference by VTABLE. So,
what about virtual inline member function?

class Foo
{
public:
virtual static print(const std::string &str)
{
std::cout << str << std::endl;
}
}

Is call to print replaced by function body? or "inline" is ignored if
the function is virtual?

I guess you have made a mistake in the function above and written static
instead of inline.
However if the compiler knows where the object originates from, then it
might be sure that the virtual function is not overridden and then it can
make the function inline. If the origin is unsure (object is a parameter to
a function), then it must call the function via VTABLE.

Niels Dybdahl
 
J

jlopes

Surendra Singhi said:
I didn't get what you mean by no difference. Do you understand what are
virtual functions? Do you understand polymorphism?

As far as C++ is considered they are chalk and cheese.
I guess in Java they are the same i.e., inherited functions are also
virtual. Correct me on this if I am wrong.

I will advise you to read the chapters on virtual function in Stroustrup
or Lippman's book.

Let me refine my question. Given a set of objects, each with common
behavior. I then pull out that common behavior into a single object.
That becomes the base of the set of objects. The set need not use
polymorphism, I could make the common methods virtual or not. Since
the common methods are inherited I need only use them. Having read
Stroustrup 3rd ed, he presents vitual methods in their pure form. To
say the a Base class method is virtual and its children in herit that
method is no different than simplly defining the method.

So my qusetion:
If one removes the concept of polymorphism and pure virtual
methods. To inherit a method from a base class defined as virtual is
no different than a defined method in that base class.
 
L

le ténébreux

So my qusetion:
If one removes the concept of polymorphism and pure virtual
methods. To inherit a method from a base class defined as virtual is
no different than a defined method in that base class.

I think the whole point of using virtual functions is
to allow the derived classes to define their own unique
behaviour for that function.

Say you want to call a function through a single pointer to
the base class which at runtime may arbitrarily point to any
derived object. If the function is virtual, it will use the
appropriate code for that derived class. Extremely useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top