zoukankan      html  css  js  c++  java
  • 命名空间和模块化编程,头文件

    #include <iostream>
    #include <string>
    #include "dynamic_memory.h"
    
    Company::Company(std::string theName)
    {
        name = theName;
    };
    
    void Company::printInfo()
    {
        std::cout << "this company's name is " << name << std::endl;
    };
    
    TechCompany::TechCompany(std::string theName, std::string theproduct) : Company(theName)
    {
        product = theproduct;
    };
    
    void TechCompany::printInfo()
    {
        std::cout << "this company's name is " << name << " his product is " << product << std::endl;
    }
    
    int main()
    {
        Company *company = new Company("Apple");
        company->printInfo();
    
        delete company;
        company = NULL;
    
        company = new TechCompany("Apple", "Iphone");
        company->printInfo();
    
        delete company;
        company = NULL;
    
    }
    /* vim: set ts=4 sw=4 sts=4 tw=100 */
    预处理命令,避免重复编译
    #ifndef DYNAMIC_MEMORY_H_INCLUDED 名字要保持一致,为了方便团队编程
    #define DYNAMIC_MEMORY_H_INCLUDED class Company { public: Company(std::string theName); virtual void printInfo(); protected: std::string name; }; class TechCompany : public Company { public: TechCompany(std::string theName, std::string product); virtual void printInfo(); private: std::string product; }; #endif // DYNAMIC_MEMORY_H_INCLUDED
    命名空间
    #include <iostream> using namespace std; namespace a { void print(); void print(){ cout << "hello world from namespace a" << endl; } } namespace b{ void print(); void print(){ cout << "hello world from namespace b" << endl; } } int main() { a::print(); b::print();
       std:cout << "hello" << std:endl; 提倡
    using namespace std; 整个命名空间全局化违背了命名空间的设计意图。不提倡
    using std::cout 只给cout全局性, OK
    只放在某个函数里,那么它只能在这一个函数里使用。
    }
  • 相关阅读:
    angular 路由请求js文件
    WeX5的简单介绍及UI的简单讲解
    JAVA 反射
    JAVA 线程
    JAVA 远程通讯机制
    用Java实现几种常见的排序算法
    自动完成
    Springmvc和poi3.9导出excel并弹出下载框
    Windows Server 搭建企业无线认证(NPS搭建)
    Windows Server 搭建企业无线认证(Radius认证方案)
  • 原文地址:https://www.cnblogs.com/i80386/p/4358270.html
Copyright © 2011-2022 走看看