Midterm Prep

Created Sunday 19 June 2016

template<typename T>
T adder(T v) {
return v;
}

template<typename T, typename... Args>
T adder(T first, Args... args) {
return first + adder(args...);
}

IMPORTANT NOTES


----------

B *b = static_cast<B*>(new A); is A not B! (can execute A's stuff but not B's UNLESS special case(virtual))

----------

casts have to be either pointers OR references

----------

A B C

A has a virtual function, B and C do not. THEY ALL HAVE VPTRS

----------

// Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other.mData = NULL;
other.mLength = 0;

----------

http://accu.org/index.php/journals/268

----------

STUFF TO RESEARCH


- Function Overload Resolution
- specialized templates for functions and classes
- Which headers to use for which functions

- std::move -> <utility>
- std::forward -> <utility>
- <cstring>
- std::remove_reference -> <type_traits>
- std::swap -> <algorithm>
- std::copy -> <algorithm>
- Move semantics (big 3)
- try, catch, throw
http://thbecker.net/articles/rvalue_references/section_07.html
http://thbecker.net/articles/rvalue_references/section_08.html


CS225 Midterm Checklist - what you are could be tested on (This is list is neither exhaustive nor comprehensive but indicative, that is, you can get the sense of the emphases of the midterm from this list, but this list doesn’t cover everything (e.g., you must know all the C stuff such as pointers and arrays, but we don’t include it here.), moreover, it may also contain stuff that ultimately is not tested in the midterm.

1. General problem solving and programming. You should be able to code stuff that can solve simple problems such as finding substrings etc. So you need to be able to write programs.


2. C++ syntax/semantics/features that you are expected to know:


a. Command-line arguments – this is basic stuff.




Try the following program:

1 #include <iostream>
2 
3 int main(int argc, char** argv) {
4 	std::cout << "Have " << argc << " arguments:" << std::endl;
5 	for (int i = 0; i < argc; ++i) {
6 		std::cout << argv[i] << std::endl;
7 	}
8 }

Running it with ./test a1 b2 c3 will output

1 Have 4 arguments:
2 ./test
3 a1
4 b2
5 c3

argc is number of arguments (first included), argv is the actual arguments. normally the first argument has the "./" too.

  1. === Top-level vs low-level consts and the implications (how they affect copy assignments mainly and misc stuff) ===


Const objects can only run const functions - apart from constructor and destructor

JUST THINK ABOUT TYPES


  1. === Complex C/C++ declarations ===


Should be fine.


  1. === Data-alignment for Structs (basic stuff) ===




i. C++ -style casts – static_cast, const_cast, reinterpret_cast, dynamic_cast and their appropriate usage.



Inheritance changes the game because now there are three different things we might mean by a pointer cast. Say class D derives from class B. What should (D*)pb do, where pb has type B*? Here are three possibilities:

  1. (REINTERPRET_CAST) Return a pointer to the same byte of memory, but just change the type of the pointer. (This is the same as what all pointer casts do in C, as explained above.)
  2. (DYNAMIC_CAST) Check whether the B* really points to a B that is part of a D object. If so, return a pointer to the D object. If not, fail (maybe by returning a null pointer or throwing an exception.)
  3. (STATIC_CAST)Assume that the B* points to a B that is part of a D object; don't bother performing a check. Adjust the address of the pointer if necessary so that it will point to the D object.

It is conceivable that in C++ we might want to perform any of these three conversions. Therefore C++ gives us three different casts:reinterpret_cast<D*> for function (1), dynamic_cast<D*> for function (2), and static_cast<D*> for function (3).


j. Object-oriented Programming – inheritance – relationships between objects


k. “is-a” C++ idiom – either inheriting implementation or inheriting interface. Although C++ allows you to do both at the same time, you should really do one of the above. What are the implications for programming?


l. Understanding the meaning of virtual keyword


m. Name-lookup/function matching etc for objects involved in class hierarchies.


n. Non-Virtual Interface Idiom


o. Co-variant return type and the virtual constructor


p. Function Templates – CS170 stuff


q. Function template speciailization


r. Class templates – CS170 stuff


s. Class template specialization


t. Function matching algorithm(for overloaded functions, including template/specialized functions)


u. STL algorithms


v. std::copy and it’s variant forms


ii. std::swap


p. STL containers


i. std::vector<T>


ii. std::list<T>


iii. std::string


q. Range for


r. Rvalue reference and move semantics


i. Being able to implement move constructors, move assignments etc.


s. Universal references and perfect forwarding


General rubrics for programming questions


1. Unless the question states otherwise, always choose to include appropriate headers and use appropriate functions instead of writing your own code. Of course, you are limited to only what’s provided in the Standard Library.


2. Use range-for instead of using integral counters for loops.


3. Use member initializer lists for constructors!


4. Const-correctness. Example: member functions that doesn’t change the internals of the object should be const, objects should be passed in via const ref if they are not supposed to be changed.


5. See this link for more details: https://distance.sg.digipen.edu/file.php/2165/public/CS225_-_code_rubrics.pdf