开发者

interfaces, inheritance, and what between them

开发者 https://www.devze.com 2023-01-14 20:38 出处:网络
if i want to have 3 classes, which have common fields (and i want them to be static) and they have a common function (which needed to be overridden, i.e virtual)

if i want to have 3 classes, which have common fields (and i want them to be static) and they have a common function (which needed to be overridden, i.e virtual)

  • what the best design to do this?
  • do i need to create an interface in a header file

    and then create it's .cpp file and get the 3 classes inheritance from it?

  • what about the static members?
  • 开发者_如何学JAVAcan i declare them in the header file?
  • when creating header file which representing interface, do i have to create it's .cpp file?


Declare the classes in header files.
This is so that the declaration can be shared between multiple source files (with #include) and thus obey the (One definition rule).

It is traditional (though not required) that each class has its own file. To make it consistent and easy to find things you should name the file after the class. So Class A should be declared in A.h and defined in A.cpp.

MyInterface.h

class MyInterface
{
    protected:
        static int X;
        static int Y;
        static int Z;
    public:
        // If a class contains virtual functions then you should declare a vritual destructor.
        // The compiler will warn you if you don't BUT it will not require it.
        virtual ~MyInterface() {}   // Here I have declared and defined the destructor in
                                    // at the same time. It is common to put very simplistic
                                    // definitions in the header file. But for clarity more
                                    // complex definitions go in the header file. C++ programers
                                    // dislike the Java everything in one file thing because it
                                    // becomes hard to see the interface without looking at the
                                    // documentaiton. By keeping only the declarations in the
                                    // header it is very easy to read the interface.

        virtual int doSomthing(int value) = 0; // Pure virtual
                                               // Must be overridden in derived
 };

A.h

 #include "MyInterface.h"

 class A: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

B.h

 #include "MyInterface.h"

 class B: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

C.h

 #include "MyInterface.h"

 class C: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

Now you define the implementation in the source files:

MyInterface.cpp

#include "MyInterface.h"

// Static members need a definition in a source file.
// This is the one copy that will be accessed. The header file just had the declaration.
int MyInterface::X = 5;
int MyInterface::Y = 6;
int MyInterface::Z = 7;

A.cpp

#include "A.h"

// Define all the methods of A in this file.
int A::doSomthing(int value)
{
    // STUFF
}

B.cpp

#include "B.h"

int B::doSomthing(int value)
{
    // STUFF
}

C.cpp

#include "C.h"

int C::doSomthing(int value)
{
    // STUFF
}


  • There is no explicit "interface" thing in the C++ language.
  • If you'd like to have an interface-like class, that's a class with pure virtual methods (that is a method w/o definition, e.g. virtual void printme() = 0;).
  • Static variables are bound to object files (internal linkage). If you define them in your header file and include that header file into several cpp files, you'll end up having several definitions of that static variable (in different object files)
  • Since static variables are either global or part of a class, they cannot be 'common'. They belong to one class and may be accessed by another one.
  • Same goes for methods. One class has a method, another one may call it. If it's a derived class, it may also override it (that is either hide it or implement a virtual method).

Now, if you have three classes that have the same structure, you may (or may not) like to inherit them from a base class for several reasons. One is to avoid copying code. Another one is the main reason, that you may want to treat objects from the derived classes all the same, let's say you have a vehicle that you can use, but the vehicle may be a car, a bike or a plane. You want to use a vehicle, but don't mind which vehicle it actually is, so you create


class Vehicle
{
public:
   virtual void use() = 0;
};

class Car
   : public Vehicle
{
public:
   virtual void use();
};

void Car::use()
{
   // drive the car
}

Than you can use a Car as vehicle, for example


Car myCar;
Vehicle& myVehicle = static_cast< Vehicle& >(myCar);
myVehicle.use();  // drive the car.

That all is fundamental C++ OOP, look it up in some book.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号