欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Built-In (a.k.a. Native) Types in C++ .ppt

    • 资源ID:379160       资源大小:168.50KB        全文页数:17页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Built-In (a.k.a. Native) Types in C++ .ppt

    1、Built-In (a.k.a. Native) Types in C+,int, long, short, char (signed, integer division) unsigned versions too unsigned int, unsigned long, etc. C+ guarantees a char is one byte in size Sizes of other types are platform dependent Can determine using sizeof() , INT_MAX float, double (floating point div

    2、ision) More expensive in space and time Useful when you need to describe continuous quantities bool type Logic type, takes on values true, false,User (& Library) DefinedTypes in C+,enumerations enum primary_color red, blue, yellow;functions and operators For example, things called from main function

    3、structs and classes Similar abstractions in C+, extend C structs,Comparing C+ Classes and Structs,struct My_Data My_Data (int i) : x_(i) int x_; ;class My_Object public:My_Object ();My_Object ();private:int y_; ;,Struct members are public by default Class members are private by default Both can have

    4、 Constructors Destructors Member variables Member functions Common practice: use structs for data use classes for objects with non-trivial methods,More About Both Native and User Types,Pointers raw memory address of an object or variable its type constrains what types it can point to (more later) ca

    5、n take on a value of 0 (not pointing to anything) References “alias” for an object or variable its type constrains what types it can refer to (more later) cannot be 0 (always references something else) Mutable (default) vs. const types (read right to left) const int i; / read-only declaration int j;

    6、 / readable and writable declaration,Scopes in C+,Each symbol is associated with a scope The entire program (global scope) A namespace (namespace scope) Members of a class (class scope) A function (function scope) A block (block scope)A symbol is only visible within its scope Helps hide unneeded det

    7、ails (abstraction) Helps prevent symbol conflicts (encapsulation),Why Use Namespaces?,Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes and structs are used to specify self-contained, cohesive abstractions Can sa

    8、y what class/struct does in one sentence What if we want to describe more loosely related collections of state and behavior? Could use a class or struct But that dilutes their design intent,Namespaces,C+ offers an appropriate scoping mechanism for loosely related aggregates: Namespaces Good for larg

    9、e function collections E.g., a set of related algorithms and function objects Good for general purpose collections E.g., program utilities, performance statistics, etc. Declarative region Where a variable/function can be declared Potential scope Where a variable/function can be used From where decla

    10、red to end of declarative region,Namespace Properties,Declared/(re)opened with namespace keyword namespace Foo int baz_; namespace Foo int fxn() return baz_; Access members using scoping operator : std:cout “hello world!” std:endl; Everything not declared in another namespace is in the global (progr

    11、am-wide) namespace Can nest namespace declarations namespace Foo namespace Err ,Using Namespaces,The using keyword makes elements visible Only applies to the current scope Can add entire namespace to current scope using namespace std; cout “hello, world!” endl; Can introduce elements selectively usi

    12、ng std:cout; cout “hello, world!” std:endl; Can also declare unnamed namespaces Elements are visible after the declaration namespace int i_; / i_ is now visible ,C+ string Class,#include #include using namespace std; int main (int, char*) string s; / emptys = “”; / emptys = “hello”; s += “, ”;s = s

    13、+ “world!”;cout s endl; / prints: hello, world!return 0; , header file Various constructors Assignment operator Overloaded operators += + = = The last one is really useful: indexes string if (s0 = h) ,Using C+ vs. C-style Strings,#include #include using namespace std; int main (int, char*) char * w

    14、= “world”;string sw = “world”;char * h = “hello, ”;string sh = “hello, ”;cout (h w) endl; / 0: why?cout (sh sw) endl; / 1:why?h += w; / illegal: why?sh += sw;cout h endl;cout sh endl;return 0; ,C-style strings are contiguous arrays of char Often accessed through pointers to char (char *) C+ string c

    15、lass (template) provides a rich set of overloaded operators Often C+ strings do “what you would expect” as a programmer Often C-style strings do “what you would expect” as a machine designer Suggestion: use C+ style strings any time you need to change, concatenate, etc.,C+ Input/Output Stream Classe

    16、s,#include using namespace std; int main (int, char*) int i;/ cout = std ostreamcout i; cout “You said ” i “.” endl;return 0; , header file Use istream for input Use ostream for output Overloaded operators istream extraction operator Other methods ostream: write, put istream: get, eof, good, clear S

    17、tream manipulators ostream: flush, endl, setwidth, setprecision, hex, boolalpha,C+ File I/O Stream Classes,#include using namespace std; int main () ifstream ifs;ifs.open (“in.txt”);ofstream ofs (“out.txt”);if (ifs.is_open (), header file Use ifstream for input Use ofstream for output Other methods

    18、open, is_open, close getline seekg, seekp File modes in, out, ate, app, trunc, binary,C+ String Stream Classes,#include #include #include using namespace std; int main (int, char*) ifstream ifs (“in.txt”);if (ifs.is_open ()string line_1, word_1;getline (ifs, line_1);istringstream iss (line_1);iss wo

    19、rd_1;cout word_1 endl;return 0; , header file Use istringstream for input Use ostringstream for output Useful for scanning input Get a line from file into string Wrap string in a stream Pull words off the stream Useful for formatting output Use string as format buffer Wrap string in a stream Push fo

    20、rmatted values into stream Output formatted string to file,Using C+ String Stream Classes,#include #include #include using namespace std; int main (int argc, char *argv) if (argc f; argsin g;cout f “ / ” g “ is ” f/g endl;return 0; ,Program gets arguments as C-style strings But lets say we wanted to

    21、 input floating point values from the command line Formatting is tedious and error-prone in C-style strings (sprintf etc.) iostream formatting is friendly Can we get there from here?,Storing Other Data Types Than char,There are many options to store non-char data in C+ Differ in complexity, ease of

    22、useNative C-style arrays Can not add or remove positions Can index positions directly Not necessarily zero-terminated (why?)STL list container (bi-linked list) Add/remove positions on either end Cannot index positions directlySTL vector container (“back stack”) Can add/remove positions at the back C

    23、an index positions directly,X,0,1,2,3,4,X,X,0,1,2,3,4,A Quick Look at Vectors,Goals Give you a good basic data structure to use for now Cover its correct usage Start understanding why Vectors: nicer than arrays Less to manage/remember Harder to get things wrong (but still need to be careful)Example

    24、to the left prints v0 is 1 v1 is 2 v2 is 4,#include #include using namespace std; int main (int, char *) vector v;/ This would be asking for trouble/ v0 = 1; v1 = 2; v2 = 4;/ . but this works fine.v.push_back (1);v.push_back (2);v.push_back (4);/ . and now this is ok .for (size_t s = 0; s v.size(); +s) cout “v“ s “ is “ vs endl;return 0; ,


    注意事项

    本文(Built-In (a.k.a. Native) Types in C++ .ppt)为本站会员(inwarn120)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开