zoukankan      html  css  js  c++  java
  • c++中多文件的组织

     参考书目:visual c++ 入门经典 第七版 Ivor Horton著 第八章

    根据书中例子学习使用类的多文件项目。

    首先要将类CBox定义成一个连贯的整体,在CBox.H文件中写入相关的类定义,在CBox.cpp 中写入类函数成员的代码。在CBox.cpp中要包含#include “CBox.H” ,而其他的库函数则要写在CBox.h中。

    然后我们可以定义多个类的相关文件。

    最后把他们共同组织在一个***.cpp中,这个文件里要有main函数,并且包含所有的.h文件 。

    例子:

    在一个解决方案中有如下文件

    Ex8_13.CPP

    Box.cpp

    Box.h

    BoxOperators.h

    代码片段如下

    //Ex8_13.CPP
    //main
    #include <iostream>
    #include"Box.h"
    #include"BoxOperators.h"
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        CBox candy{ 1.5, 1.0, 1.0 };
        CBox candyBox{ 1.5, 1.0, 1.0 };
        CBox carton{ 3, 2.0, 1.5 };
    
        int numCandies{ carton / candyBox };
        cout << "numCandies is : " << numCandies << endl;
    ......
    ......
    //Box.h
    #pragma once
    #include <algorithm>
    #include <utility>
    
    using std::rel_ops::operator<=;
    using std::rel_ops::operator>;
    using std::rel_ops::operator>=;
    using std::rel_ops::operator!=;
    
    class CBox
    {
    public:
        CBox();
        ~CBox();
        explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0);
    private:
        double m_Length;
        double m_Width;
        double m_Height;
    public:
        // less-than operator for CBox objects
        bool operator<(const CBox& aBox);
    
        double volume() const;
        
        bool operator==(const CBox & aBox) const;
        CBox operator+(const CBox & aBox);
        
        int operator/(const CBox & aBox) const;
    };
    //Box.cpp
    #include "Box.h"
    
    CBox::CBox()
    {
    }
    CBox::~CBox()
    {
    }
    CBox::CBox(double lv, double wv , double hv ) :
    m_Length{ lv }, m_Width{ wv }, m_Height{ hv }
    {
        if (m_Height > m_Length)
        {
            std::swap(m_Height, m_Length);
            std::swap(m_Width, m_Height);
        }
        else if (m_Height > m_Width)
        {
            std::swap(m_Height, m_Width);
        }
    }
    double CBox::volume() const
    {
        return m_Length*m_Height*m_Width;
    }
    // less-than operator for CBox objects
    bool CBox::operator<(const CBox& aBox)
    {
        return volume()<aBox.volume();
    }
    
    bool CBox::operator==(const CBox & aBox) const
    {
        return volume() == aBox.volume();
    }
    
    
    CBox CBox::operator+(const CBox & aBox)
    {
        return CBox(std::max(m_Length, aBox.m_Length), std::max(m_Width, aBox.m_Width), m_Height + aBox.m_Height);
    }
    
    
    int CBox::operator/(const CBox & aBox) const
    {
        return volume()/aBox.volume();
    }
    //BoxOperators.h
    //CBox operatoions that dont need to access private members 
    #pragma once
    #include "Box.h"
    
    inline bool operator>(const double value, const CBox & aBox)
    {
        return value > aBox.volume();
    }
    inline bool operator<(const double value, const CBox & aBox)
    {
        return value< aBox.volume();
    }
    inline bool operator>(const CBox & aBox, const double value)
    {
        return value < aBox.volume();
    }
    inline bool operator<( const CBox & aBox,const double value)
    {
        return value > aBox.volume();
    }
    ......
    ......

    总结:

    C++控制台程序包含了以下基本类型的文件:

    1. 包含库文件#include指令、全局常量和变量、类定义及函数原型的.h文件。换而言之,.h文件中包含了除可执行代码以外的一切。它们还包含内联函数的定义。当程序中有多个类定义的时候,通常将这些类定义放入不同的.h文件中去。
    2. 包含程序可执行代码的.cpp文件,其中还包含可执行代码所需全部定义的#include指令;
    3. 包含main函数 的另一个可执行的**.cpp.
  • 相关阅读:
    室内设计师招募中...
    winform控件部署于web中控件装载ie中
    Oracle10g在windows2003下双机热备安装
    购房风波(4)不了了之
    [原创]面向对象理解·抽象类和派生类理解和使用
    Infragistics NetAdvantage 2006 Volume 2 CLR 2.0曲折安装
    两个Javascript小tip
    PHP学习笔记之二
    C#开发语音机项目
    关于WebBrowser的DocumentText
  • 原文地址:https://www.cnblogs.com/simayuhe/p/5225611.html
Copyright © 2011-2022 走看看