2010-09-06 85 views
0

如果我想有3个类,它们有共同的字段(我希望它们是静态的) ,它们有一个共同的功能(需要重写,即虚拟)接口,继承,以及它们之间的什么

  • 什么是最好的设计要做到这一点?
  • 我需要在头文件中创建一个接口
    然后创建它的.cpp文件并从中获得3个类的继承吗?
  • 静态成员呢?
  • 我可以在头文件中声明它们吗?
  • 当创建代表界面的头文件时,我是否必须创建它的.cpp文件?

回答

3

声明头文件中的类。
这是为了使声明可以在多个源文件之间共享(使用#include),并遵从(一个定义规则)。

传统的(虽然不是必需的)每个类都有自己的文件。为了使其一致并且容易找到,你应该在课后给文件命名。因此,类A应在A.h中声明并在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); 
}; 

现在,定义在源文件中的实现:

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 
} 
+0

+1我只是不想写了这一切,^^ – dyp 2010-09-06 17:32:01

+0

在静成员,我可以在MyInterface.cpp中声明一个静态成员,并在每个派生类中对它进行不同的初始化?或者我必须在派生类中声明它们? – 2010-09-06 18:39:23

+0

@ or.nomore:否。静态意味着只有对象。因此所有实例共享相同的值。 – 2010-09-06 20:22:40

1
  • 在C++语言中没有明确的“接口”。
  • 如果您想要一个类似界面的类,那是一个纯虚拟方法的类(这是一个没有定义的方法,例如virtual void printme() = 0;)。
  • 静态变量绑定到目标文件(内部链接)。如果在头文件中定义它们并将头文件包含到几个cpp文件中,那么最终会得到该静态变量的几个定义(在不同的目标文件中)
  • 由于静态变量既可以是全局变量,也可以是类的一部分,他们不能'共同'。它们属于一个类别,可能会被另一个类别访问。
  • 同样的方法。一类有一种方法,另一类可以称之为方法。如果它是派生类,它也可以覆盖它(即隐藏它或实现虚拟方法)。

现在,如果您有三个具有相同结构的类,您可能(或可能不)从几个原因继承基类。一种是避免复制代码。另一个原因是你可能想要从派生类中处理对象,比如说你有一辆可以使用的车辆,但是车辆可能是汽车,自行车或飞机。你想用车辆,但不介意它实际上是,那么您创建


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

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

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

哪辆车比你可以使用汽车作为交通工具,例如


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

这一切是根本C++ OOP,在一些书中查找它。