Built-In (a.k.a. Native) Types in C++ .ppt
《Built-In (a.k.a. Native) Types in C++ .ppt》由会员分享,可在线阅读,更多相关《Built-In (a.k.a. Native) Types in C++ .ppt(17页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BUILTINAKANATIVETYPESINCPPT
