Strengthen the function basis using the method of about C ++

Knowledge intensive training [1] String String class
knowledge [2] Overview of inheritance and derived (understand) 1-2
inherit advantages: reducing redundant code to improve code reusability of
knowledge inherited form [3]
inheritance Category ways:
parent class classification number:
Note:
case 1: public inheritance public
summary:
case 2: protected inheritance protected
summary: protected inheritance
case 3: private inheritance private
summary: private private inheritance
summarize:
knowledge of the inner layer 4 [inheritance structure] (understanding) (vs studio)
knowledge [5 inherits the structure and sequence of destruction]
summary:
knowledge [6 sub-class has a parent class, a member of the order of construction and destruction of objects of]
summarizes important :()
knowledge point 7 [Detailed subclass structure]
1, sub-class will default to call the superclass constructor with no arguments
2, subclass must be displayed using the initialization list to call the parent class has a reference structure
8 [parent and child classes of knowledge member variable of the same name processing]
1, when the parent and child classes member variables of the same name selection sub-class members proximity principle scope of this subclass
2, if the parent class must be used in a subclass of the same name into Members must be added to the scope of the parent class.
3, a subclass can use public methods of the parent class the parent class's operation of indirect private data (data not visible)
knowledge [9 members of the same name of the parent class and subclass Processing]
Case: 1 subclass inherits all the parent class member functions and member variables
Case 2: sub-class of the same name and the parent class member functions

Knowledge intensive training [1] String class String

mystring.h

#ifndef
MYSTRING_H #define MYSTRING_H #include the using namespace STD;
class MyString {Friend the ostream &
operator << (the ostream & OUT, MyString & OB); Friend the istream &
operator >> (the istream & in, MyString & OB); Private: char * STR; int size; public: MyString (); MyString ( const char * str); MyString (const MyString & ob); ~ MyString (); int Size (void); // overloaded [] char & operator [] ( int index); // weight carrier = parameter is the object MyString &
operator = (const MyString & OB); // heavy = parameter is a string constant 
const char * MyString &
operator = (const char * STR); // overloaded + operator    
MyString & operator + (const MyString & ob ); MyString & operator + (const char
* str); // overloadedOperators bool operator(const
MyString &ob);     bool
operator==(const char *str);  };  #endif // MYSTRING_H

mystring.cpp

#include
"mystring.h" #include <string.h> #include the using
namespace STD; MyString :: MyString () {    
this-> STR = NULL;    
this-> size = 0;    
COUT << "constructor with no arguments" < <endl;} MyString :: MyString ( const char * str) {cout << "char * constructor" << endl; // space applications this-> STR = new new
char [strlen (STR) + 1'd]; // copy string strcpy (this-> STR,
STR); // update this- size> size = strlen (STR);} MyString :: MyString (const & MyString OB)
{COUT << "copy constructor" << endl; // application space this-> STR = new new
char [strlen (ob.str) + 1'd]; // copy string strcpy (this-> STR,
ob.str); // update this- size> size =
OB. size;} MyString :: ~ MyString ( ) {cout << " destructor" << endl;        
if(this->str != NULL)    
{         delete []
this-> STR; this-> STR =
NULL;}} MyString :: int Size () {return this-> size;} char & MyString :: operator [] (int
index) // array subscript index represented by {/ / index is determined whether a legitimate iF (index> = 0 && index <
this-> size) {return this-> STR [index];}    
the else {COUT << "invalid index" << endl;}} MyString & MyString :: = operator (const
MyString OB &) {// the copy ob.str to this-> str // 1 inside the this-> str points to old space freed IF (this-> str! = NULL) {        
Delete [] this-> STR;        
this-> STR = NULL;} // application according to the size of the space ob.str this-> STR = new new
char [ob.size +. 1]; strcpy (this-> STR,
ob.str); this-> size =
ob.size; * return the this;} & MyStringMyString::operator=(const char
* str) {// 1, the this-> str points to old space
freed IF (this-> str
= NULL) {Delete [] this-> str;! this-> str = NULL;}     
this-> str new new char = [strlen (STR) + 1'd]; strcpy (this-> STR, STR); this-> size = strlen (STR); * return the this;} & MyString MyString :: operator + (const
MyString OB &) {// this points to an alias str5 ob is str6    
// calculate the length of the two strings together future newSize = this- int> ob.size size +
+ 1'd; char * = tmp_str new new
char [newSize]; // Clear tmp_str space pointed memset (tmp_str, 0, newSize) ; // first this-> str tmp_str and then copied to the ob.str tmp_str appended to the    
strcpy (tmp_str, this-> str);    
strcat (tmp_str, OB. STR);     
static MyString of newString (tmp_str);    
// temporary space if tmp_str release points (tmp_str = NULL!) {        
Delete [] tmp_str; tmp_str
= NULL;} return of newString;} & MyString MyString :: operator + (const char
* STR) {// calculate future two strings length after splicing newSize = int
this-> size + strlen (STR) + 1'd; char
* = tmp_str new new char [newSize]; // empty space memset tmp_str pointed (tmp_str, 0, newSize); // first this-> str tmp_str and then copied to the string pointed to by str added in tmp_str    
strcpy (tmp_str, this-> str);    
strcat (tmp_str, str); static
MyString of newString (tmp_str); // temporary release point tmp_str space IF (tmp_str = NULL!) {        
Delete [] tmp_str; tmp_str
= NULL;} return of newString;} BOOL MyString :: operator == (const MyString
OB &) {IF ((strcmp (this-> STR,
ob.str) == 0) && (this-> == ob.size size)) {    
    return to true;}     
return to false;} BOOL
MyString :: operator == (const char * STR) {    
IF ((strcmp (this-> STR, STR) == 0) && (this-> size ==
strlen (STR))) {return to true;}     
return to false;} the ostream & operator << (the ostream & OUT, MyString
& ob) {OUT << ob.str; // accessed in ob private data must be set to OUT friend return;} the istream & operator >> (the istream
& in, MyString & ob) {// remember the original some data clearly IF (ob.str = NULL!) {        
Delete [] ob.str; ob.str
= NULL;} // get the string keyboard input char buf [1024] = ""; // temporary buf in >> buf;// get the data to the keyboard input and the actual size of the buf
open space ob.str = new
char[strlen(buf)+1];     strcpy(ob.str,
buf);     ob.size = strlen(buf);      return in; }

main.cpp

#include
#include "mystring.h" the using namespace STD; int main (int argc, char * the argv []) {MyString str1 ( "Hehe"); // custom object must override <<
(Normal global friend function realization)    
cout << str1 << endl;    
cout << "size =" << str1.Size () << endl; // custom object must override >>
(common global friend function to achieve)    
cin >> str1 ;    
COUT str1 << << endl;    
COUT << "size =" << str1.Size () << endl; MyString str2 ( "Hello
class"); // overloaded [] operator    
cout << str2 [1 ] << endl;     
// overloaded [] operator returns the value must be left in order to write the value of // overloaded [] operator returns the value must be a reference str2 [1] = 'E' ; cout << str2 < <endl; MyString Str3 ( "Hello
Str3"); COUT << "Str3:" << endl << Str3;// object str2 assigned
to Str3 // (default assignment
shallow copy) must be overridden // operator = (member functions completed) Str3 = str2;    
<< COUT "Str3:" << endl << Str3; MyString str4 ( "Hello str4");    
COUT << "str4:" << << str4 ", size =
" << str4.Size () << endl ;    
// must override operator = (member functions completed) str4 = "Hello String";    
COUT << "str4:" << << str4 ", size =
" << str4.Size () << endl;     
/ / overloaded + operator MyString str5 ( "I love you"); MyString
STR6 ( "I love the one thousand Feng"); cout << str5 + str6 << endl; MyString str7 ( " I love you"); cout <<
str7 + "I love one thousand front" << endl; // == operator overloading MyString
Str8 ( "Hehe"); MyString
STR9 ( "haha"); IF (Str8 == STR9) {        
COUT << "equal" < <endl;}    
the else {COUT << "not equal" << endl;             }      if(str8 == “hehe”)     {        
cout<<“相等”<<endl;     }    
else {cout << "not equal" << endl;} return 0;}

operation result:

Knowledge Point 2 [Overview] inheritance and derived (understand) 1-2

Inherited advantages: reducing redundant code to improve code reusability

Knowledge [3] inherited format

Definition Format derived class:
   Class derived class name: the name of the base class inheritance {// new derived class data members and member functions}; class
subclasses: the name of the parent class inheritance {// new subclass data members and member function};

Inheritance Category:

public: public inheritance (important)

private: private inheritance

protected: protected inheritance

The number of parent category:

Single inheritance: means each derived class inherits directly only feature a base class (parent class derive a subclass)

Multiple inheritance: refers to a plurality of base class inheritance a derived class, the derived class multiple inheritance directly inherited characteristics than a base class (parent class derive a plurality of sub-class)

note:

Subclasses inherit the parent class, subclass has all of the parent class member variables and member methods (in addition to constructor and destructor member methods), but in the subclass inherits the member does not necessarily directly accessed, different inheritance approach will lead to different access rights.

Case 1: public inheritance public

// set a parent class Base {public class: int
A; Private: int B; protected: int
C;}; // set a subclass of
class Son: public Base {public: // public data of the parent class 
in the sub class is also public    
Private data // parent class is a subclass of the protected data is not visible // parent class is an internal void showSon // subclass protected in subclass () {        
// B = 200; // not directly accessible to c = 300; // internally accessible subclass}}; 
void Test01 () {// external son ob subclass; ob.a = 100; cout << "parent the public data = a
"<< endl << ob.a;     
//ob.b = 200 is; // not accessible outside subclass //ob.c = 200; // not accessible outside subclass}

to sum up:

Public class Parent data is in the public subclass

Private data of the parent class
in the subclass is not visible

Parent class protected data is protected in a subclass of

(Public inherits the parent class's private data is not visible to other intact in a subclass)

Case 2: protected inheritance protected

// SON1 protected inheritance class: protected Base {Private: public:    
Private data // public data of the parent class in the subclass is the parent class // protected
in the subclass is not visible in the parent class // data is protected in the subclass subclass // protected internal void showbase () {a = 100 ; // internal subclasses access // b = 200; // not directly accessible to c = 300; // subclass internal accessible}}; void test02 () { Son1 ob; //ob.a;// subclasses outer inaccessible //ob.b;// subclass subclass outer inaccessible //ob.c;// outer inaccessible}

Summary: protected inheritance

Public data of the parent class 
in subclasses are protected

private data in the parent class subclass is not visible

Parent class protected data
in the protected is a subclass of

(Protection class inherited his father's private data is not visible to the subclass other data protection has changed)

Case 3: private inheritance private

// Son2 protected inheritance class: private Base {private: 
public: // public data of the parent class 
is a subclass in the private // private data in the parent class
is not visible in the parent class // subclass data is protected in the subclass subclass // private internal void showbase () {a = 100 ; // internal subclasses access // b = 200; // not directly accessible to c = 300; // subclass internal accessible}}; void test03 () { Son2 ob; //ob.a;// subclasses outer inaccessible //ob.b;// subclass subclass outer inaccessible //ob.c;// outer inaccessible}

Summary: private private inheritance

Parent class public data is private subclass

Private data of the parent class
in the subclass is not visible

Parent class protected data
is private in the subclass

(Private inherit the parent class's private data is not visible in other subclasses become private)

to sum up:

No matter what inheritance: the parent class's private data is not visible in a subclass

Knowledge of the inner structure of inherited [4] (understanding) (vs studio)

class Base {
public:     int a; protected:     int b; private:     int c; }; 
class Son :public Base { public:    
int d;     int e; }; int main(int
argc, char* argv[]) {     cout <<
sizeof(Son) << endl;     return 0;
}

step:

cl /d1
reportSingleClassLayoutSon test.cpp

Son class layout:

[5 sequence to the succession of the constructor and destructor knowledge]

{Base class
public: Base ()    
{COUT << "no parent class constructor parameter" << endl;} ~ Base ()    
{COUT << "parent class destructor" << endl;}};
son class: public Base {public:    
son () "without reference constructor class" << endl << {COUT;} ~ son ()    
{COUT << "subclass destructor" << endl;} }; void
Test01 () {Son OB1;}

operation result:

to sum up:

Construction order: the parent class (class-yl) configured ------> subclass (derived class) configured

Destructor sequence: subclass (derived class) destructor ------> parent (base class) destructor

[6 knowledge parent class has a subclass, order of the objects in the constructor and destructor member]

Construction and object members parent class constructor and destructor destructor subclasses own constructor and destructor

:( summarize important)

Other class
{public: Other () {        
COUT << "object constructor member" << endl;}    
~ Other () {COUT << "destructor object members" << endl;}};
class Base { public: Base () {        
COUT << "no parent class constructor parameter" << endl;}    
~ Base () {COUT << "parent class destructor" << endl;}};
class Son: public Base {public:    
son () {COUT << "class constructor with no arguments" << endl;} ~ son ()    
{COUT << "subclass destructor" << endl;} Other ob; // object members}; void test01 () {Son ob1;}

operation result:

Detailed knowledge [7 constructor class]

1, sub-class will default no-argument constructor calls the parent class

2, subclass must show there is a reference configuration using the initialization list to call the parent class

Call form: name of the parent class.

Son(int
a,int b):Base(a),b(b) {     //this->b
= b; }

{Base class
Private: int A; public: Base ()    
{COUT << "no parent class constructor parameter" << endl;} Base (int A) {        
this-> A = A;        
COUT << "parent there argument constructor "<< endl;}    
~ Base () {COUT <<" parent class destructor "<< endl;}};
class Son: {Private Base public: int
B; public: Son ( ) {        
"class constructor with no arguments" COUT << << endl;}    
son (int B) {this-> B = B; "int argument constructor has subclasses" << endl << COUT;    
} / call the parent class / subclass must initialize the display list has a parent class name // configuration parameters (parameters)    
son (a int, int B): there is a reference configuration Base (a) // call the parent class display {        
the this -> B = B;        
COUT << "there subclass argument constructor int
int" << endl;    Son ~} ()
{COUT << "subclass destructor" << endl;}};
void test01 () {// subclasses default constructor with no arguments to call the parent class // Son ob1 (10); // call the parent class with subclasses must initialize the display list has a reference name of the parent class configured @ + ( ) Son ob2 (10,20);}

operation result:

Case improve:

If the parent class has constructor parameters:

Base (int a, int data) {this-> a = a; this-> data = data; "has a parent argument constructor" cout << << endl;}

Subclass want to call the parent class constructor has parameters:

// Subclasses must
call the parent class show initialization parameters configured with a list of // parent class name (parameter) Son (int a, int b , int c): Base (a, c), b (b) // call the parent class has a display configuration parameters {// this-> b = b; " there subclass argument constructor int int" cout << << endl; }

8 knowledge [of the same name member variables of the parent class and subclass processing]

1, when the parent and child classes member variables of the same name selection sub-class members proximity principle scope of this subclass

2. If you must use a member of the same name in the parent class in the subclass must be added to the scope of the parent class.

}}; void test01 () {Son OB1 (10,20); ob1.showNum (); }










operation result:

3, a subclass can use public methods of the parent class the parent class's operation of indirect private data (data not visible)

Base class
{Private: int NUM; // private data of the parent class when it comes to inherit not visible in public subclasses: Base (int
NUM) {this-> NUM = NUM; COUT << "Base configuration parameters have int" < <endl;    
} ~ Base () {        
COUT << "destructor" << endl;}    
int getNum (void) {return NUM;}}; 
class Son: {Private Base public:    
int NUM; public: Son (int num1, int
num2): Base (num1) {this-> NUM = num2; COUT << "there are parameters configured int int" << endl;    
} ~ Son () {        
COUT << "destructor" << endl; }    
void shownum (void) {// if you must use the same name as the parent class members in the subclass must add "num = parent class" << getNum scope cout << parent class () << endl; // proximity principle selected "num = subclass" members of this subclass scope cout << when a parent and child classes when the member variables of the same name in the subclass << num << endl;}};
void test01 () {Son
OB1 (10,20); ob1.showNum (); }

operation result:

Knowledge [9 members of the parent and child classes of the same name as a function of processing]

Case: 1 subclass inherits all the parent class member functions and member variables

{Base class
public: void FUNC (void) {        
"void parent class << COUT
FUNC" << endl;} void FUNC (int A) {        
COUT << "parent class A FUNC = int
" << A endl <<;}
}; Son class: public Base {public:}; 
? excluding void test01 () {// constructor and destructor why parent class constructor and destructor parent only he knows how to do (and configuration the system automatically calls the destructor) // subclasses inherit all the parent class member functions (except the constructor and destructor functions) and member variables    
son ob1; ob1.func (); // access to the parent class is void func (void)    
ob1.func (10); // access to the parent class func (int a)}

Case 2: sub-class of the same name and the parent class member functions

{Base class
public: void FUNC (void) {        
"void parent class << COUT
FUNC" << endl;} void FUNC (int A) {        
COUT << "parent class A FUNC = int
" << A endl <<;}
}; son class: public Base {public: // Once subclasses implement the member function of the same name as the parent class of the shield member with the same name for all the parent class function FUNC void (void) {        
COUT << "subclasses voidfunc "<< endl;}}; 
? except void test01 () {// constructor and destructor why the parent class's constructor and destructor only parent they know how to do (constructor and destructor automatically calls) // child class inherits all the parent class member functions (except the constructor and destructor functions) and member variables    
son ob1; ob1.func (); //ob1.func(10);//err // Once subclasses of the parent class implements of the same name member function
will block all the same name as the parent class member functions // If the user
must call the same name as the parent class member functions must be added to the scope    
ob1.Base :: func (); // call the parent class void func ob1.Base :: func (10); // call the parent class int func} int main (int argc,char *argv[]) {     test01();     return 0; }

operation result:

Published an original article · won praise 0 · Views 82

Guess you like

Origin blog.csdn.net/zeminglin/article/details/105108140