C ++新手。使用CodeBlocks我如何将以下代码分解为头文件(.h)和实现文件(.cpp)与主?

时间:2015-12-03 19:13:40

标签: c++

我知道我应该在不同的课程中将其分开,但除了告诉我这样做之外,它并没有告诉我如何做。我感到完全迷失了。

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

class CArea {
private:
    string shape;
public:
    CArea(const string& strShape) : shape(strShape) {}
    void display_choice() {cout << "You have chosen " << shape;}
    void output(const float& result) const
        {
        cout << "The area of the " << shape << " is: " <<
                setprecision(2) << fixed << result << endl;
        }
    virtual void get_area() = 0;
    template <class T>
    static T get_number(const string& cstrPrompt, const T& ctMin = 0, const T& ctMax = 0)
        {
        T tRet = 0;
        string strBuffer;
        do {
            cout << cstrPrompt;
            getline(cin, strBuffer);
            stringstream ss(strBuffer);
            if (!(ss >> tRet)) {
                cout << "Not a valid number!" << endl;
                continue;
                }
            else if (ctMin || ctMax) {
                if ((tRet < ctMin) || (tRet > ctMax)){
                    cout << "Range: " << ctMin << " <= x <= " << ctMax << endl;
                    }
                else {
                    break;
                    }
                }
            else {
                break;
                }
            }while(true);
        return tRet;
        }
    };
class Circle : public CArea {
private:
    float radius;
public:
    Circle() : CArea("Circle"), radius(0.0f) {}
    void get_area()
        {
        display_choice();
        radius = get_number<float>("\nPlease enter Radius> ");
        output(radius * radius * 3.14f);
        }
    };
class Square : public CArea {
private:
    float side;
public:
    Square() : CArea("Square"), side(0) {}
    void get_area()
        {
        display_choice();
        side = get_number<float>("\nPlease enter side> "),
        output(side * side);
        }
    };

我知道主要的是它自己的部分也是正确的吗?

 int main()
    {
    const string strMenu = "\n\n********* MENU ***********\n"
                         "\t1. Circle\n"
                         "\t2. Square\n"
                         "\t3. Exit\n"
                         "\tCHOICE> ";
    int choice;
    Circle c;
    Square s;

    do {
        choice = CArea::get_number<int>(strMenu, 1, 3);

        if (choice == 1)
            c.get_area();
        else if (choice == 2)
            s.get_area();
                        else
            break;
        } while (true);
    cout << "Thank you for using the program !" << endl;
    return 0;
    }

0 个答案:

没有答案