Windows编程博客

1.include< > 和 include " "的区别

用"<>"将要包含的文件的文件名括起来。这种方法指示预处理程序到预定义的缺省路径下寻找文件。预定义的缺省路径通常是在INCLUDE环境变量中指定的。

用双引号将要包含的文件的文件名括起来。这种方法指示预处理程序先到当前目录下寻找文件,再到预定义的缺省路径下寻找文件。


2.namespace用法举例

#include <iostream>
#include <string>
using namespace std;

namespace myown1{
    string user_name = "myown1";
}
namespace myown2{
    string user_name = "myown2";
}
int main()
{
    cout<< "\n"
    << "Hello, "
    << myown1::user_name //用命名空间限制符myown1访问变量user_name
    << "... and goodbye!\n";
    cout<< "\n"
    << "Hello, "
    << myown2::user_name //用命名空间限制符myown2访问变量user_name
    << "... and goodbye!\n";
    return 0;
}

3.前置声明用法举例

//a.h
#ifndef A_H
#define A_H
#include <iostream>
using namespace std;
class b;      //前置声明
class a
{
public:
    b *ib;      //注意是指针类型
    void putA()
    {
        cout<<"这是A类"<<endl;
    }
};
#endif // A_H

//b.h
#ifndef B_H
#define B_H
#include <iostream>
#include "a.h"
using namespace std;
class b
{
public:
    a ia;     //因为前面有include,所以声明成a类
    void putB()
    {
        cout<<"这是B类"<<endl;
    }
};
#endif // B_H
//main.cpp
#include <stdio.h>
#include "b.h"     //b头文件里包含了a头文件,所以就只用包含b.h就行
int main()
{
    b B;
    B.putB();
    B.ia.putA();
    getchar();
    return 0;
}

4.一个例子

//father.h
#ifndef FATHER_H
#define FATHER_H
#include <string>
#include "child.h"
using namespace std;
class Father
{
public:
    Father();
    string name;
    Child child;
    void callChild();
    void answer();
};
#endif // FATHER_H

//child.h
#ifndef CHILD_H
#define CHILD_H

#include <string>
using namespace std;

class Father;     //前置声明
class Child
{
public:
    Child();
    string name;
    Father *father;    //注意这里要声明一个指针类型
    void answer();
    void callFather();
};
#endif // CHILD_H

//father.cpp
#include "father.h"
#include <iostream>
Father::Father()
    :name("Lao Hua")
{
}
void Father::callChild(){
    cout<<endl<<"I am calling my child!";
    child.answer();
}
void Father::answer(){
    cout<<endl<<name<<" is here waiting for you!";
}
//child.cpp

#include "child.h"
#include "father.h"     //两个头文件都包含了
#include "iostream"
Child::Child():name("xiao Hua")
{
}
void Child::answer(){
    cout<<endl<<name<<" is here!";
}
void Child::callFather(){
    father=new Father();         //注意这里要声明
    cout<<endl<<"I am calling my father!";
    father->answer();
}

//main.cpp
#include <QCoreApplication>
#include "father.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Father baba;
    Child erzi;
    baba.callChild();
    cout<<endl;
    erzi.callFather();
    cout<<endl;
    return a.exec();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值