cs31 lecture 6&7

Declare a variable in one cpp file and use it issue

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: bal variables in namespaces x Goal: define a variable associated with a namespace Example: store some internal state x Issue: how to declare a variable in one cpp file, and use it Issue: in another file? in For ex., cannot declare twice a function, we use function For prototype prototype Prototype for variable declaration: extern x Rule: a global variable can only be declared once in the Rule: program program x To refer to this variable from another file: extern <vardecl> 41 Example In foo.cpp: int g_foo; In foo.hh: extern int g_foo; 42 Namespaces again x Global variables can be declared in a namespace x Typedefs can be declared in a namespace x In both cases, the using directive and :: operator can be In used used x Be careful: namespaces are not classes! instance of a class: an object (with its own data) Namespace: simple “prefix” in the names of functions/global Namespace: variables/type declarations variables/type 43 Scopes in C++ x A variable is accessible only from within its scope x Scope: region (typically a block enclosed with {} ) where Scope: the variable exists the x Examples: for (int i = 0; i < N; ++i) sum += i; sum foo = i; // <- invalid statement: i is not defined 44 The intuition behind scopes x A program can be seen as a hierarchy of regions x A variable declared in a region can be used only from variable within this region and all its subregion within x From a (sub-)region, the closest definition of the variable From applies. If the variable is not defined in the current subapplies. region, then a definition is searched for in the parent region, region, etc. region, 45 Example: foo.cpp // region 1 starts iint foo() nt { /// region 1.1 starts. / int i = 42; int { /// region 1.1.1 starts. Parents: 1.1, and 1 / float i = 0; float /// region 1.1.1 ends / } { /// region 1.1.2 starts. Parents: 1.1, and 1 / /// region 1.1.2 ends / } /// region 1 ends. / } // region 1 ends 46 The case of for loops x The <init> clause of the for loop belongs to the scope of The the for loop the x Be careful: a for loop is a nested scope: for (int i = 42; i < 100; ++i) sum += i; sum Is translated into: { // scope 1 starts int i = 42; int while (i < 100) { /// scope 1.1 starts / sum += i; sum ++i; ++i; } 47 Typical problems x Re-defining a variable that was previously defined (not an Re-defining error: it is a valid C++ behavior) error: x Trying to use a variable not defined in the current scope x Defining a missing variable in the wrong scope 48 A word on program arguments x Early intuition: the arguments of a function are stored on Early the stack the x Their value can be stored, or only a “pointer” to where the Their value is value pass-by-value pass-by-reference x In C++, scalar types (bool, int, float, double, char, …) are In passed by value: an actual copy is made passed x They can be passed by reference, using They <type1>& arg <type1>& 49 Examples void foo(int i, int& j) { i = 42; // there is a copy of i, local to the function 42; j = 43; // j just refers to an address in memory, outside the function 43; } int main() { int p = 1, q = 2; int foo(p, q); foo(p, std::cout << “p: “ << p << “ q: “ << q << std::endl; // prints p: 1 q: 43 std::cout return 0; return } 50 A word on strings in C++ x A string is a vector of characters, roughly x Each character can be read one by one by indexing the Each vector vector string s = “asdfasdfasdf”; for (int i = 0; i < s.size(); ++i) std::cout << s[i] << std::endl; 51 Good programming style 1. Code must be documented / commented 2. Functions should be used whenever possible Rough guideline: a function should be less than 100 lines, Rough ideally more around 25 lines ideally Namespaces should be used for multiple-file projects 1. Ok approach: 1 namespace for the full project 1. Replacing a variable definition in a sub-region should be Replacing avoided avoided 2. Global variables should be used with parsimony Global 52 A few advices for the midterm on Feb. 6 x All material covered in the lectures (inc. Feb. 4) should be known, All EXCEPT EXCEPT compiler optimizations stack/implementation of function calls/recursive functions x Basically, you should be able to do the projects by hand The point will not be to trick you x Being able to read and write standard C++ is required Being But we don’t expect you to be a full-blown human C++ compiler x Being able to write an algorithm/pseudo-code for a new problem is Being required (do the homework!) required x It will likely be an open-book exam (the final may not be) 53...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online