Monday, October 19, 2015

Thinking in C++ note (chapter 1 - 3)


Making and using objects:
1. Declaring a variable without defining it means using the extern keyword before a description of the variable, like this:
extern int a;
The keyword is extern. It can mean the definition is external to the file, or that the definition occurs later in the file.
2. A header file is a file containing the external declarations for a library; it conventionally has a file name extension of ‘h’, such as headerfile.h.
#include "local.h" is to search for the file relative to the current directory. If the file is not found, then the include directive
is reprocessed as if it had angle brackets instead of quotes.
3. First program
#include <iostream> // Stream declarations
using namespace std;

int main() {
  cout << "Hello, World! I am "
       << 8 << " Today!" << endl;
} ///:~
C:\MinGW\sources>g++ Hello.cpp -o Hello.exe
4. system
system("ping -a www.google.com");
To use the system( ) function, you give it a character array that you would normally type at the operating system command prompt..
5. Reading and writing files
//: C02:Scopy.cpp
// Copy one file to another, a line at a time
#include <string>
#include <fstream>
using namespace std;

int main() {
  ifstream in("Scopy.cpp"); // Open for reading
  ofstream out("Scopy2.cpp"); // Open for writing
  string s;
  while(getline(in, s)) // Discards newline char
    out << s << "\n"; // ... must add it back
} ///:~

The C in C++
1. pointer
int* ip; // ip points to an int variable
int a = 47;
int* ipa = &a; //Now both a and ipa have been initialized, and ipa holds the address of a.
*ipa = 100; //Now a contains the value 100 instead of 47.
2.
Pointers work roughly the same in C and in C++, but C++ adds an additional way to pass an address into a function. This is pass-by-reference
and it exists in several other programming languages so it was not a C++ invention.
Once you assign to a void* you lose any information about what type it is. This means that before you can use the pointer, you must cast it to the correct type
int main() {
  int i = 99;
  void* vp = &i;
  // Can't dereference a void pointer:
  // *vp = 3; // Compile-time error
  // Must cast back to int before dereferencing:
  *((int*)vp) = 3;
}
3.
Global variables are defined outside all function bodies and are available to all parts of the program (even code in other files).
extern int globe;
There are restrictions to the use of register variables. You cannot take or compute the address of a register variable.
A register variable can be declared only within a block
4.
 If you want a value to be extant throughout the life of a program, you can define a function’s local variable to be static and
 give it an initial value. The initialization is performed only the first time the function is called, and the data retains its
 value between function calls.
 You may wonder why a global variable isn’t used instead. The beauty of a static variable is that it is unavailable outside the scope
 of the function, so it can’t be inadvertently changed. This localizes errors.
 #include <iostream>
using namespace std;

void func() {
  static int i = 0;
  cout << "i = " << ++i << endl;
}

int main() {
  for(int x = 0; x < 10; x++)
    func();
} ///:~

// File scope means only available in this file:
static int fs;

int main() {
  fs = 1;
} ///:~

5. const and volatile
The modifier const tells the compiler that a name represents a constant. In C++, a const must always have an initialization value
const int x = 10;
Whereas the qualifier const tells the compiler “This never changes” (which allows the compiler to perform extra optimizations),
the qualifier volatile tells the compiler “You never know when this will change,” and prevents the compiler from performing any
optimizations based on the stability of that variable.

6.
static_cast: is used for all conversions that are well-defined.
const_cast: If you want to convert from a const to a nonconst or from a volatile to a nonvolatile, you use const_cast.
reinterpret_cast: To cast to a completely different meaning.
typedef unsigned long ulong;

To read: Data Abstraction

No comments:

Post a Comment