在1 C ++项目中管理多个子项目的最佳方法?

时间:2011-03-14 23:35:30

标签: c++ project-management

我正在使用Eclipse CDT。我的想法是,我想在1个项目中管理10个小的练习(10个不相关的问题),因为它似乎没有优化来创建10个项目。我正在使用这样的模型: 有一个cpp文件包含main(),名为“Controller.cpp”,后跟其标题“Controller.h”。每个问题1类,例如:“ex1.cpp”&amp; “ex1.h” - &gt;共22个文件。在main()中,我使用一个开关来选择要解决的问题。但是,不知道为什么每个类,我必须在“ .cpp”中键入原型,并在“ .h”(内部和外部类)中键入原型才能使其工作:< / p>

“Controller.cpp”:

#include "Controller.h"
#include "E2P4.h"
#include "E3P3.h"
#include "E3P4.h"
#include "E3P6.h"
#include "E4P1.h"
#include "E4P2.h"
#include "E4P3.h"
#include "E4P4.h"
#include "E7P6.h"
#include "E7P6Apps.h"
#include "E7P7.h"
#include "E7P7Apps.h"
#include <iostream>
#include <string>
using namespace std;

int main(){
    string choice;
    cout << "Enter choice :" << endl;
    cin >> choice ;

    if ( choice.compare("E2P4") == 0 ){
        E2P4 A;
        A.Do();
    }
    else if ( choice.compare("E3P3") == 0 ){
        E3P3 A;
        A.Do();
    }
    else if ( choice.compare("E3P4") == 0 ){
        E3P4 A;
        A.Do();
    }
    else if ( choice.compare("E3P6") == 0 ){
        E3P6 A;
        A.Do();
    }
    else if ( choice.compare("E4P1") == 0){
        E4P1 A;
        A.Do();
    }
    else if ( choice.compare("E4P2") == 0){
        E4P2 A;
        A.Do();
    }
    else if ( choice.compare("E4P3") == 0){
        E4P3 A;
        A.Do();
    }
    else if ( choice.compare("E4P4") == 0){
        E4P4 A;
        A.Do();
    }
        else if ( choice.compare("E7P6") == 0){
                E7P6apps();
        }
        else if ( choice.compare("E7P7") == 0){
            E7P6apps();
        }
    else cout << "Wrong command !!!" ;
        return 0;
}

Controller::Controller() {
    // TODO Auto-generated constructor stub

}

Controller::~Controller() {
    // TODO Auto-generated destructor stub
}

练习:

#ifndef _H_
#define _H_
void Do();

class E2P4 {
public:
    void Do();
    E2P4();
    virtual ~E2P4();
};

#endif /* _H_ */

和:

#include "E2P4.h"
#include <iostream>
void Do();

void E2P4::Do(){
    std::cout << "Please input Celsius degree :" << std::endl;
    double d;
    std::cin >> d ;
    double converted = 1.8 * d + 32 ;
    std::cout << d << " C degree corresponds " << converted << " F degree";
}

E2P4::E2P4() {
    // TODO Auto-generated constructor stub

}

E2P4::~E2P4() {
    // TODO Auto-generated destructor stub
}

这非常无聊,因为我必须将原型复制3次。

虽然最近我发现了另一种方法,但没有将每个问题都视为一个类。我使用“ex1.cpp”,“ex1.h”和“ex1Application.cpp”和“ex1Application.h”间接地与main()中的.So在“Controller.cpp”中交互,在main()中我只调用它调解员“ex1Application.cpp”。而且只需要原型一次。但它根本需要41个文件。

有没有更好的想法来管理这些?

1 个答案:

答案 0 :(得分:-1)

答案是使用子类。

// Exercises.h
class Exercises {
    public:
    Exercises();
    virtual void Do();
    virtual ~Exercises();
}

// Exercises.cpp
// (Ignoring inline for now; you'll learn about that later)
#include "Exercises.h"
Exercises::Exercises() {}
void Exercises::Do() {}
Exercises::~Exercises() {}

现在,每个类都使用Exercises作为父类:

// E2P4.h
#include "Exercises.h"
class E2P4 : public Exercises {
    E2P4();
    void Do();
    virtual ~EP24();
}

// E2P4.cpp -- same as before
#include "E2P4.h"
E2P4::E2P4 () { /* Whatever */ }

void E2P4::Do() { /* Whatever */ }

E2P4::~E2P4()  { /* Whatever */ }

现在main()只需要知道创建对象时类的作用,而不是在使用它时:

// Main.cpp
void main() {
    // ...

    // Create the object
    Exercise *e;
    switch (which_class) {
        case E2P4: e = new E2P4();
        // ...
    }

    // Use the object
    (*e).Do();

    // When you're finished, you need to delete...
    // I see you've already learned about virtual
    // destructors, so this shouldn't be difficult.
    delete e;
}