1

I'm wondering if the following can be done in C++.

What I want to do is have a group of classes which all have the same public parts -- that is, the same public variables, and the same set of methods -- however, different classes within this group of classes would differ in the private parts -- that is, they could have different private variables -- and though the set of methods would be uniform for all classes of such a group, the implementations of these methods could vary.

And there's the one extra bit -- which I wonder if C++ supports -- I would like it to be so that all objects which belong to classes within this group be effectively the same datatype. That is, any part of the program outside the internals of the class will make no distinction between objects of one class and objects of the other class -- provided that both classes are part of this same group.

A function that takes as an argument one class of this class-group would be able to take as that argument any class of this class-group. A variable that references an object of one class of this class-group could reference an object of any class of this class-group.

Why do I want to know if this can be done? Because if it can be, I could use such a group of classes as an abstraction layer.

Sophia_ES
  • 1,034
  • 2
  • 11
  • 21
  • 4
    Did you read enough about C++ ? Do you know what for `template` and `virtual` keywords are used? You could have an abstract class with only virtual member functions (and inherit from it), etc... – Basile Starynkevitch Jan 11 '14 at 08:14

3 Answers3

1

Looks like you are about interfaces. Well, there are not ones purely implemented in C++. In C++ you can create a pattern-simulation of interface.

Also you can find it useful to read about differences between intefraces and abstract classes in C#.

Community
  • 1
  • 1
Ilya Tereschuk
  • 1,164
  • 7
  • 20
  • Yes -- after reading your answer, I followed up by reading a bit about interfaces. They do seem to be what I need. Unfortunately, though, it appears that C# is too closely tied to the Microsoft Visual Studio for my purposes. Also, none of the documentation I could find gave a straightforward answer about whether or not all objects defined under a common interface are treated as the same data-type --- and that is a very crucial non-negotiable trait of what I am looking for. Still, it appears as though your answer is the best from among the three I was given. Thanks. – Sophia_ES Jan 14 '14 at 19:03
  • @user3080003 I was glad to help ;) – Ilya Tereschuk Jan 14 '14 at 19:05
1

This really looks like a job for inheritance. You can use a base class that defines the base data type (as you requested) and at the same time defines the public interface (possibly with pure virtual member functions) for the "group" of classes.

Shoe
  • 70,092
  • 30
  • 150
  • 251
0

Create an abstract base class for all your "public parts" and then create subclasses for your "private parts." All instances of all of the classes can then be simply referenced using variables and method parameters declared to be of the abstract base class type. OOP 101...

And, of course, there is more than one way to do it...

Ned
  • 887
  • 6
  • 8