1.在Qt环境新建non-Qt项目写出练习代码并运行通过。
当然,我们也可以使用程序开头的预编译指示来使用命名空间中的名字。使用预编译指示的好处在于在程序中不必显式地使用命名空间限制符来访问变量。以上主程序可修改为:
但第二个变量必需用命名空间限制符来访问,因为此时myown1空间中的变量已经可见,如果不加限制,编译器就会无法识别是那一个命名空间中的变量。这一点一定要注意。 以上只是初学者不清楚的一个概念,在以后的文章中还将继续讨论其它的一些概念。
#include <iostream> #include<string> using namespace std; inline void test(const char*title,bool value){ cout<<title<<"returns "<<(value?"true":"false")<<endl; } int main(int argc, char *argv[]) { string s1="DEF"; cout<<"s1 is"<<s1<<endl; string s2; cout<<"Please enter s2:"; cin>>s2; cout<<"length osf s2:"<<s2.length()<<endl; test("s1<=\"ABC\"",s1<="ABC"); test("\"DEF\"<=s1","DEF"<=s1); s2+=s1; cout<<"s2=s2+s1:"<<s2<<endl; cout<<"length of s2:"<<s2.length()<<endl; return 0; }
#include <iostream> using namespace std; enum GameResult{WIN,LOSE,TIE,CANCEL}; int main(int argc, char *argv[]) { GameResult result; enum GameResult omit=CANCEL; for(int count = WIN;count <=CANCEL;count++){ result=GameResult(count); if(result==omit) cout<<"The game was cancelled"<<endl; else{ cout<<"The game was played "; if(result==0) cout<<"and we won!"; if(result==1) cout<<"and we lost."; if(result==2) cout<<"and we tie."; cout<<endl; } } return 0; }
#include<iostream> using namespace std; class Clock{ public: void setTime(int newH=0,int newM=0,int newS=0); void showTime(); private: int hour,minute,second; }; void Clock::setTime(int newH, int newM, int newS){ hour=newH; minute=newM; second=newS; } inline void Clock::showTime(){ cout<<hour<<":"<<minute<<":"<<second<<endl; } int main(){ Clock myClock; cout<<"First time set and output:"<<endl; myClock.setTime(); myClock.showTime(); cout<<"Second time set and outut:"<<endl; myClock.setTime(8,30,30); myClock.showTime(); return 1; }
#include <iostream> using namespace std; class Base1{ public: void display() const {cout<<"Base1::display()"<<endl;} }; class Base2 :public Base1{ public: void display() const {cout<<"Base2::display()"<<endl;} }; class Derived :public Base2{ public: void display() const {cout<<"Derived::display()"<<endl;} }; void fun(Base1 *ptr){ ptr->display(); } int main(int argc, char *argv[]) { Base1 base1; Base2 base2; Derived derived; fun(&base1); fun(&base2); fun(&derived); }
#include<iostream> using namespace std; class Clock{ public: void setTime(int newH=0,int newM=0,int newS=0); void showTime(); private: int hour,minute,second; }; void Clock::setTime(int newH, int newM, int newS){ hour=newH; minute=newM; second=newS; } inline void Clock::showTime(){ cout<<hour<<":"<<minute<<":"<<second<<endl; } int main(){ Clock myClock; cout<<"First time set and output:"<<endl; myClock.setTime(); myClock.showTime(); cout<<"Second time set and output:"<<endl; myClock.setTime(8,30,30); myClock.showTime(); return 1; }
2.#include语句中,引用头文件时使用<和"的区别;
#incluce ""格式:
引用非标准库的头文件,编译器从用户的工作目录开始搜索。双引号表示先在程序源文件所在目录查找,如果未找到则去系统默认目录查找,通常用于包含程序作者编写的头文件;尖括号表示只在系统默认目录或者括号内的路径查找,通常用于包含系统中自带的头文件。
尖括号:在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找;
双引号:首先在当前的源文件目录中查找,若未找到才到包含目录中去查找。
归纳namespace关键字的用法
C++中采用的是单一的全局变量命名空间。在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突。当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字;有时为了程序的方便,必需使用同一名字。比如你定义了一个变量string user_name, 有可能在你调用的某个库文件或另外的程序代码中也定义了相同名字的变量,这就会出现冲突。命名空间就是为解决C++中的变量、函数的命名冲突而服务的。解决的办法就是将你的strTemp变量定义在一个不同名字的命名空间中。就好像张家有电视机,李家也有同样型号的电视机,但我们能区分清楚,就是因为他们分属不同的家庭。
#include <iostream> #include <string> using namespace std; //using namespace编译指示,使在C++标准类库中定义的名字在本程序中可以使用 //否则,iostream,string 等c++标准类就不可见了,编译就会出错。 //两个在不同命名空间中定义的名字相同的变量 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; }
当然,我们也可以使用程序开头的预编译指示来使用命名空间中的名字。使用预编译指示的好处在于在程序中不必显式地使用命名空间限制符来访问变量。以上主程序可修改为:
int main() { using namespace myown1; cout<< "/n" << "Hello, " << user_name << "... and goodbye!/n"; // using namespace myown2; cout<< "/n" << "Hello, " << myown2::user_name //用命名空间限制符myown2访问变量user_name << "... and goodbye!/n"; return 0; }
但第二个变量必需用命名空间限制符来访问,因为此时myown1空间中的变量已经可见,如果不加限制,编译器就会无法识别是那一个命名空间中的变量。这一点一定要注意。 以上只是初学者不清楚的一个概念,在以后的文章中还将继续讨论其它的一些概念。
#include<iostream> using namespace std; namespace Cui{ void Love() { cout<<"He love a wonderful girl, and her name is SHILI"<<endl; } } using namespace Cui; int main() { int a=10; cout<<a<<endl; Love(); Cui::Love();//调用方式2 return 0; }