Basic of C++

来源:互联网 发布:不用交钱的手机网络兼职 编辑:程序博客网 时间:2024/05/08 15:44

Data abstraction and encapsulation:

Encapsulation: conceiling of the implementationdetails of a data object from the outside world.

Abstraction: theseperation between the specificationof a data object and its implementation.

data type is a collection of the objects and a set of operations that act on those objects.

ADT (Abstracted Data Type): ::= should be read as "is defined as". 

Basic of C++

Two kinds of files: header files (store declaration) and source files

individually compiled, linked and loaded.

 

To ensure that a header file is never included twice, the contents of a header file are enclosed inside the following preprocessor directives:

#ifndef FILENAME_H

#define FILENAME_H

//insert contents of the header file here

.

.

#endif

 

Scope in C++:

1) use the scope operator :: to access the global variable

2) use "extern" to declare a global variable within the same value in diff files

3) use "static" to declare a global variable with different values in diff files

 

1.4.3 C++ Statements and Operators

new

delete

<< (input)

>> (output)

1.4.4 Data Declarations

constant values, variables, constant variables (cannot be assigned a value, ex: const int MAX = 500), enumeration type (declare a series of integer constants: enum), pointers, reference types (provide an alternative name for an object: int& j = 1;)

 

1.4.5 Comments in C++

/* */

//

 

1.4.6 Inputs/Outputs in C++

#include <iostream.h>

cout<<;

cin>>; //>> is used to seperate variables being input

format-free; IO operators can be overloaded

 

File I/O: fstream.h

 

1.4.7 Function

two kinds of functions: regular functions and member functions

a function consists of a name, a lists of arguments or signature (input), a return type and the body.

 

1.4.8 Parameters Passing in C++

arguments are passed by value(local copy, doesn't affect actual arguments) or reference (by the address).

For retaining the advantages of both parameter-passing methods is to pass constant references such as const T&a, where T is the type of the argument

array types are passed by reference, any changes made to the array inside the function are reflected in the actual array.

 

1.4.9 Function Name Overloading

There can be more than one function with the same name as long as they have different signatures.

 

1.4.10 Inline Functions

tells the compiler that any calls to "function name" must be replaced by the body of the function. inline and const can be used to eliminate the use of preprocessor directives such as #define

 

1.4.11 Dynamic Memory Allocation

"new" operator creates an objective of the desired objective and returns a pointer to the data type that follow it. deleted by applying the "delete" operator to a pointer to the obj.

They also can be used to create and delete an array of objs: delete []a;

 

1.5 Algorithm Specification

1.5.1 Intro

Algorithm is a finite set of instructions that accomplishes a particular tast: input, output, definiteness, finiteness, effectiveness

 

1.5.2 Recursive algorithms

Recursion is similar to the method of induction which is often used to prove mathmatical statement. Write a function to produce an output for some input (n) by assuming that the same function will compute the correct output for input n - 1. A recursive function requires a termination condition.

when to use? 1) the problem is recursively defined

To understand a recursive function, you must

1) formulate in your mind a statement of what it is that the function is supposed to do for a given input

2) verify that the function does achieve its goal if the recursive invocations to itself do what they are supposed to

3) ensure that a finite number of recursive invocations eventually lead to an the terminate

4) perform the correct computations if it terminates.

Recursion is not a simple call itself or interactively control.  The core of recursion is conquer-and- divide. This technique can solve most complex problems, but not all. The problem must satisfy two criteria:

 

1) it can be split into multiple small same problems.

2) it can terminate through a easy exit.

 

Notice that the termination must be written at the beginning of the function.

The way of showing results: 1) print alone the procedure 2) print when base case happens: print the base and back tract print all results, or print whole bunch of results if the results are store in a consist data structure one by one.

 

The deficiency of recursion:

when the steps of calling itself is more than  the real processing, recursion is not a good choice.

The differences between loop, iterate, recursion and  traversal:

1) Loop: repeat processing same code block, ex: while

2) Iterate:  repetition of a process within a computer program.

3) Recursion: repetition of calling itself

4) Traversal: Travel alone the branches within a tree.

1.6 Performance analysis and measurement

criteria concerning performance: space complexity and time complexity

into two phases: performance analysis and performance measurement

 

1.6.1 Performance analysis

1.6.1.1 Space Complexity

following components: a fixed part and varaiable (depend on the problem, referenced variables and recursion stack queue)

S(P) = c + Sp (instance characteristics)

 

1.6.1.2 Time Complexity

T(P) is the sum of the compile time and the run time (tp)

program steps.

 

1.6.1.3 Asymptotic Notation

Our motivation to determine step counts is to be able to compare the time complexities of two programs that compute the same function and also to predict the growth in run time as the instance characteristics.

Little advantage in determing the exact values of the constants. Becase for small values of n, either program could be faster, and inexact program steps.

 

The function f andg are nonnegative functions.

Definition [Big "oh"]: f(n) = O(g(n)) iff there exist posive constants c and n0 such that f(n) <= cg(n) for all n, n >=  n0.

 

Explanation and proof: Find the highest exponent number, and add one to that number. Then find the solution of n in the new inequal formular.

 

O(1) means a computing time is a constant. O(n) is called liner, O(n2) is called quadratic, O(n3) is called cubic, and O(2n) is called exponential. If an algorithm takes time O(log n), it is faster than O(n). Similarly, O(nlogn) is better than O(n2) but not as good as O(n).

 

For the statement f(n) = O(g(n)) to be informative, g(n) should be as small a function of n as one can come up with for which f(n) = O(g(n)).

 

Theorem 1.2: if f(n) = amnm + ... + a1n + a0, then f(n) = O(nm)

 

Defination: [Omega] f(n) = Ω(g(n)) iff there exist positive constants c and n0 such that f(n) >= cg(n) for all n, n >= n0.

 

Theorem: If f(n) = amnm + ... + a1n + a0, then f(n) = Ω(nm)

 

Defination: [Theta] f(n) = Ө(g(n)) iff there exist positive constants c1, c2, and n0 such that c1g(n) <= f(n) <= c2g(n) for all n, n >= n0.

The theta notation is more precise than both the "big oh" and omega notations, f(n) =Ө(g(n)) iff g(n) is both an upper and lower bound on f(n).

 

Theorm 1.4: If f(n) = amnm + ... + a1n + a0 and am > 0, then f(n) = Ө(g(n))

 

Notice that the coefficients in all of the g(n)'s used in the preceding three examples have been "1".

When deciding which of the two programs to use, we must know whether the n we are dealing with is sufficiently large.

 

1.6.2 Performance Measurement

Concrening with obtaining the actual space and time requirements of a program. These quantities are dependent on the particular compiler and options used as well as on the specific computer on which the program is run.

 

1.6.3 Generating Test Data

For each set of values of the instance characteristics of interest, we generate a suitably large number of random test data. It is desireable to analyze the algorithm being tested to determine classes of data that should be generated for the experiment.