C++快速入门国外大神的教程.docx
- 文档编号:2810136
- 上传时间:2022-11-15
- 格式:DOCX
- 页数:26
- 大小:26.58KB
C++快速入门国外大神的教程.docx
《C++快速入门国外大神的教程.docx》由会员分享,可在线阅读,更多相关《C++快速入门国外大神的教程.docx(26页珍藏版)》请在冰豆网上搜索。
C++快速入门国外大神的教程
C++isasystemsprogramminglanguagethat, accordingtoitsinventorBjarneStroustrup,wasdesignedto
∙bea"betterC"
∙supportdataabstraction
∙supportobject-orientedprogramming
∙supportgenericprogramming
Thoughitssyntaxcanbemoredifficultorcomplexthannewerlanguages,itiswidelyusedbecauseitcompilestonativeinstructionsthatcanbedirectlyrunbytheprocessorandofferstightcontroloverhardware(likeC)whileofferinghigh-levelfeaturessuchasgenerics,exceptions,andclasses.ThiscombinationofspeedandfunctionalitymakesC++oneofthemostwidely-usedprogramminglanguages.
//////////////////
//ComparisontoC
//////////////////
//C++is_almost_asupersetofCandsharesitsbasicsyntaxfor
//variabledeclarations,primitivetypes,andfunctions.
//JustlikeinC,yourprogram'sentrypointisafunctioncalled
//mainwithanintegerreturntype.
//Thisvalueservesastheprogram'sexitstatus.
//Seehttp:
//en.wikipedia.org/wiki/Exit_statusformoreinformation.
intmain(intargc,char**argv)
{
//Commandlineargumentsarepassedinbyargcandargvinthesameway
//theyareinC.
//argcindicatesthenumberofarguments,
//andargvisanarrayofC-stylestrings(char*)
//representingthearguments.
//Thefirstargumentisthenamebywhichtheprogramwascalled.
//argcandargvcanbeomittedifyoudonotcareaboutarguments,
//givingthefunctionsignatureofintmain()
//Anexitstatusof0indicatessuccess.
return0;
}
//However,C++variesinsomeofthefollowingways:
//InC++,characterliteralsarechars
sizeof('c')==sizeof(char)==1
//InC,characterliteralsareints
sizeof('c')==sizeof(int)
//C++hasstrictprototyping
voidfunc();//functionwhichacceptsnoarguments
//InC
voidfunc();//functionwhichmayacceptanynumberofarguments
//UsenullptrinsteadofNULLinC++
int*ip=nullptr;
//CstandardheadersareavailableinC++,
//butareprefixedwith"c"andhaveno.hsuffix.
#include
intmain()
{
printf("Hello,world!
\n");
return0;
}
///////////////////////
//Functionoverloading
///////////////////////
//C++supportsfunctionoverloading
//providedeachfunctiontakesdifferentparameters.
voidprint(charconst*myString)
{
printf("String%s\n",myString);
}
voidprint(intmyInt)
{
printf("Myintis%d",myInt);
}
intmain()
{
print("Hello");//Resolvestovoidprint(constchar*)
print(15);//Resolvestovoidprint(int)
}
/////////////////////////////
//Defaultfunctionarguments
/////////////////////////////
//Youcanprovidedefaultargumentsforafunction
//iftheyarenotprovidedbythecaller.
voiddoSomethingWithInts(inta=1,intb=4)
{
//Dosomethingwiththeintshere
}
intmain()
{
doSomethingWithInts();//a=1,b=4
doSomethingWithInts(20);//a=20,b=4
doSomethingWithInts(20,5);//a=20,b=5
}
//Defaultargumentsmustbeattheendoftheargumentslist.
voidinvalidDeclaration(inta=1,intb)//Error!
{
}
/////////////
//Namespaces
/////////////
//Namespacesprovideseparatescopesforvariable,function,
//andotherdeclarations.
//Namespacescanbenested.
namespaceFirst{
namespaceNested{
voidfoo()
{
printf("ThisisFirst:
:
Nested:
:
foo\n");
}
}//endnamespaceNested
}//endnamespaceFirst
namespaceSecond{
voidfoo()
{
printf("ThisisSecond:
:
foo\n");
}
}
voidfoo()
{
printf("Thisisglobalfoo\n");
}
intmain()
{
//IncludesallsymbolsfromnamespaceSecondintothecurrentscope.Note
//thatsimplyfoo()nolongerworks,sinceitisnowambiguouswhether
//we'recallingthefooinnamespaceSecondorthetoplevel.
usingnamespaceSecond;
Second:
:
foo();//prints"ThisisSecond:
:
foo"
First:
:
Nested:
:
foo();//prints"ThisisFirst:
:
Nested:
:
foo"
:
:
foo();//prints"Thisisglobalfoo"
}
///////////////
//Input/Output
///////////////
//C++inputandoutputusesstreams
//cin,cout,andcerrrepresentstdin,stdout,andstderr.
//<
#include
usingnamespacestd;//Streamsareinthestdnamespace(standardlibrary)
intmain()
{
intmyInt;
//Printstostdout(orterminal/screen)
cout<<"Enteryourfavoritenumber:
\n";
//Takesininput
cin>>myInt;
//coutcanalsobeformatted
cout<<"Yourfavoritenumberis"< //prints"Yourfavoritenumberis cerr<<"Usedforerrormessages"; } ////////// //Strings ////////// //StringsinC++areobjectsandhavemanymemberfunctions #include usingnamespacestd;//Stringsarealsointhenamespacestd(standardlibrary) stringmyString="Hello"; stringmyOtherString="World"; //+isusedforconcatenation. cout<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 快速 入门 国外 大神 教程