CH1 better C.
- Operator overloading specifies that there must be a custom type for the user
- Some functions that the output does not notice:
float x = 123.456, y = 12345;
Cout.precision (2); Show two valid values, others are scientific notation
cout << x << "" << y << Endl;
?
COUT.SETF (ios::showpoint);//Display the end of the 0
cout << x << "" << y << Endl;
?
COUT.SETF (Ios::showpos);//Display symbol
cout << x << "" << y << Endl;
- Print Address
Printf ("%p", s);
cout<< "Address:" <<&s;
?
CH2 pointer
- Distinguish the position of the const
Const Char *p; Note that *p is constant and cannot be changed.
char* const P; Note that P is constant and cannot be changed.
Depending on the position of the *.
- Initialize two-dimensional pointers
int **a;
A = new int*[3];//line
???? for (int i = 0; i < 4; ++i)
???? {
???????? A[i] = new int[4];//column
????}
Delete[] A;
?
?
???? int A[][4] = {{1,2,3},{4,5,6},{7,8,9}};
???? int (*p) [4] = A;
?
???? P[i] = = * (p+i);
???? P[I][J] = = * (p[i]+j) = = * (* (p+i) +j);
?
?
???? size_t totals = sizeof (a);//entire number of bytes
???? size_t totalcols = sizeof (a[0]);//The entire column of bytes
???? size_t rows = sizeof (a)/sizeof (a[0]);
???? size_t cols = sizeof (a[0])/sizeof (a[0][0]);
???? for (int i = 0; i < rows; ++i)
???? {
???????? for (int j = 0; j < cols; ++j)
???????????? cout << P[i][j] << "";
???????? cout <<endl;
????}
???? cout << rows << "<< cols << Endl;
- function pointers
Pointers to non-member functions:
#include <Windows.h>
#include <iostream>
?
using namespace Std;
?
Int (*FP) (int,int);//Global pointer variable
void (*farray[]) (void); Can be defined outside the main function or internally. Array of function pointers
int Max (int a, int b)
{
???? if (a > B)
???????? return A;
???? Else
???? return b;
}
int main ()
{
???? fp = Max;
???? int a = FP (UP);
???? cout << a << Endl;
return 0;
}
?
Pointers to member functions:
#include <Windows.h>
#include <iostream>
?
using namespace Std;
?
Class C
{
Public
???? void F () {cout << "c::f \ n";};
???? void G () {cout << "c::g \ n";};
???? int Max (int a, int b)
{
???? if (a > B)
???????? return A;
???? Else
???? return b;
}
};
?
int main ()
{
c C;
void (C::* pmf) () = &C::f; Defines a pointer to a member function
(C.*PMF) ();//A pointer to a member function that is slightly different from the pointer of a non-member function and has more objects. *
PMF = &C::g;
(C.*PMF) ();
?
Int (C::* fp) (int,int) = &C::Max;
int aa = (C.*FP) (3,4);
cout << AA << Endl;
return 0;
}
Array of pointers to member functions
- #include <Windows.h>
- #include <iostream>
- ?
- using namespace std;
- ?
- class Object
- {
- Public:
- ??? void Retreve () {cout << "object::retreve";}
- ??? void Insert () {cout << "object::insert";}
- ??? void Update () {cout << "object::update";}
- ??? void process (int choice);
- Private:
- ??? typedef void(Object::* OMF) (); //From the alias up good
- ??? Static OMF Farray[3]; //member function pointer array
- ?
- };
- OBJECT::OMF Object::farray[3] = {
- ?? &object::insert,
- ?? &object::retreve,
- ?? &object::update
- };
- void Object::p rocess (int choice)
- {
- ??? if (Choice >=0 && choice <=2)
- ??? {
- ?????? (this->*farray[choice]) ();
- ?????? cout << Endl;
- ???}
- }
- int Main ()
- {
- ??? Object o;
- ??? for (;;)
- ??? {
- ?????? int Choice;
- ?????? CIN >> Choice;
- ?????? if (Choice >=0 && choice<=2)
- ????????? O.process (choice);
- ?????? Else
- ?????????? break;
- ???}
- ??? return 0;
- }
- Encapsulation vs. Incomplete types
The implementation of a class similar to the source class is to redefine a class similar to the source class and then repack it.
?
CH3 Preprocessor
- Debug mode can run under release under Run, with macro definition implementation
#define DEBUG 1
?
int main ()
{
int i = 2;
#if DEBUG
cout << "Debugmod" << Endl;
#endif
return 0;
}
?
The following sentence can also achieve the above function
?
#ifdef _DEBUG
cout << "Debugmod" << Endl;
#endif //_DEBUG
?
?
To mix the new C + + code with the old C code, you need to add the following statement
extern "C" void f (); F () is compiled in C environment
- Character
Enter \ r newline \ \b Warning \a
?
?
?
One of the CH4 C standard libraries: for qualified programmers
1. <ctype.h>
Common functions: Is series. Eg:isupper (); Islower (); Isspcace () and so on.
char a = ' a ';
int B = Isalpha (a);
Instance:
- #include <Windows.h>
- #include <iostream>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- ?
- long Atox (char *s)
- {
- ??? while (Isspace (*s)) //Kill the opening space, as long as there is a nonzero number that jumps out
- ?????? s++;
- ??? long sum;
- ??? for (sum = 0L; Isxdigit (*s); ++s)
- ??? {
- ?????? int Digit;
- ?????? if (IsDigit (*s))
- ????????? digit = *s-' 0 ';
- ?????? Else
- ????????? digit = ToUpper (*s)-' A ' + 10;
- ?????? sum = sum*16l + digit;
- ???}
- ??? return sum;
- }
- ?
- long Atox (char*s)
- {
- ??? char xdigs[] = {0123456789ABCDEF};
- ??? long sum;
- ??? while (Isspace (*s))
- ?????? s++;
- ??? for (sum = 0; Isxdigit (*s); ++s)
- ??? {
- ?????? int digit = STRCHR (Xdigs,toupper (*s))-xdigs; //Find the position of the pointer, that is, the position of the number (0-15 values) to find the position of the first occurrence of the string (*s) in Xdigs.
- ?????? sum = sum*16l + digit;
- ???}
- }
- long Atox (char *s)
- {
- ??? long n = 0;
- ??? SSCANF (S, "%lx", &n); Reference code: http://www.91linux.com/html/article/program/cpp/20081130/14121.html
- ??? return N;
- }
- int Main ()
- {
- ??? Char* s = "123 abc";
- ??? //Method one
- ??? long t = Atox (s);
- ??? //Method two
- ??? Char* * ss = NULL;
- ??? long tt = Strtol (s,ss,16);
- ??? cout << tt << Endl;
- ??? return 0;
- }
?
C and C + + code pristine NOTE 1