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
    只放在某个函数里,那么它只能在这一个函数里使用。
    }
  • 相关阅读:
    解决CentOS 7 history命令不显示操作记录的时间和用户身份问题
    CentOS7关闭selinux
    Centos7下添加开机自启动服务和脚本
    快速查看一个文件的权限 stat -c %a
    修改centos7系统语言
    sudo
    chsh命令 修改用户登录shell
    忘记root开机密码及怎样开启密码远程连接模式
    centos7系统中添加 pstree 命令
    vim 多行添加注释,取消注释
  • 原文地址:https://www.cnblogs.com/i80386/p/4358270.html
Copyright © 2011-2022 走看看