C ++ beginner

zhaozj2021-02-16  87

1. The most easy to say the beginner is to write #include #include "iostream.h"

All headers in Standard C are removed. H suffix.

2, using namespace.

Standard C In order to distinguish between the naming of variables, all functions, etc. are encapsulated under STD. If you do not write Using NameSpace STD before Main, you need to add std ::: std :: cout << "hi" .

Use a namespace example:

#include #include using namespace std; int main (int argc, char * argv []) {string s_test_string; s_test_string = "Welcome Use Std C "; cout << s_test_string << endl; return 0; }

Examples of namespaces:

#include #include int main (int Argc, char * argv []) {std :: string s_test_string; s_test_string = "Welcome Use STD C "; std :: cout << s_test_string << std :: Endl; return 0;

3, a simple type of template code:

#include #include using namespace std; template class TArray {public: explicit TArray (int sz = DefaultArraySize); TArray (type * Array, int Array_Size); TArray (const TArray & rhs); ~ Tarray () {delete [] IA;}; BOOL OPERATOR == (Const Tarray &) Const; Bool Operator! = (Const Tarray &) Const; Bool Operator = (Const Tarray &) Const; int size () const {return _size; }; Void sort (); int min () const; void display (); int fixed (); private: static const Int defaultArysize; int _size; type * ia;}; template Const IntTarray :: defaultArraysize = 12; int main (int Argc, char * argv []) {tarRay mytarray; mytarray.display (); return 0;} template tarray :: Tarray (int SZ) {_size = SZ; IA = new type [_size]; for (int = 0; ix <_size; ix ) IA [ix] = type ();} template tariff < Type> :: tarray (type * array) {_size = array_size; = new type [_size]; for (int = 0; ix <_size; ix ) ia [ix] = tarray [ix];} Template tarray :: tarray (const TARRAY & RHS) {_size = rhs._size; = new type [_size]; for (int = 0; ix <_size; ix ) IA [= = rhs.ia [xi];} template void TARRAY :: display () {for (int = 0; ix <_size; ix ) cout << "" << ix 1 << "element:" << ia [ix] << Endl } template bool tarray :: operator = (const tarray & ptarray) const {if (_size! = ptarray.size ()) {cout << "Array size is not equal to the value" << Endl } False;} for (int = 0; ix <_size; ix ) ia [ix] =

PTARRAY.IA [IX]; return true;} template bool tarray :: operator == (const tarray & ptarray) const {f (_size! = ptaRay.size ()) Return False; for (int IX = 0; ix <_size; ix ) {IF (IA [IX]! = PTARRAY.IA [IX]) Return False;} return true;} template Bool Tarray :: operator! = Const Tarray & PtaRay) const {ix (_size == ptarray.size ()) Return False; for (int = 0; ix <_size; ix ) {IF (IA [=]] == ptarray.ia [ix]) Return False;} return true;} Note: 1, BOOL Operator = (Const Tarray &) Const Heavy Duty assignment operation symbol, equal to the left side of the THIS pointer, that is, we can directly operate local variables. Other operators are overloaded.

2, IA [IX] = Type () Type () is used to assign initial values. (Null)

3, pay attention to the definition syntax of the template class.

4. Const int tarness :: defaultArraysize = 12; pay attention to the assignment of local static constants.

转载请注明原文地址:https://www.9cbs.com/read-11885.html

New Post(0)