Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm doing a small assignment in C++ where I have to use concepts like virtual functions and inheritance in a domain of my choice. I'm implementing the prototype for a package manager in Linux.

I have the base class Package, this has attributes like package_id, name, version, dependencies etc. There are two types of packages MetaPackages and ConcretePackages which differ in the way they must be installed and removed.

I am thinking of declaring the methods install and remove in Package as virtual. While storing these packages in a datafile can I somehow know if the stored package is Meta or Concrete.

I'm not sure as to how I should best design this. Can anybody point me to a resource so that I can better learn this stuff.

We have Grady Booch in our curriculum and honestly it's a pain to read much less comprehend.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I am thinking of declaring the methods install and remove in Package as virtual.

Sounds like a good idea.

While storing these packages in a datafile can I somehow know if the stored package is Meta or Concrete.

Why do you need to know it? The point of polymorphism (and probably of this specific exercise) is that the user of the object doesn't need to know its concrete type, just call its methods. If said methods adhere to the "contract" defined by the base interface, their implementation details aren't important for the caller.

You should implement the install and remove methods in each concrete subclass according to the specifities of meta and concrete packages. Then the installer of the packages needs only to iterate through the collection of packages and call the respective method on each.

share|improve this answer
    
May be I musenderstood but ... was the OP needing to save the data on file and load them back? If so, this is a "serialization" problem, and a way to detect what has been saved to reconstruct the appropriate objects is required. –  Emilio Garavaglia Oct 21 '11 at 15:01
    
@EmilioGaravaglia, possible. In this case, here is a relevant thread: stackoverflow.com/questions/243365/… –  Péter Török Oct 21 '11 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.