473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question on C++ inheritance.

In base class, I have a function "Initialize(int a)". In the derived
class , I need the funciton "Initialized" to do more things, therefore
I redefined the function as "Initialize(int a, int b)".

Now I want the Derived::initialize to include all the operations
contained in Base::initialize. Are there and simple ways to do that?
Currently, I just copy all the codes in Base::initialize to
Derived::initialize.

Thanks.

May 7 '06 #1
3 1345

"syang8" <sy****@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
In base class, I have a function "Initialize(int a)". In the derived
class , I need the funciton "Initialized" to do more things, therefore
I redefined the function as "Initialize(int a, int b)".

Now I want the Derived::initialize to include all the operations
contained in Base::initialize. Are there and simple ways to do that?
Currently, I just copy all the codes in Base::initialize to
Derived::initialize.


Initialization should occur in a constructor.
When a derived object is created, its constructor,
as well as the base's constructor will be
invoked automatically. (Base constructor is
called first).

class Base
{
public:
Base(int a)
{
/* etc */
}
};

class Derived : public Base
{
public:
Derived(int a, int b) : Base(a)
{
/* etc */
}
};

int main()
{
Derived d(42, 99); /* invokes Base::Base(42),
then Derived::Derived(42,99) */
return 0;
}

If the parameter 'a' for Derived() is not the same
value you want to pass to Base(), then add another
parameter to Derived() to pass to Base(), e.g.:

Derived(int b, int d1, int d2) : Base(b)
{
/* etc */
}

-Mike
May 7 '06 #2
Thanks for your reply. I agree your point. However, my question is how
to handle more general situations. For example, some other functions
instead of initialize().

May 7 '06 #3

"syang8" <sy****@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
In base class, I have a function "Initialize(int a)". In the derived
class , I need the funciton "Initialized" to do more things, therefore
I redefined the function as "Initialize(int a, int b)".

Now I want the Derived::initialize to include all the operations
contained in Base::initialize. Are there and simple ways to do that?
Currently, I just copy all the codes in Base::initialize to
Derived::initialize.

Thanks.


Derived::Initialize(int a, int b)
{
Base::Initialize(a);
// other stuff with b
}

You are right to look for another solution if you are copying code. In the
long run that is impossible to maintain.
May 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.