ImageVerifierCode 换一换
格式:PDF , 页数:24 ,大小:109.24KB ,
资源ID:2492400      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/2492400.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(C++快速入门——国外大神的教程.pdf)为本站上传会员【w****g】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

C++快速入门——国外大神的教程.pdf

1、C+is a systems programming language that,according to its inventor Bjarne Stroustrup,was designed tobe a better Csupport data abstractionsupport object-oriented programmingsupport generic programmingThough its syntax can be more difficult or complex than newer languages,it is widely used because it

2、compiles to native instructions that can be directly run by the processor and offers tight control over hardware(like C)while offering high-level features such as generics,exceptions,and classes.This combination of speed and functionality makes C+one of the most widely-used programming languages./Co

3、mparison to C/C+is _almost_ a superset of C and shares its basic syntax for/variable declarations,primitive types,and functions./Just like in C,your programs entry point is a function called/main with an integer return type./This value serves as the programs exit status./See http:/en.wikipedia.org/w

4、iki/Exit_status for more information.int main(int argc,char*argv)/Command line arguments are passed in by argc and argv in the same way /they are in C./argc indicates the number of arguments,/and argv is an array of C-style strings(char*)/representing the arguments./The first argument is the name by

5、 which the program was called./argc and argv can be omitted if you do not care about arguments,/giving the function signature of int main()/An exit status of 0 indicates success.return 0;/However,C+varies in some of the following ways:/In C+,character literals are charssizeof(c)=sizeof(char)=1/In C,

6、character literals are intssizeof(c)=sizeof(int)/C+has strict prototypingvoid func();/function which accepts no arguments/In Cvoid func();/function which may accept any number of arguments/Use nullptr instead of NULL in C+int*ip=nullptr;/C standard headers are available in C+,/but are prefixed with

7、c and have no.h suffix.#include int main()printf(Hello,world!n);return 0;/Function overloading/C+supports function overloading/provided each function takes different parameters.void print(char const*myString)printf(String%sn,myString);void print(int myInt)printf(My int is%d,myInt);int main()print(He

8、llo);/Resolves to void print(const char*)print(15);/Resolves to void print(int)/Default function arguments/You can provide default arguments for a function/if they are not provided by the caller.void doSomethingWithInts(int a=1,int b=4)/Do something with the ints hereint main()doSomethingWithInts();

9、/a=1,b=4 doSomethingWithInts(20);/a=20,b=4 doSomethingWithInts(20,5);/a=20,b=5/Default arguments must be at the end of the arguments list.void invalidDeclaration(int a=1,int b)/Error!/Namespaces/Namespaces provide separate scopes for variable,function,/and other declarations./Namespaces can be neste

10、d.namespace First namespace Nested void foo()printf(This is First:Nested:foon);/end namespace Nested/end namespace Firstnamespace Second void foo()printf(This is Second:foon);void foo()printf(This is global foon);int main()/Includes all symbols from namespace Second into the current scope.Note /that

11、 simply foo()no longer works,since it is now ambiguous whether /were calling the foo in namespace Second or the top level.using namespace Second;Second:foo();/prints This is Second:foo First:Nested:foo();/prints This is First:Nested:foo :foo();/prints This is global foo/Input/Output/C+input and outp

12、ut uses streams/cin,cout,and cerr represent stdin,stdout,and stderr./is the extraction operator.#include /Include for I/O streamsusing namespace std;/Streams are in the std namespace(standard library)int main()int myInt;/Prints to stdout(or terminal/screen)cout myInt;/cout can also be formatted cout

13、 Your favorite number is myInt n;/prints Your favorite number is cerr Used for error messages;/Strings/Strings in C+are objects and have many member functions#include using namespace std;/Strings are also in the namespace std(standard library)string myString=Hello;string myOtherString=World;/+is use

14、d for concatenation.cout myString+myOtherString;/Hello Worldcout myString+You;/Hello You/C+strings are mutable and have value semantics.myString.append(Dog);cout myString;/Hello Dog/References/In addition to pointers like the ones in C,/C+has _references_./These are pointer types that cannot be reas

15、signed once set/and cannot be null./They also have the same syntax as the variable itself:/No*is needed for dereferencing and/&(address of)is not used for assignment.using namespace std;string foo=I am foo;string bar=I am bar;string&fooRef=foo;/This creates a reference to foo.fooRef+=.Hi!;/Modifies

16、foo through the referencecout fooRef;/Prints I am foo.Hi!/Doesnt reassign fooRef.This is the same as foo=bar,and/foo=I am bar/after this line.cout&fooRef endl;/Prints the address of foofooRef=bar;cout&fooRef endl;/Still prints the address of foocout fooRef;/Prints I am bar/The address of fooRef rema

17、ins the same,i.e.it is still referring to foo.const string&barRef=bar;/Create a const reference to bar./Like C,const values(and pointers and references)cannot be modified.barRef+=.Hi!;/Error,const references cannot be modified./Sidetrack:Before we talk more about references,we must introduce a conce

18、pt/called a temporary object.Suppose we have the following code:string tempObjectFun().string retVal=tempObjectFun();/What happens in the second line is actually:/-a string object is returned from tempObjectFun/-a new string is constructed with the returned object as argument to the/constructor/-the

19、 returned object is destroyed/The returned object is called a temporary object.Temporary objects are/created whenever a function returns an object,and they are destroyed at the/end of the evaluation of the enclosing expression(Well,this is what the/standard says,but compilers are allowed to change t

20、his behavior.Look up/return value optimization if youre into this kind of details).So in this/code:foo(bar(tempObjectFun()/assuming foo and bar exist,the object returned from tempObjectFun is/passed to bar,and it is destroyed before foo is called./Now back to references.The exception to the at the e

21、nd of the enclosing/expression rule is if a temporary object is bound to a const reference,in/which case its life gets extended to the current scope:void constReferenceTempObjectFun()/constRef gets the temporary object,and it is valid until the end of this /function.const string&constRef=tempObjectF

22、un();./Another kind of reference introduced in C+11 is specifically for temporary/objects.You cannot have a variable of its type,but it takes precedence in/overload resolution:void someFun(string&s)./Regular referencevoid someFun(string&s)./Reference to temporary objectstring foo;someFun(foo);/Calls

23、 the version with regular referencesomeFun(tempObjectFun();/Calls the version with temporary reference/For example,you will see these two versions of constructors for/std:basic_string:basic_string(const basic_string&other);basic_string(basic_string&other);/Idea being if we are constructing a new str

24、ing from a temporary object(which/is going to be destroyed soon anyway),we can have a more efficient/constructor that salvages parts of that temporary string.You will see this/concept referred to as move semantics./Enums/Enums are a way to assign a value to a constant most commonly used for/easier v

25、isualization and reading of codeenum ECarTypes Sedan,Hatchback,SUV,Wagon;ECarTypes GetPreferredCarType()return ECarTypes:Hatchback;/As of C+11 there is an easy way to assign a type to the enum which can be/useful in serialization of data and converting enums back-and-forth between/the desired type a

26、nd their respective constantsenum ECarTypes:uint8_t Sedan,/0 Hatchback,/1 SUV=254,/254 Hybrid/255;void WriteByteToFile(uint8_t InputValue)/Serialize the InputValue to a filevoid WritePreferredCarTypeToFile(ECarTypes InputCarType)/The enum is implicitly converted to a uint8_t due to its declared enum

27、 type WriteByteToFile(InputCarType);/On the other hand you may not want enums to be accidentally cast to an integer/type or to other enums so it is instead possible to create an enum class which/wont be implicitly convertedenum class ECarTypes:uint8_t Sedan,/0 Hatchback,/1 SUV=254,/254 Hybrid/255;vo

28、id WriteByteToFile(uint8_t InputValue)/Serialize the InputValue to a filevoid WritePreferredCarTypeToFile(ECarTypes InputCarType)/Wont compile even though ECarTypes is a uint8_t due to the enum /being declared as an enum class!WriteByteToFile(InputCarType);/Classes and object-oriented programming/Fi

29、rst example of classes#include/Declare a class./Classes are usually declared in header(.h or.hpp)files.class Dog /Member variables and functions are private by default.std:string name;int weight;/All members following this are public/until private:or protected:is found.public:/Default constructor Do

30、g();/Member function declarations(implementations to follow)/Note that we use std:string here instead of placing /using namespace std;/above./Never put a using namespace statement in a header.void setName(const std:string&dogsName);void setWeight(int dogsWeight);/Functions that do not modify the sta

31、te of the object /should be marked as const./This allows you to call them if given a const reference to the object./Also note the functions must be explicitly declared as _virtual_ /in order to be overridden in derived classes./Functions are not virtual by default for performance reasons.virtual voi

32、d print()const;/Functions can also be defined inside the class body./Functions defined as such are automatically inlined.void bark()const std:cout name barks!n;/Along with constructors,C+provides destructors./These are called when an object is deleted or falls out of scope./This enables powerful par

33、adigms such as RAII /(see below)/The destructor should be virtual if a class is to be derived from;/if it is not virtual,then the derived class destructor will /not be called if the object is destroyed through a base-class reference /or pointer.virtual Dog();/A semicolon must follow the class defini

34、tion./Class member functions are usually implemented in.cpp files.Dog:Dog()std:cout A dog has been constructedn;/Objects(such as strings)should be passed by reference/if you are modifying them or const reference if you are not.void Dog:setName(const std:string&dogsName)name=dogsName;void Dog:setWeig

35、ht(int dogsWeight)weight=dogsWeight;/Notice that virtual is only needed in the declaration,not the definition.void Dog:print()const std:cout Dog is name and weighs weight kgn;Dog:Dog()cout Goodbye name n;int main()Dog myDog;/prints A dog has been constructed myDog.setName(Barkley);myDog.setWeight(10

36、);myDog.print();/prints Dog is Barkley and weighs 10 kg return 0;/prints Goodbye Barkley/Inheritance:/This class inherits everything public and protected from the Dog class/as well as private but may not directly access private members/methods/without a public or protected method for doing soclass O

37、wnedDog:public Dog void setOwner(const std:string&dogsOwner);/Override the behavior of the print function for all OwnedDogs.See /http:/en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping /for a more general introduction if you are unfamiliar with /subtype polymorphism./The override keywo

38、rd is optional but makes sure you are actually /overriding the method in a base class.void print()const override;private:std:string owner;/Meanwhile,in the corresponding.cpp file:void OwnedDog:setOwner(const std:string&dogsOwner)owner=dogsOwner;void OwnedDog:print()const Dog:print();/Call the print

39、function in the base Dog class std:cout Dog is owned by owner n;/Prints Dog is and weights /Dog is owned by/Initialization and Operator Overloading/In C+you can overload the behavior of operators such as+,-,*,/,etc./This is done by defining a function which is called/whenever the operator is used.#i

40、nclude using namespace std;class Point public:/Member variables can be given default values in this manner.double x=0;double y=0;/Define a default constructor which does nothing /but initialize the Point to the default value(0,0)Point();/The following syntax is known as an initialization list /and i

41、s the proper way to initialize class member values Point(double a,double b):x(a),y(b)/*Do nothing except initialize the values*/Overload the+operator.Point operator+(const Point&rhs)const;/Overload the+=operator Point&operator+=(const Point&rhs);/It would also make sense to add the-and-=operators,/b

42、ut we will skip those for brevity.;Point Point:operator+(const Point&rhs)const /Create a new point that is the sum of this one and rhs.return Point(x+rhs.x,y+rhs.y);Point&Point:operator+=(const Point&rhs)x+=rhs.x;y+=rhs.y;return*this;int main()Point up(0,1);Point right(1,0);/This calls the Point+ope

43、rator /Point up calls the+(function)with right as its parameter Point result=up+right;/Prints Result is upright(1,1)cout Result is upright(result.x ,result.y )n;return 0;/Templates/Templates in C+are mostly used for generic programming,though they are/much more powerful than generic constructs in ot

44、her languages.They also/support explicit and partial specialization and functional-style type/classes;in fact,they are a Turing-complete functional language embedded/in C+!/We start with the kind of generic programming you might be familiar with.To/define a class or function that takes a type parame

45、ter:templateclass Box public:/In this class,T can be used as any other type.void insert(const T&).;/During compilation,the compiler actually generates copies of each template/with parameters substituted,so the full definition of the class must be/present at each invocation.This is why you will see t

46、emplate classes defined/entirely in header files./To instantiate a template class on the stack:Box intBox;/and you can use it as you would expect:intBox.insert(123);/You can,of course,nest templates:BoxBox boxOfBox;boxOfBox.insert(intBox);/Until C+11,you had to place a space between the two s,otherw

47、ise/would be parsed as the right shift operator./You will sometimes see/template/instead.The class keyword and typename keywords are _mostly_/interchangeable in this case.For the full explanation,see/http:/en.wikipedia.org/wiki/Typename/(yes,that keyword has its own Wikipedia page)./Similarly,a temp

48、late function:templatevoid barkThreeTimes(const T&input)input.bark();input.bark();input.bark();/Notice that nothing is specified about the type parameters here.The compiler/will generate and then type-check every invocation of the template,so the/above function works with any type T that has a const

49、 bark method!Dog fluffy;fluffy.setName(Fluffy)barkThreeTimes(fluffy);/Prints Fluffy barks three times./Template parameters dont have to be classes:templatevoid printMessage()cout Learn C+in Y minutes!endl;/And you can explicitly specialize templates for more efficient code.Of/course,most real-world

50、uses of specialization are not as trivial as this./Note that you still need to declare the function(or class)as a template/even if you explicitly specified all parameters.templatevoid printMessage()cout Learn C+faster in only 10 minutes!endl;printMessage();/Prints Learn C+in 20 minutes!printMessage(

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服