Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overload question

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Sorry for asking such basic question, let's I have some source code for two classes : class1.cpp + class2.cpp
in Class1.h I have two functions declared

protected:
drawworld

private:
initialize

initialize is called in the construtor:
Class1::Class1(xxx): Class2(xxx){
initialize();
}

xxx are lots of input params

Now my problem is that I want to change both initialize and drawworld fucntions, how can I do this?

I don't want to modify Class1.cpp code. Do I have to create a new class Class3 derived from Class2 and create a function initialize, will it overides Class2 initialize though?

I hope this is clear.

Thanks!
 
Your question was a little vauge, what is the relationship between Class1 and Class2 (btw, Class1 and Class2 are not very meaningful names)? Well, let me gander at what it looks like from the code... Sounds like you don't quite get inheritence... here goes...

Class1::Class1(xxx): Class2(xxx)
{
initialize();
}

..indicates that Class1 has or is a Class2.
If Class1 HAS A Class2, there is no problem in any redeclartion of these functions (but then there should be an abstract class that they both impliemnt for good design). If Class1 IS A Class2, you'll have no problem overloading the drawworld with public inheritence. You'll want to declare the function virtual in the base class, and make both destructors virtual, so that later you can use the classes with dynamic dispatch. The initialize() is private, and even with public inheritence, you wouldn't be able to override it... The Private is still private to the Base class, and not owned by the derived class (Class1). Make the initialize protected, or don't use it at all.

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top