DOC PREVIEW
WUSTL CSE 332S - C++_variables_basic_data_type

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Built-In (a.k.a. Native) Types in C++User (& Library) DefinedTypes in C++Comparing C++ Classes and StructsMore About Both Native and User TypesScopes in C++PointersWhat’s a Pointer?What’s a Reference?Untangling Operator SyntaxAliasing and ReferencesAliasing and PointersReferences to PointersReferences to ConstConst Pointers and Pointers to ConstPassing Parameters to FunctionsA Few More Type Declaration KeywordsCSE 332: C++ variables and basic data types Built-In (a.k.a. Native) Types in C++ •int, long, short, char (signed, integer arithmetic)–unsigned versions toounsigned int, unsigned long, etc.–C++ guarantees a char is one byte in size–Sizes of other types are platform dependent–Can determine using sizeof() , <climits> INT_MAX•float, double (floating point arithmetic)–More expensive in space and time–Useful when you need to describe continuous quantities•bool type–Logic type, takes on values true, falseCSE 332: C++ variables and basic data types User (& Library) DefinedTypes in C++ •(unscoped or scoped) enumerationsenum primary_color {red, blue, yellow};// scoped enums are preferred as of C++11enum struct palette {ochre, umber chartreuse, ecru};•functions and operators–For example, things called from main function•structs and classes–Similar abstractions in C++, extend C structsCSE 332: C++ variables and basic data typesComparing C++ Classes and Structsstruct 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 –Constructors–Destructors–Member variables–Member functions•Common practice:–use structs for data–use classes for objects with non-trivial methodsCSE 332: C++ variables and basic data typesMore 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)–can 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 declarationint j; // readable and writable declarationCSE 332: C++ variables and basic data typesScopes in C++•Each symbol is associated with a s cope–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 details (abstraction)–Helps prevent symbol conflicts (encapsulation)CSE 332: C++ variables and basic data typesPointers•Often need to refer to another object–Without making a copy of the object itself•Two ways to do this–Indirectly, via a pointer•Gives the address of the object (analogy: street address)•Requires the code to do extra work: dereferencing•Like going to the given address, to talk to the person–Directly, via a reference•Acts as an alias for the object•Code interacts with reference as if it were object itselfCSE 332: C++ variables and basic data typesWhat’s a Pointer?•A variable holding an address–Of what it “points to” in memory•Can be untyped–E.g., void * v; // points to anything•However, usually they’re typed –Checked by compiler–Can only be assigned addresses of variables of type to which it can point–E.g., int * p; // only points to int•Can point to nothing– E.g., p = 0; // or p = nullptr; (C++11)•Can change where it points–As long as pointer itself isn’t const –E.g., p = &i; // now points to i0x7fffdad07int iint *pCSE 332: C++ variables and basic data typesWhat’s a Reference?•Also a variable holding an address–Of what it “refers to” in memory•But with a nicer interface–A more direct alias for the object–Hides indirection from programmers•Must be typed –Checked by compiler–Again can only refer to the type with which it was declared–E.g., int & r =i; // refers to int i•Always refers to (same) something–Must initialize to refer to a variable–Can’t change what it aliases 0x7fffdad07int iint & rCSE 332: C++ variables and basic data typesUntangling Operator SyntaxSymbol Used in a declarationUsed in a definitionunary & (ampersand)reference, e.g.,int i; int &r = i; address-of, e.g.,p = & i;unary *(star)pointer, e.g., int * p;dereference, e.g.,* p = 7;-> (arrow)member access via pointer, e.g.,C c; C * cp=&c; cp->add(3);. (dot)member access via reference or object, e.g., C c; c.add(3); C & cr = c; cr.add(3);CSE 332: C++ variables and basic data typesAliasing and Referencesint main (int argc, char **argv){ int i = 0; int j = 1; int & r = i; int & s = i; r = 8; // i is now 8, j is still 1} •An object and all the references to it alias the same location–E.g., i, r, and s•Assigning a new value to i, r or s changes value seen through the others•But does not change value seen through j0xefffdad08int iint & r1int j0xefffdad0int & sCSE 332: C++ variables and basic data typesAliasing and Pointersint main (int argc, char *argv[]){ int i = 0; int j = 1; int * p = & i; int * q = & i; *q = 6; // i is now 6, j is still 1} •Distinct va riables have different memory locations–E.g., i and j•A variable and all the pointers to it (when they’re dereferenced) all alias the same location–E.g., i, *p, and *q•Assigning a new value to i, *p or *q changes value seen through the others•But does not change value seen through j0xefffdad06int iint *p1int j0xefffdad0int *qCSE 332: C++ variables and basic data typesReferences to Pointersint main (int argc, char **argv){ int j = 1; int & r = j; // r aliases j int * p = & r; // p really // points to j int * & t = p; // t aliases p} •Can’t have a pointer to a reference–But can point to what the reference aliases•Address-of operator on a reference to a variable–Gives address of variable–… not of reference itself•Reference to a pointer–An alias for the pointer … not for what it points to–Useful to pass a pointer to code that may change it0xefffdad01int jint & rint * p0xefffdad00xefffdad0int * & tCSE 332: C++ variables and basic data typesReferences to Constint main (int argc, char **argv){ const int i = 0; int j = 1; int & r = j; //


View Full Document

WUSTL CSE 332S - C++_variables_basic_data_type

Download C++_variables_basic_data_type
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++_variables_basic_data_type and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++_variables_basic_data_type 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?