zoukankan      html  css  js  c++  java
  • 头文件和宏

    头文件:

    Person.h

     1 #pragma once // 只编译一次  //  解决头文件 重复包含
     2 
     3 class CPerson
     4 {
     5 public:
     6     int m_nAge;
     7     const int m_nSex;
     8     static int m_nName;
     9 public:
    10     CPerson(void);
    11     ~CPerson(void);
    12 public:
    13     void Show();
    14     void AA() const;
    15     static void BB();
    16     virtual void CC();
    17     virtual void DD()=0;
    18 };

    Son.h

     1 #pragma once
     2 #include "person.h"
     3 class CSon :
     4     public CPerson
     5 {
     6 public:
     7     CSon(void);
     8     ~CSon(void);
     9 public:
    10     virtual void DD();
    11 };

    源文件:

    Person.cpp

     1 #include "Person.h"
     2 #include <iostream>
     3 using namespace std;
     4 
     5 int CPerson::m_nName = 100;  //  static成员变量 在 .cpp  中初始化
     6 
     7 CPerson::CPerson(void):m_nSex(12)
     8 {
     9     m_nAge = 0;
    10 }
    11 
    12 CPerson::~CPerson(void)
    13 {
    14 }
    15 void CPerson::Show()
    16 {
    17     cout << m_nAge << endl;
    18 }
    19 void CPerson::AA() const   // const 不能删除
    20 {
    21     cout << "AA" << endl;
    22 }
    23 void CPerson::BB()
    24 {
    25     cout << "BB" << endl;
    26 }
    27 void CPerson::CC()
    28 {
    29     cout << "CC" << endl;
    30 }

    Son.cpp

     1 #include "Son.h"
     2 
     3 CSon::CSon(void)
     4 {
     5 }
     6 
     7 CSon::~CSon(void)
     8 {
     9 }
    10 void CSon::DD()
    11 {
    12     
    13 }

    主函数:

     1 #include<iostream>
     2 #include "Son.h"
     3 using namespace std;
     4 
     5 //    函数实现要有  类名::
     6 //    const函数的实现要有 关键字
     7 //    static ,virtual 函数的实现不要关键字
     8 //    static 变量在源文件中初始化
     9 
    10 int main()
    11 {
    12     CSon ps;
    13     ps.Show();
    14     ps.AA();
    15     ps.BB();
    16     ps.CC();
    17 
    18     system("pause");
    19     return 0;
    20 }

    宏:

    def.h

     1 //   和下一行连接   后面不能有任何字符
     2 #define    AAAA()
     3     for(int i=0;i<10;i++)
     4     {
     5         cout << i << " " ;
     6     }
     7 
     8 //  宏的参  也是替换.    ## 拼接的,文本的拼接
     9 #define    BBBB(ThisClass)
    10     ThisClass ps##ThisClass;
    11     ps##ThisClass.Show();
    12 
    13 //   # 代表字符串
    14 #define CCCC(STR) cout << #STR << endl;

    main.cpp

     1 #include<iostream>
     2 #include"def.h"
     3 #include "Person.h"
     4 #include "People.h"
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10 
    11     AAAA()
    12 
    13     BBBB(CPerson)
    14     //CPerson psCPerson;
    15     //psCPerson.Show();
    16 
    17     BBBB(CPeople)
    18     //CPeople psCPeople;
    19     //psCPeople.Show();
    20 
    21     CCCC(CPerson)
    22     CCCC(CPeople)
    23 
    24     system("pause");
    25     return 0;
    26 }

    结果如图:

    #ifndef和#ifdef

     1 #ifndef _DEF_H_  //如果没有定义_DEF_H_则定义
     2 #define _DEF_H_
     3 class CPerson
     4 {
     5 
     6 };
     7 #endif//_DEF_H_
     8 
     9 
    10 
    11 #include <windows.h>
    12 void MessageBoxA(char* p)
    13 {
    14 
    15 }
    16 void MessageBoxW(wchar_t* p)
    17 {
    18 
    19 }
    20 
    21 #ifdef UNICODE//宽字符集
    22     #define MessageBox MessageBoxW
    23 #else
    24     #define MessageBox MessageBoxA
    25 #endif
  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/7944473.html
Copyright © 2011-2022 走看看