C++ crash course

25
C++ crash course Class 10 practice problems

description

C++ crash course. Class 10 practice problems. 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 ; }. Strings. - PowerPoint PPT Presentation

Transcript of C++ crash course

Page 1: C++ crash course

C++ crash course

Class 10practice problems

Page 2: C++ crash course

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

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

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

Pointers

• This won’t compile. Why?

double a = 10;int *aptr = &a;

Page 6: C++ crash course

Pointers

• What is “this”, and what is its use?

Page 7: C++ crash course

Data Types

• How is initializing a vector of ints different from initializing a single int?

Page 8: C++ crash course

Data Types

• Write the following in decimal.

1010012

04560x21

Page 9: C++ crash course

Files and Unix

• What’s the difference between > and < on the Unix command line?

Page 10: C++ crash course

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

Operators

• What do all of the following do?

*itr++;*++itr;(*itr)++;

Page 12: C++ crash course

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

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

Overloading Operators

• What’s the function prototype to overload the output operator?

– return type– function name– parameter list

Page 15: C++ crash course

const

• What is the purpose of the “const” keyword?

• const gets a little tricky with pointers. Give an example.

Page 16: C++ crash course

Loops

• Write a function to sum even numbers from int a to int b, and return the result.

Page 17: C++ crash course

Reserved Words

• What are reserved words? Give 3 examples.

Page 18: C++ crash course

Functions

• What’s the difference between int a, int &a, and int *a in a function parameter list?

Page 19: C++ crash course

?

• What are these called? What do they do?

#include#define#ifndef#endif

Page 20: C++ crash course

Operators

• What is precedence? What is associativity?

• Is assignment left or right associative?• Is arithmetic left or right associative?

Page 21: C++ crash course

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

Sorting

• Write an insertion sort on a vector of int *.

Page 23: C++ crash course

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

Classes

• What is the difference between public, private and friend?

Page 25: C++ crash course

Data Types

• What’s the difference between a literal and a constant?