473,386 Members | 2,078 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,386 software developers and data experts.

Single level Inheritance

Hello All,

I have case where I create one instance of a class "Base" and there are various other child objects that I need to create ("Derived" from "Base"). Is there a pattern where I could ensure that once I create a base class instance in memory, all derived classes refer the same base instance ?

class Base{
//..functions etc
}

class Derived: public Base{

//Needs all functionality of Base...

//Overrides a few methods of Base ..
}

Now i first create a Base* pB = new Base();

I need to create a large number of derived objects and wanted to ensure that everytime I do a Derived* pD = new Derived(); I create the derived object and the base object but the base always refers to the exact same instance of "base" created right at the beginning.

I hope this does not sound confusing. Apologies for the same if it is.

Kindly let me know your comments.

Thanks,
Deepak
Feb 16 '07 #1
2 2175
sicarie
4,677 Expert Mod 4TB
Hello All,

I have case where I create one instance of a class "Base" and there are various other child objects that I need to create ("Derived" from "Base"). Is there a pattern where I could ensure that once I create a base class instance in memory, all derived classes refer the same base instance ?

class Base{
//..functions etc
}

class Derived: public Base{

//Needs all functionality of Base...

//Overrides a few methods of Base ..
}

Now i first create a Base* pB = new Base();

I need to create a large number of derived objects and wanted to ensure that everytime I do a Derived* pD = new Derived(); I create the derived object and the base object but the base always refers to the exact same instance of "base" created right at the beginning.

I hope this does not sound confusing. Apologies for the same if it is.

Kindly let me know your comments.

Thanks,
Deepak
Anyone, please correct me if I am wrong, but as I understood inheritance, there did not need to be an instantiated "base" class - you can directly instantiate a derived class, and that will hold all the same methods (unless overridden) as the base class, as well as default values. Other than that, I believe you need to instantiate all derived classes to have the base values you require.
Feb 16 '07 #2
Ganon11
3,652 Expert 2GB
Sicarie is correct. A derived class instance has nothing to do with its base class. For example, consider a very simple class inheritance hierarchy:

Superclass: Mammal (class Mammal)
Subclass: Cat (Cat extends Mammal) (class Cat : public Mammal)
Subclass: Dog (Dog extends Mammal) (class Dog : public Mammal)

Subclass: Chihuahua (Chihuahua extends Dog) (class Chihuahua : public Dog)
Subclass: GermanShepherd (GermanShepherd extends Dog) (class GermanShepherd : public Dog)

Now, if I create a Dog instance, as in Dog myPet = new Dog;, and then want to create a GermanShepherd instance (GermanShepherd myPuppy = new GermanShepherd;), does myPuppy have anything to do with myPet? No! myPuppy is-a Dog, so it has a relationship to the class Dog, but not to any particular instance of Dog.

Any GermanShepherd I create has nothing to do with any Dog I create - but every GermanShepherd and Chihuahua I create is-a Dog. This is the only relationship involved in inheritance.
Feb 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
4
by: Matt Kruse | last post by:
While developing an internal IE6-only webapp, a discussion started about the 'best' way to apply classes to data tables across multiple pages. The two arguments were: 1. Apply a single class to...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
1
by: Gary Wessle | last post by:
Hi is there some guide lines on how many level deep once can structure the inheritance, I have 3 level deep inheritance setup and just wondering whether this is the norm? each level is...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.