Friday, 5 August 2016

C++ basics and hacks

1. Volatile keyword is useful when a value can change by some external code. A volatile variable is never optimized, this is very important and useful. So, it is very much used in parallel where we write a global variable which can be modified by different threads.

2. Concept of Inheritance:

  • Creating an abstract class from which other classes derive. For example we create an animal class, and classes dog and cat derive from this class. Here, animal class is called the base class and dog, cat are derived classes. 
  • Base class has more general functions and variables and every derived class has more specific features. 
  • Private variables are not accessible outside a class or to any derived classes of this class. 
  • Protected variables are not accessible outside a class but the derived classes inherit these variables and functions as a part of their own class. 
  • Public variables are accessible outside the class also.
  • A very important thing to note is that only variables are shared among different objects of derived classes of same base class and not the values in these variables. The same applies to functions. 

3. Virtual Class:

  • If you want a derived class to have a different implementation of a function mentioned in base class, you use virtual to declare it in the base class initially. 
  • For example: 
  • Another use is when u don't want to define a function in the base class but have its implementation in some derived classes. 
  • For example: 



4. Template function/class

5. Polymorphism?

  • strings can be accessed by position using str.at(pos), and not getat(pos)
  • a\*c\*d will be read as a*c*d by C++, to make \ accountable ur input should be a\\*c\\*d which will be read as a\*c\*d 
  • consequently u check for '\\' in single quotes to check if \\ (or effectively \) exists in a string. 
  • Problem: http://www.cprogramming.com/challenges/regexp.html

6. Static Variables:


  • Static memory is allocated at compile time before the program is executed, unlike dynamic memory allocation when memory is allocated as required at run time. 
  • Static global vs global: static variable has an internal linkage, the compiler does not know it exists. Two files can have same named static variable without collision but they will not be accessible in each others files. A normal global variable has an external linkage. You can refer to a global variable in different file if it is declared using extern. But, having 2 global variables of same name in different files will cause collision as compiler will get confused, 

  • In this example, there will be collision for variable a, but if they were both static, they will be limited to their own files with no collision.
  • Static local's scope is the function in which it is defines, static global's scope is the file in which it is defined. Global variable can be used in an entire project. 


Sources:
1. https://en.wikipedia.org/wiki/Static_variable

No comments:

Post a Comment