简体   繁体   中英

How to split program up in c++

I'm getting into OOP now, but the files are becoming quite big. I was wondering if there was a way of putting some larger functions in separate files and the classes into a separate file too. For example, say I had the code:

#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

//------------------------SHAPE------------------------------------//

class shape {
    protected:
        double area;
    public:
        shape(): area(0) {}
        shape(double A): area(A) {}
        virtual ~shape() {}
};

//------------------------SQUARE------------------------------------//

class square : public shape {

    friend ostream & operator<<(ostream &os, const square &squ);

    protected:
        double width;
    public:
        square(): shape(0), width(0) {}
        square(double w): shape(w*w), width(w) {}
        virtual ~square() {}

};

ostream & operator<<(ostream &os, const square &squ) {
    os << "Width: " << squ.width << ", Area: " << squ.area;
    return os;
}

//------------------------CUBE-------------------------------------//

class cube : public square {

    friend ostream & operator<<(ostream &os, const cube &cub);

    public:
        cube(): square(0) {}
        cube(double w):
            square(w) {}
        cube(square squ):
            square(squ.getwidth()) {}
        ~cube() {}
        double volume() const {
            double temp = width*area;
            return temp;
        }

};
ostream & operator<<(ostream &os, const cube &cub) {
    os << "Width: " << cub.width << ", Volume: " << cub.volume();
    return os;
}

double square(double N) {
    return N*N;
}
//------------------------MAIN------------------------------------//

int main() {

    square s1;
    //other stuff

}

How would I put the classes in one file and the function in another and the main in another but still have it all compile with g++?

Typically, your declarations go into a .h file, and the definitions go into multiple .cpp files.

Each file that needs to know the structure of each of those classes needs to #include the .h file.

Since your shape class is well-structured already, it can be put into shape.h pretty much as-is.

Your square class, as it is derived from shape , needs to #include "shape.h" as well as a few of the other global includes, such as #include <iostream> since it's utilizing ostream .

Your cube class is actually mixing both declaration AND definition in some parts, which is better to be broken apart: double volume() const should only be declared in the cube.h file, and then actually defined in cube.cpp with:

double cube::volume() const {
    double temp = width*area;
    return temp;
}

When you actually go to compile, the typical way to do it is to compile them into object files, usually on the command line with g++ -c -o cube.o cube.cpp cube.h shape.h , where it creates a bunch of .o files.

Then once all those individual files are compiled into those .o segments, you combine them all and link them together: g++ -o my_completed_project shape.o square.o cube.o .

(There may be a few typos in this: this was all off the top of my head... sorry about that.)

But all-in-all, your code looks very clean to me. It's well-structured, and easy to read. Great work!

Typically, a declaration only gives the structure of the function, so the compiler knows that it will have a specific return type, the name of the function, and the type and number of arguments. The compiler doesn't know what that particular function does ; it just wants to know that you're passing in what it's expecting, and that you expect a certain return type.

Declarations go into the .h files, and are very minimalistic: it tells nothing more than "what the function looks like."

Definitions, on the other hand, go into the .cpp files, and when you compile the program, it tells the program what to do once that function is called. The cube.cpp file now defines what the volume() function should (it should return width*area ). (它应该返回width*area )。

However, since cube derives from the square class, during compile time, all it cares about is that there is a getwidth() function defined in square , that it takes no parameters, and that it returns a double . It doesn't care how it gets the value for that double : just the fact that it is declared in the square.h is good enough for the compiler to have all the information it needs to compile cube.h and cube.cpp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM