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
    只放在某个函数里,那么它只能在这一个函数里使用。
    }
  • 相关阅读:
    clr via c# 定制特性
    clr via c# delegate
    clr via c# Array2
    clr from c# 字符 ,字符串 和 文本处理
    clr via c# 接口
    clr via c# 泛型
    clr via c# 事件
    clr via c# 参数和属性
    程序设计入门——C语言 第6周编程练习 2 完数(5分)
    程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)
  • 原文地址:https://www.cnblogs.com/i80386/p/4358270.html
Copyright © 2011-2022 走看看