Comment on C++

Lecture 1

  1.  #include is really useful when you want to use facilities that is not in the core language. include the library name you want to use inside the <> symbol after it.
  2. Cout is the command to display values.
  3. All the statement must end with ” ; “.
  4. Source code is the fundamental component of a computer program that is created by a programmer.
  5. C++ is a compiled language so you need to translate the source code in a file that the computer can execute. This file is generated by the compiler and is called the object code ( .obj )
  6. Source code is portable between two different platforms while object code cannot.
  7. Variables in C++:
    1. Need to be declared fist.
    2. Can be initialized when declared( with variablename(value)).
    3. There are several built-in type:
      1. int ,short int,long int, unsigned int, unsigned short int etc
      2. float, double, long double.
      3. char unsigned char ,bool
      4. enum  (B. Strousgrup , 2013, The C++programming language, fourth edition. pp50)
    4. One can implement his own type: e.g. class.
    5. The variable with const type must be initialized when it is declared and can not be modified later.
    6.  <iosmanip>:  The standard library provides manipulators corresponding to the various format states and state changes.
    7. <vector > contains the implementation of the standard-library vector container.
    8. <Algorithm> is a collection of routines for manipulating standard-library style containers
  8. Anything declared in a scope will be destroyed when it goes out of the scope.
  9. “break”  terminates the execution of the nearest enclosing loop.
  10. “continue”  Statement to jump to the beginning of the next iteration in the nearest enclosing while statement.
  11. The output of the function should also be declared.
  12. The separation of the condition for loop is made by notation” ; “, not “,”!!!
  13. In the single statement for scopes, the braces can be omitted.
  14. Vector operations.(Notice that the v.end() returns an iterator to the memory place one past the last element.)
  15. switch: a switch selects among a set of alternatives (with case label). The expression in the case labels must be a constant expression of integral or enumeration type.
  16. Passing arguments problem: in the function, the value of the variable is not used directly. Instead, they make a copy of the value in the variable and modify the value of it.
  17. One can use “& xx” for passing arguments.
  18. One can use ” const & xx ” for passing argument by const reference.

 

Lecture 2

  1. Creating objects:
    1. Global Variable: static storage.
    2. new operator is used to create an object that we want to store it in any circumstance. For example, if we want to store a variable create during the function. In general case, if we declare the variable in the function, it will be destroyed out of the scope. Hence if we want to store it, we can use the new operator to allocate memory for storing it. Delete operator is the corresponding way of destroying a variable.
  2.  The expression ” type & b = a” means to define the b as the reference to a. i.e. no matter what value a changes into, b also changes in to that vale. Otherwise b is just initialized with value of a.
  3. If add “&” notation between the type and arguments of the function, it means that instead of returning the value of the function, we return the reference of the return of the function.
  4. POINTER!!!!!!!!:
    1. Pointer to an object is the value representing the memory address of the object.
    2. In many cases, its cheaper to pass the pointer than the whole object.
    3. The object is accessible through its pointer. (This line is important as it tells the way how we can put a function as an argument of  the other function.)
    4. Creation of a pointer: Typename & *iptr. (Here typename can be any built in type or used defined type; iptr is just the name of the pointer.)
    5. Take the value of the address of an existing object: Typename *iptr  = & a.
    6. How to access the object through its pointer : Typename i = *iptr.
  5.  The memory on the heap must be freed using the operator deleted before the end of the program. (Notice the reason is that once a new variable is created, its memory space is “locked”, no one else can store things there unless a key is insert, i.e. delete operator.)
  6. One way to do this is to use the smart pointer “->”. The smart pointer can delete automatically without using the delete operator.
  7. Pointers to function: E.g. Typedef double (*Myfuntype)(double). This line defines a type named Myfuntype which is a type of function that takes a double and return a double.
  8. Namespace: With namespaces , one can group functions, classes, typedefs, under a name.
  9. There are different naming conventions, see lecture notes 3, page 20
  10. Classes: A class is defined  to have a set of members,which can be data, function or type members. It We distinguish between class and instance of a class. (For example in the example   on page 6 of the lecture notes 4. the number takes they type complex number is called the instance of the class ComplexNumber.
  11. The instance is const argument if the member is declared const.
  12. Functions declared within a class definition are called member functions and can be invoked only for a specific variable.
  13. Data Member:  It can be plain values, references, pointers and const version of these. Const and reference members must be declare at the construction of the instance.
  14. Constructor initialize instances of classes. (For example, You construct a class called complex number. Then if you want to define a variable with type complex number, you need to initialize that variable no matter you want to put a value in it or not. Hence you need a constructor to execute this action.
  15. Default Constructor: If initialization is not done explicitly, then compiler will call the default constructor of the data member.
  16. Default constructor takes no arguments.
  17. There is one example for the constructor in the Lecture 4 page 11. The line “Explicit ComplexNumber (double, double = 0.0)”. This gives an example of the constructor and the imaginary part has already been initialized while the real part is not. It means that when we only put one number into any variable with complexnumber type, it initialized the first part of the function.
  18. A copy constructor takes an instance of the same type and create a new instance from that.
  19. The Explicit protects against implicit type conversion.
  20. For the example in the page 14 , lecture 4. The 4th line is the default constructor with initializing both dRe_ and dIm_. The fifth line is to initialize the data member with the member provided by the arguments.
  21. It is needed to use the namespace when accessing the function members of the class for implementation.
  22. Overloaded Operator: one can define the operator for the instance of the class as they wish. This is called the overlaoded operator. Computer will detect the type of the input and execute corresponding operator.
  23. There is a special syntax for the declaration of the operator members:                           Classname & operator __ (const classname  &  ) [The underlined part should be filled with the operator such as “+”]
  24. One special operator is ().
  25. The operator  “=” with argument (const reference to) the user defined class is the copy assignment.
  26. The special operator “*this” has a function that is to return a reference to the instance it was called.

Lecture 3

  1. Template functions: It gives the function to be defined based on the input argument. So the compiler will detect the type of the input provided, and then give the type to the dummy variable used in the template title( The line that looks like template<typename T>, where T is a dummy variable.)
  2. If the argument of the function uniquely determines the template type argument , then there is no need to specify the template parameter at call.
  3. If there is only one parameter in the template, then taking several input may cause compiling error as the type that should be used is not clear.
  4. In C++, one can implement template class. One may give a default type in the template line i.e.template<typename T,typename Op = std::less<T>>. If no Op is not specified, then it will take the default type.
  5. We can also do templates with non -type arguments and specialization. For example. define template function with the integer n in order to calculate the fibonacci sequence.
  6. There is also a partial templates specialization:  sometimes the function behaves differently when one of the input is of specific type.
  7. Full specialization: the behaviour might be very specific for a a particular template argument list.
  8. A (singly) linked list consists of nodes. Each node 
    1. has some data
    2. has set and get method for this data
    3. knows about the next node
    4. cal tell if it has a next node
    5. has a get method for the next node

Leave a comment