C++

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The "not" operator !

!true is false, and !false is true int main() { bool want_to_start; cout << "Do you want to start the application? [0: No] [1: Yes]" << endl; cin >> want_to_start; //An input of 0 is 'false', and 1 is 'true' if(!want_to_start) { cout << "Then why open the application? Idiot, too bad, I'm starting anyway." << endl; } cout << "Application starting." << endl; //Continue main program code here return 0; }

longer example of if, else if, and else statement:

#include <iostream> using namespace std; int main(){ int mark; cout << "What mark did you get in the test?" << endl; cin >> mark; if(mark >= 90) { cout << "You got an A*" << endl; cout << "You Passed!" << endl; } else if(mark >= 80) { cout << "You got an A" << endl; cout << "You Passed!" << endl; } else if(mark >= 70) { cout << "You got an B" << endl; cout << "You Passed!" << endl; } else if(mark >= 60) { cout << "You got an C" << endl; cout << "You Passed!" << endl; } else if(mark >= 50) { cout << "You got an D" << endl; cout << "You Failed!" << endl; } else if(mark >= 40) { cout << "You got an E" << endl; cout << "You Failed!" << endl; } else { cout << "You got an U" << endl; cout << "You Failed!" << endl; } return 0; }

#include <cmath>

#include <iostream> #include <cmath> using namespace std; int main() { //Main application code will go here return 0; }

What is the same as c = c + 1 ?

c += 1;

The "and" operator &&

int main() { int age; cout << "Enter your age: "; cin >> age; if(age >= 35 && age <= 80) { cout << "You're between 35 and 80 and can save money on your car insurance!" << endl; } else { cout << "Sorry, we don't have any deals for you today!" << endl; } return 0; }

Some Pointer Syntax Summary:

int* ptr = declares a pointer to an int. The * in the declaration of a pointer does not have the same meaning as the * when we set the pointed to variable's value &variable = the "&" means the variable's address, not its value. ptr = &variable; = this sets "ptr" to point to the address of variable" *ptr = 99; = this sets the value at the address "ptr" points to

+= operator

string one = "Hello"; one = one + " Bob!"; cout << one << endl; So instead of typing something like the above, we could instead use something a little bit cleaner like: string one = "Hello"; one += " Bob!"; cout << one << endl;

If the value and address of paul is 21 and 400, respectively, what would the value of tom and melissa be after these lines of code: tom = paul; melissa = &paul;

tom = 21 melissa = 400

run a C++ program

./programname

int *melissa;

That's it! This will move a person named melissa into a house, who's sole purpose is to store the address of someone else in computer town. Basically, she is a pointer!

Booleans

The boolean data-type is for storing true or false values. Boolean variables are declared using the bool type, and boolean constants are defined by either using the true or false keywords. bool variable_name = true;

Chars

The character data-type is for storing single character values - for example 'a' or 'e'. Character variables are declared by using the char type, and character constants are defined by using single quotes (apostrophes) around the character. char variable_name = 'c';

postdecrementation

--d

input/output on screen

#include <iostream> using namespace std;

LIKE BROTHERS: Pointers & Arrays

Let's say we have two brothers. One brother finds a girl, settles down with her, and stays with that woman for the rest of his natural life. He is very constant. The other brother is the complete opposite. He might stay with a woman for a day, a week, a month, or maybe a couple of years. He might somewhere in his life settle down, but he doesn't stick with only one girl. Well this is the same with pointers and arrays. An array is really just a constant pointer. When you declare an array like: int ramon[5]; ramon is actually a pointer that constantly points at the beginning of the array. We cannot point ramon somewhere else. To make a const pointer we would type: const int *tony; This will create a const pointer called tony. Once we initialize where tony will point, we cannot point him somewhere else or his wife will be mad... I mean the compiler will give you an error. But, remember the wild brother can always point to the constant pointer... int ramon[5]; int *paul; paul = ramon; Now, paul and ramon are pointing to the same thing; the first element of the array.

logical operators - NOT

NOT negation ! !(5 > 7 || 5 != 10) negates a statement

int paul = 21; int *melissa, *dave; melissa = &paul; dave = melissa;

This will create two people who point, melissa and dave. Melissa will store the value of the address of paul, as seen on line 3. But then dave will also store the value of paul, because he gets it from melissa. So, we now have two people pointing to the same house. Cool, huh?

p1 = &a[1]; printf("*p1 = %d\n", *p1); p1 = p1+2; printf("*p1 = %d\n", *p1);

This would print out: *p1 = 2 *p1 = 4

int paul = 21; int *melissa; melissa = &paul;

So here we move paul into his house, of size int, and store 21 in it. We then declare that melissa will be a person that only points to houses of size int. We then tell melissa to point to paul's house. And that is it!

int **ramon; int *paul; int melissa = 5; paul = &melissa; ramon = &paul; printf("ramon = %d\n", ramon); printf("&paul = %d\n", &paul); printf("*ramon = %d\n", *ramon); printf("&melissa = %d\n", &melissa); printf("**ramon = %d\n", **ramon);

The first line declares a double pointer called ramon. We then declare a pointer called paul. And finally we declare a regular integer called melissa which we initialize to 5. Next, we point paul to melissa, which stores melissa's address into paul's house. Then we point ramon to paul, which will store paul's address into ramon's house. So, now let's look at what will be printed out using the diagram below's addresses. ramon = 1000 &paul = 1000 *ramon = 500 &melissa = 500 **ramon = 5

Now, let's suppose paul's address is 1500. paul = 21; tom = paul; melissa = &paul;

The first line will store 21 into paul's house. The second line will store the value 21 inside tom's house. So far no different then what we have usually done. The third line, though, will store the address of paul, 1500, into melissa's house.

input

cin >> variable;

postincrementation

d++

Pointer

A data type that 'points' to another value store in memory. **Pointers holds a memory address - Since pointers contain the actual address of the data, the compiler does less work when finding that data in memory.

logical operators - AND

AND conjunction && 7 > 5 && 5 != 10 conjunction is true ONLY when both expressions are true

e.g. string & cin

#include <iostream> #include <string> using namespace std; int main() { string name; //A string variable to store the user's name cout << "Enter your name: "; //Prompt the user for their name cin >> name; //Take input for the user's name and store it in our string variable, 'name' cout << "\nHello, " << name << "! It's nice to meet you!"; //Greet the user return 0; }

int a[5] = {1,2,3,4,5}; int *p1; p1 = &a[1]; // gets address of this element printf("*p1 = %d\n", *p1); p1++; // point to the next element printf("*p1 = %d\n", *p1);

*p1 = 2 *p1 = 3 Note: If you thought answer was 1 and 2, remember that the array starts at 0. So when we put [1], this is actually the second element. And if we wanted to go back an element it would be: p1--;

p1 = &a[1]; int dave; dave = *(p1+2); printf("*p1 = %d\n", *p1); printf("dave = %d\n", dave);

*p1 = 2 dave = 4

What is printed out? int a[5] = {1,2,3,4,5); int *p1; p1 = &a[2]; printf("*p1 = %d\n", *p1); p1 = p1 + 2 printf("*p1 = %d\n", *p1);

*p1 = 3 *p1 = 5

Difference between Pointers & References

*pi = 4; ri = 4; When using pointers, the address must be dereferenced using the *, whereas, when using references, the address is dereferenced without using any operators at all! The main effect of this is that the address can directly be manipulated if it is a pointer. We can do things such as: pi++; to increment to the next address. This is not possible using references. Therefore, to summarize, a pointer can point to many different objects during its lifetime, a reference can refer to only one object during its lifetime.

preincrementation

++d

comments

//This is a single-line comment. /* This is a multi-line comment. It can have multiple lines! */

The * appears before the pointer variable in only TWO places:

1. when you declare a pointer variable, and 2. when you de-reference a pointer variable (point to (access) the data whose address is stored in the pointer)

bitwise operators

Created to work on bits (small info stored in computer, 0 and 1)

Strings

Defined by using double quotes. You are required to #include <string> to use this data-type. #include <string> string variable_name = "This is a string!";

logical operators - OR

OR disjunction || (alternative) 5 > 7 || 5 != 10 disjunction is true when at least one expression is true disjunction is false when both expressions are false

int *melissa, paul;

This does not create two pointers, melissa and paul. It actually creates a pointer of size int named melissa and a regular int named paul. If we want to declare two pointers of the same type we would do this: int *melissa, *paul; This will create two pointers of size int named melissa and paul. Another thing is size. A pointer must know how big the house it is pointing to. So if paul is an int, only an int pointer can point to it. If paul is a float, then only a float pointer can point to it, and so on.

melissa = &paul;

This line would store the address of paul into melissa's house. (**paul's address is 1500)

The "or" operator ||

This means that true || true, true || false, and false || true will all return true, and false || false will return false. int main() { int age; cout << "Enter your age: "; cin >> age; if(age < 0 || age > 160) { cout << "You're lying - you CANNOT be that age." << endl; } else { cout << "Thanks for inputting your age!" << endl; } return 0; }

[pointers] computer memory

We are going to believe that computer memory is made up of a bunch of houses on one very long street. Thus, each house is a memory cell. Now, there must be a way for us to find this house. Well, in each house someone lives there. This person of course has a name and this will be our variable identifier. For example: int paul; This will put paul into a vacant house, of size int, somewhere along the street. We do not decide where paul will live. This is done by the operating system and the compiler at runtime. Currently, paul does not have anything stored in his house. But, we all know that wouldn't be any fun to not store anything. So, each house can of course store a value. Continuing from above: paul = 25; This will store the value 25 into paul's house. Let us remember that paul's house is a unique number in memory. In addition, if paul's house was numbered 1234 we know that his house is between houses 1233 and 1235. This is a very important concept for later sections.

Pointer Operations (& - Address of Operator)

Will print the memory address in hexadecimal format. By using the & operator in front of a variable name we can retrieve the memory address-of that variable. It is best to read this operator as address-of. (WILL PRINT THE ADDRESS)

int a = 5, b = 10; int *p1, *p2; p1 = &a; p2 = &b; *p1 = 10; p1 = p2; *p1 = 20; printf("a = %d\n", a); printf("b = %d\n", b);

a = 10 b = 20

What is printed out? int a = 10, b = 20; int *p1; p1 = &1; *p1 = 5; p1 = &b; *p1 = 10; printf("a = %d\n", a); printf("b = %d\n", b);

a = 5 b = 10

Once a pointer is pointing a variable you can change the value of the variable using *p:

a was 99, but now it is changed to 100. *p will not change the value in *p, but the value in p that p is pointing to.

output

cout << "string of characters"; cout << variable; << endl

predecrementation

d--

If the address of paul is 1500 and the address of melissa is 400, what would the value of dave be after these lines of codes: paul = 21; melissa = &paul; dave = *melissa;

dave = 21

to compile a C++-program

g++ filename.cpp -o programname **program name is up to you

if statement

if my_bank_account_balance > 50.00 puts "I'm eating steak!" else puts "I'm eating ramen :(" end

If and Else Statements

if(boolean_expression) { // statement(s) will execute if the boolean expression is true } else { // statement(s) will execute if the boolean expression is false }

e.g. if and else if and else statements

if(height == age) { cout << "Your height is equal to your age!"; } else if(height < age) { cout << "Your height is less than your age!"; } else if(height > age) { cout << "Your height is greater than your age!"; }

How do you declare an int pointer called melissa?

int *melissa;

relational operator

int a = 10; int b = 5; cout << (a ==b) << endl; ==> 0 The relational operator is always either 1 or 0 (true/false) (a == b) True (1) when both variables have the same value (a != b) False (0) when both variables do not have the same value (a > b) True (1) when variable a is greater than variable b (a < b) (a >= b) (a <= b) !(a ==b) ==> a != b

int j = 190; cout << "j is " << endl; cout << "Address of j is " << &j << endl;

j will print out 190 &j will print out something like 0041FD2C (memory address)

; (semi-colon)

lines are often finished with a semicolon in C++ to show that the instruction or the line has finished.

Dereferencing a pointer (*)

means getting the value stored in the memory at the address which the pointer "points" to.

int *p2; p1 = &a[1]; p2 = p1++;

p2 would not point to the third element, but actually to the second element since the (++) operator is after the variable it is done after the assign (=) operator. So *p1 would equal 3 while *p2 would equal 2.

Syntax for declaring pointer

type + * + name int* p; int *p; string* q;


संबंधित स्टडी सेट्स

APEX English 10 Unit 2 Quiz 2.2.4: American Born Chinese Part 2

View Set

Boards - Ocular Motility and Binocular Vision

View Set

8.03 Quiz: Applications of Triangle Similarity

View Set

XcelSolutions Pennsylvania Insurance Test

View Set

Chapter 32: Cholinergic Agonists

View Set

CITI social and behavioral research

View Set

Americans with Disabilities Act (Test 2)

View Set