分類彙整: C++

main(), wmain() and _tmain()

Reference from

http://thedeveloperonline.wordpress.com/2008/04/10/main-wmain-and-_tmainwhat-is-the-deifference/

http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx


main(), wmain() and _tmain() are all the head functions in C++ program. wmain() and main() are different from each other becuase wmain() and _tmain() are used for programmes which needs to support unicode, while main() is for programmes which needs to support ANSI code. The choice our code will affect how you dealing with the input and output characters in command lines.

Function Pointers

Reference

http://www.cprogramming.com/tutorial/function-pointers.html

http://msdn.microsoft.com/en-us/library/zes7xw0h.aspx


Uses

  • Allowing the user to choose how to sort the data is simply to let the user pass in a function to the sort function. This function might take two pieces of data and perform a comparison on them. We'll look at the syntax for this in a bit.
  • Setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

A Line of Simple Code

void create_button( int x, int y, const char *text, function callback_func );

Syntax

void (*foo)(int);

void *(*foo)(int *);

To initialize a function pointer, you must give it the address of a function in your program.

foo = &foo_with_1_int_parameter;

foo(1); //Invoke foo_with_1_int_parameter(1);

Sometimes, different return value of the function gives different result.

In the stdlib.h, we have

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *, const void *) );

For the compare function

Compare function

Return value | Description

< 0 | elem1 less than elem2

0 | elem1 equivalent to elem2

> 0 | elem1 greater than elem2

C++ Classes

Reference:

C++ Problem Solving And Programming by Kenneth A. Barclay and Brian J. Gordon

http://www.cplusplus.com/doc/tutorial/classes/

General Format

class class_name {
  access_specifier_1:
    member1;
    function1_prototype;
  access_specifier_2:
    member2;
    function2_prototype(arg1type arg1=default_value_arg1...);
  ...
  } object_names;

If the access_specifier is private, the subsequent members are inaccessible to users of the class.

If you want to use the class, class variables are defined by employing the class tag name as a type specifier followed by a list of variables as in

class_name var_1, var_2;

To apply an operation to a class object, the object name is qualified by the member function name in a member access operator(.)

object-name.member-function-name(actual-arguments);

The scope resolution operator(::) is used to select a class member.

inline return_type class_name::function1_name(arg1_type arg1, arg2_type arg2,...){
             //Operations
             return (return_value);
}

for example,

inline void Date::set(int aDay, int aMonth, int aYear){
             //Operations
             theDay = aDay; //theDay is a private member
             theMonth = aMonth; //theMonth is a private member
             theYear = aYear; //theYear is a private member
}

Constructor is invoked implicitly by the compiler whenever a class object is established.

Constructor does support overloading

class class_name {
  access_specifier_1:
    class_name(void); //Constructor
    class_name(arg1type arg1=default_value_arg1);//Constructor
    class_name(arg1type arg1=default_value_arg1,arg1type arg2=default_value_arg2);//Constructor
    class_name(const class_name& arg1){ member2 = arg1.member2;} //copy constructor

...
    member1;
    function1_prototype;
  access_specifier_2:
    member2;
    function2_prototype(arg1type arg1=default_value_arg1...);
  ...
  } object_names;

A constructor can be used to create objects of this type using the syntax:

constructor-name(actual-argument-list);