Thursday, October 20, 2016

C++ refresher

/Volumes/BootCamp_Win7/_work/~All Codes/cpp work
/Volumes/BootCamp_Win7/_work/~OU Classes/Passed Classes

Brief history

Fortran, derived from Formula Translation, was invented in 1957 by John Backus at IBM around his 30s. “Much of my work has come from being lazy” during a 1979 interview. Fortran is the first high-level programming language and can perform relatively complex mathematical instructions such as in missile trajectories. Although old-fashioned, it is still used by physics and astronomy students due to many existing works.
C++ was invented in 1981 by Bjarne Stroustrup at Bell lab in his 30s.
Java was invented in 1995 by James Gosling at Sun/Oracle in his 40s.
Python was invented in 1991 by Guido van Rossum in his 35.

.h vs .cpp

Prefer to include the headers. They are C++ standard library headers. There are 18 <… .h> headers defined by the C standard library, but their use is deprecated.
.h: header file is for declarations. Declaration, such as int func(); tells the compiler what the class is, what data it holds, and what functions it has. It’s a interface telling user how to interact with the class.
.cpp: implementation file is for definitions, tells how the function works. The implementation details of the class are of concern only to the author of the class. Clients of the class don’t need to know and don’t care how the functions are implemented.

procedural/structured vs OO programming

structured programming: the principal idea is divide and conquer. Any task that is too complex to be described is broken down into manageable subtasks. This is very powerful. However, the deficiency is that the data structure is separated from functions and the reuse of function is difficult.
OO programming has 3 features: encapsulation (data hiding), inheritance, polymorphism, all saving you starting from scratch.
Compiling is like pre-cook food, so the compiled program can run very fast because the time-consuming task of translating the source code into machine language has already been done once.
what’s the problem I’m trying to solve?
Can this be accomplished without resorting to writing custom software?


time-honored tradition

#include <iostream>

int main()
  std::cout << "Hello World!";
  return 0;

window disappear

Run or F5, the window disappear suddenly because the program finishes. Solutions:
  1. Run without debugging (Ctrl+F5) ;
  2. add char i; std::cin >> i for input
  3. Debug->porperty->configuration properties ->Linker -> System ->SubSystem ->Console
Note: file input setting: Debug->porperty->configuration properties -> Debugging -> Command Arguments: “<input.txt”


// line comment
/* block comment */
The guideline is that comments should say why it is happening stead of what it is happening.
to get rid of prefixing cout with std::, you can declare using namespace std;
Can I ignore warning messages from my compiler?
No! Get into the habit, from day one, of treating warning messages as errors.
Every C++ program has a main() function. When your program starts, main() is called automatically.

C-style string

char s[]={'H','e','l','l','o','\0'};
char t[]="Hello"
These two lines are equivalent. ‘\0’ is the null character, which is recognized as the terminator for a C-style string

string class

#include <string>
#include <iostream>
using namespace std;
int main()
  string s1("This is a C++ string!");
  string s2=s1;
  return 0;

function overload

They must differ in terms of parameter lists, either in the types or the number of inputs. The return type doesn’t matter.


int Age= 31; //
int *pAge=0; //  initialize pointer pAge to 0, a null pointer. Otherwise, a wild pointe is very dangerous
pAge= &Age; // addressing operator

int a[2]={1,2};
cout<< a <<endl;  // array name is actually a pointer

Animal *pDog=new Animal; // allocate memory
delete pDog;  // free the memory
pDog=0; // set pointer to null, avoid deleting a wild pointer

2 libraries

Because standard template library (STL) was developed before C++ was ISO standardized, there are some overlap between STL and C++ standard library. An example is you don’t need to include to perform the mathematical operation.
#include <iostream>
using namespace std;
const double pi=3.1415926535897;   //  not included in c++ library
int main()

  return 0;

## Class and object
// LISTING 10.4 - Using Constructors and Destructors
#include <iostream> // for cout

class Cat // begin declaration of the class
public: // begin public section
    Cat(int initialAge); // constructor
    ~Cat(); // destructor
    int GetAge(); // accessor function
    void SetAge(int age); // accessor function
    void Meow();
private: // begin private section
    int itsAge; // member variable

// constructor of Cat,
Cat::Cat(int initialAge)
    itsAge = initialAge;

Cat::~Cat() // destructor, takes no action

// GetAge, Public accessor function
// returns value of itsAge member
int Cat::GetAge()
    return itsAge;

// Definition of SetAge, public
// accessor function
void Cat::SetAge(int age)
    // set member variable itsAge to
    // value passed in by parameter age
    itsAge = age;

// definition of Meow method
// returns: void
// parameters: None
// action: Prints "meow" to screen
void Cat::Meow()
    std::cout << "Meow.\n";

// create a cat, set its age, have it
// meow, tell us its age, then meow again.
int main()
    Cat Frisky(5);
    std::cout << "Frisky is a cat who is " ;
    std::cout << Frisky.GetAge() << " years old.\n";
    std::cout << "Now Frisky is " ;
    std::cout << Frisky.GetAge() << " years old.\n";

    return 0;