c++ crash course class 10 practice problems. pointers the following function is trying to swap the...
TRANSCRIPT
![Page 1: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/1.jpg)
C++ crash course
Class 10practice problems
![Page 2: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/2.jpg)
Pointers
The following function is trying to swap the contents of two variables. Why isn’t it working?
void swap(int &a, int b) {int tmp = a;a = b;b = tmp;
}
![Page 3: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/3.jpg)
Strings
• Write a function which, given a char array and two ints, will return a string * which holds the section between the two ints. Return the null pointer if it’s not possible.
string *substr(char arr[], int start, int stop);
![Page 4: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/4.jpg)
Dynamic Memory Allocation
• The below is bad practice. Why is that?
int *p = new int(5);int j = 7;p = &j;
![Page 5: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/5.jpg)
Pointers
• This won’t compile. Why?
double a = 10;int *aptr = &a;
![Page 6: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/6.jpg)
Pointers
• What is “this”, and what is its use?
![Page 7: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/7.jpg)
Data Types
• How is initializing a vector of ints different from initializing a single int?
![Page 8: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/8.jpg)
Data Types
• Write the following in decimal.
1010012
04560x21
![Page 9: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/9.jpg)
Files and Unix
• What’s the difference between > and < on the Unix command line?
![Page 10: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/10.jpg)
What’s the output?
int size = 3;int arr[12] = {7, 2, 4};char name[12] = “Jeff Jones”;
int *iptr;char *cptr;iptr = arr;cout << iptr[1] << “ and “ << *iptr << endl;cptr = &name[5];cout << cptr << endl;cout << *(cptr + 3) << “ and “ << *(cptr-4) << endl;cptr ++;cout << name[3] << “ and “ << cptr[3] << endl;
![Page 11: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/11.jpg)
Operators
• What do all of the following do?
*itr++;*++itr;(*itr)++;
![Page 12: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/12.jpg)
What does this output?void funct(int a);int main() {
int a = 5, *b = &a, c[] = {10, 21, 12};int *d, *e, f;
cout << *b << “ “ << *c + 1 << endl;e = b;b = new int;d = new int;*d = 7;*b = 9;*e = 11;cout << *e << *d << a;funct(a);e = b;free(b);cout << *e << f << a;return 0;
}
![Page 13: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/13.jpg)
Scope
• What’s the scope operator (::) doing in these instances?
std::cout
int Animal_type::avg_age() const
What can you do to avoid using the scope operator repeatedly?
![Page 14: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/14.jpg)
Overloading Operators
• What’s the function prototype to overload the output operator?
– return type– function name– parameter list
![Page 15: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/15.jpg)
const
• What is the purpose of the “const” keyword?
• const gets a little tricky with pointers. Give an example.
![Page 16: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/16.jpg)
Loops
• Write a function to sum even numbers from int a to int b, and return the result.
![Page 17: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/17.jpg)
Reserved Words
• What are reserved words? Give 3 examples.
![Page 18: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/18.jpg)
Functions
• What’s the difference between int a, int &a, and int *a in a function parameter list?
![Page 19: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/19.jpg)
?
• What are these called? What do they do?
#include#define#ifndef#endif
![Page 20: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/20.jpg)
Operators
• What is precedence? What is associativity?
• Is assignment left or right associative?• Is arithmetic left or right associative?
![Page 21: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/21.jpg)
Sorting
• Sort these using insertion sort, and show all the steps.
5 4 15 99 46 82 93 54 71 26 12
![Page 22: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/22.jpg)
Sorting
• Write an insertion sort on a vector of int *.
![Page 23: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/23.jpg)
Classes
• What’s a constructor? How do you identify one?
• Write a default constructor for this class:
class Mystery {int a;bool b;string s;
};
![Page 24: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/24.jpg)
Classes
• What is the difference between public, private and friend?
![Page 25: C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void](https://reader036.vdocuments.us/reader036/viewer/2022062511/5518b1fa550346a61f8b4f46/html5/thumbnails/25.jpg)
Data Types
• What’s the difference between a literal and a constant?