| Discussion 3C: F 10:00-12:00 in BH 4283 | CS 31 – Fall 2008 – UCLA |
| Office Hours: W 10:30-12:30 in BH 4428 | chuong [at] cs [dot] ucla [dot] edu |
News | Projects & HW | Discussions | Related Materials
DISCUSSIONS
| Week 0 | Week 1 | Week 2 | Week 3 | Week 4 | Week 5 |
| Week 6 | Week 7 | Week 8 | Week 9 | Week 10 |
Week 9 – 11/28/08
Thanksgiving duh !!!
Week 8 – 11/21/08
We talked about data structures:
- We know about built-in data types, such as
int, double, float, char, bool. We can now build our own data structures using the existing data types.-
struct Student { string name; int sid; string major; double gpa; } ; // don't forget this semicolon, it's a VERY COMMON MISTAKE to forget it
-
- Access individual field (or data member) of a
structby using the dot ‘.’ operator-
int main() { Student s; s.name = "Chuong"; s.sid = 12345; s.major = "CS"; s.gpa = 2.22; // ahem, not my real GPA cout << s.name << " " << s.sid << " " << s.major << " " << s.gpa << endl; }
-
- We can provide functions (or methods) to prevent direct access to the data member. This technique is called "data encapsulation" (or information hiding).
-
struct Student { public: bool setMajor(string aMajor); string getMajor(); ... private: string name; string major; }; bool Student::setMajor(string aMajor) { major = aMajor; } string Student::getMajor() { return major; } int main() { Student s; s.major = "CS"; // error s.setMajor("CS"); // woot, this works cout << s.getMajor(); }
-
- You can use keyword
structorclassinterchangeably. However:- For convention: use
structfor simple data structures, usually with no methods. Useclassfor more complex data structures, with methods and information hiding. - If you don't include the keyword
publicorprivate, by defaultstructispublicandclassisprivate.
- For convention: use
Week 7 – 11/14/08
This week quiz #4
We talked about pointers, a very dry topic:
- Declare, initialize pointers. Note the convention of using
int* pinstead ofint *p. - Pointer assignment
int* p = &a; // a is a variable of type int - Pointer parameters in functions: behave like pass-by-reference parameters, but with uglier syntax.
- Pointer and array
-
int a[5] = {5, 10, 15, 20, 25}; int* p; p = &a[3]; // p points to a[3] cout << *p // prints 20 p = &a[0]; // p points to a[0] cout << *p // prints 5 p = a; // the array name by itself 'a', is equivalent to writing '&a[0]' cout << *p // prints 5
-
- Pointer arithmetic
-
int a[5] = {5, 10, 15, 20, 25}; int* p = &a[2]; p++; // p points to the next element of the array, which is a[3] cout << *p; // prints 20 p = p - 2; // makes p go left 2 elements, p now points to a[1] cout << *p; // print 10
-
Week 6 – 11/7/08
We talked about:
- C strings:
- A C string is an array of characters, with a ‘\0′ at the end (0 byte terminator).
- Functions that operate on C strings rely on the ‘\0′ to signal the end of a C string.
-
#include <cstring> ... strlen // strcmp // strcpy // strcat //
-
- 2 common ways to declare a C string that can holds 100 characters:
-
const int SIZE = 100; char s[SIZE+1] = "Hello"; // hold maximum SIZE = 100 chars, plus '\0' terminator const int MAX = 101; char s[MAX] = "Hello"; // hold maximum MAX-1 = 100 chars, plus '\0' terminator
- File Input/Output.
Week 5 – 10/31/08
Nothing much. We talked about:
- Arrays
- Midterm
- Common mistakes
Week 4 – 10/24/08
This week quiz #3
We talked about:
- How to interpret an integer number (456) from a string representation of it (“456″):
- see 4 ->
number = 4; // 4 - see 5 ->
number = number * 10 + 5; // 4*10 + 5 = 45 - see 6 ->
number = number * 10 + 6; // 45*10 + 6 = 456
- see 4 ->
- Functions
- Pass-by-value: value of the parameter passed in gets copied into the local variable of the function.
- Pass-by-reference: create another name for the parameter passed in.
void swap(int& x, int& b)function.
- Variable scopes.
Week 3 – 10/17/08
There was no quiz this week. We went over the old quiz #2 instead.
We talked about:
- How to access/visit each individual character inside a string.
- The index starts at 0. Note the different between saying “the 3rd character of a string” (what index is it?) and “the character at index 3″.
string s = "abc"; cout << s.size(); // how many characters in s- Walk through every characters of a string using a
forloop. - Pre-defined functions
- In library
<iostream>:cout,cin - In library
<cctype>:isdigit,isalpha, … (read the book for more pre-defined functions)
- In library
- User-defined functions:
- function’s prototype/declaration vs. function’s implementation/definition.
returnType functionName(parameterType parameterName, ...)- e.g.
bool isAllLetters(string s)
Week 2 – 10/10/08
This week quiz #2
We talked about:
- Boolean data type: either
trueorfalse. - Comparisons:
==, !=, <, <=, >, >= - Boolean expressions: small parts are connected together with
&&(and), or||(or). -
bool a = 1 != 3; bool b = !(1 == 1) && 3 >= 5; // ! is the negation operator bool c = 5 == 5 || 2 <= 9 && 3 > 4; //note the order of evaluation, && then ||
- Branching statement:
if ifif/elseif/else if/elseelse ifandelseare always optional- Braching statement:
switch defaultcase is optional- Different effects of omitting the
break;statement - Loop statement:
while,do/while. Remember how the code flows. - Loop statement:
for
Week 1 – 10/03/08
This week quiz #1
We talked about:
- Escape sequence:
\n,\t,\\,\" - A newline can be achieved by using
\norendl. These 2 lines are equivalent:-
cout << "hello\nworld"; cout << "hello" << endl << "world";
-
- Valid identifiers: Starts with _, letter. The rest can be _, letter, or digit
- Variable declaration, initialization, assignment. What’s the difference between initialization and assignment
-
int x; //declaration x = 5; //initialization x = 10; //assignment int y = x; //declaration and initialization int a = 0, b = 2, c, d = 7, e(10); //all in 1 line. Note that c is un-initialized
-
- Differences among compile-warning, compile-warning, runtime-error
- compile-warning: your syntax (grammar) is fine, however there is something you should not be doing
- compile-error: whoops, your syntax (grammar) is wrong
- runtime-error: compile successfully (no syntax errors), however your program contains some logic errors (something that you haven’t thoughtfully written, something is wrong with the meaning/content of your program). Errors occur when you run the program (hence, runtime-error), caused by unthoughtful lines of code.
- C++ data types: int, double/float, char
- Integer division. No rounding. We drop the decimal point, taking only the integer part of the result.
-
int x = 9/5; // x is 1
-
- Comments:
-
// for a line of comment /* for a block of comments */
-
Week 0 – 09/26/08
We went over the basic Hello World example:
1 2 3 4 5 6 7 | #include <iostream> using namespace std; int main() { cout << "Hello World"; } |
The above program will display this text on your screen:
Hello World
Line 4-7:
4 5 6 7 | int main() { cout << "Hello World"; } |
Every C++ program must have a main() function, the curly braces indicate the body of the main() function.
The body of the main() function, in this example, is only 1 line (or 1 statement), which is cout << "Hello World";.This line of code instructs the program to display Hello World onto your screen.
cout is a special function in C++ (technical term is predefined function). You can think of cout as a very small program that someone already wrote. What it does is to display whatever text between the double quotes onto the screen.
Line 1-2:
1 2 | #include <iostream> using namespace std; |
So, there are many functions like cout in C++. They group a bunch of those functions together and put them in 1 basket (technical term is library). They name the basket isotream.
You can understand that line 1 as follow: we are using the cout function in our program, and the cout function belongs to the iostream library, so we need to tell our program where to look for that cout function by saying #include <iostream>
Also, in that library, the cout function is actually belong to a category (technical term is namespace) named std. So to address the cout function the right way, we need to use it’s full name, which is std::cout (as what should have been in our example std::cout << "Hello World";). However, we want to be lazy and tell the program that whenever we use cout, we really mean std::cout. The way to do that is to say using namespace std;. By saying so, whenever our program see cout, it knows that we’re talking about std::cout.
