1. The class is an abstract data type itself does not take up memory space, when the assignment will take up memory space for data, and a method of operating the data package.
class Student { protected : int Age; // member variable String name; public : void SLEEP () {}; // member functions void Study () {}; void EAT () {}; · };
Permissions Members of the class divided into three types: public, private, protected
public | Can be accessed directly by objects outside the class |
public | Foreign hidden subclasses open, inner classes use (member functions in the class use) |
private | Foreign hide, only (member functions in the class use) within a class |
Without the permission | The default permissions are private, foreign Hide |
2. Construction and destructor
Constructor
1. The name of the class name and the same function
2. The function has no return value (not a void)
3. After you create a class object is automatically called (can not call themselves)
4. If no custom constructor, the compiler will automatically generate a system with no argument constructor
Destructor:
1. The function name is added in front of the class name ~
2. The function does not return a value, it also has parameters
3. The function is automatically called when an object is destroyed
4. If no custom destructor, the system will automatically generate a destructor
#include <iostream> using namespace std; class Person{ public: The Person () // constructor { cout<<"Person()"<<endl; sex = "女"; } The Person ~ () // destructor { cout<<"~Person()"<<endl;
}
private:
int age;
string name;
string sex;
protected:
};
Constructor arguments is as follows:
class Person{ public: The Person ( String S, char * n-, int Age) // constructor with three parameters { cout<<"Person()"<<endl; sex=s; name=new char[32]; strcpy(name,n); this->age=age; } }
If no other constructors, must be developed in time to create the object of these three parameters
Person jack ( "male", "jack", 18) ; // parameter passed to the constructor
this pointer: class member function pointer argument implicitly an object, this represents who call the function, who then use this pointer, this pointer is generally used to distinguish conflict member variables and functions of local variables .
Overloaded constructor
Person jack; // class can be overloaded Person :: Person ();
Person jack("男","jack",18) Person::Person(string s,const char *n,int age)
class Person{ public: Person() { cout<<"Person()"<<endl; sex="男"; name=new char[32]; strcpy(name,"jack"); this->age=19; } // constructor overload of the Person ( String S, const char * n-, int Age) { cout<<"Person(string,char *,int")<<endl; sex=s; name=new char[32]; strcpy(name,n); this->age=age; } }
The default constructor parameters
class the Person { public : // Constructor the Person ( String S = " M " , const char * n-= " Jack " , int Age = . 19 ) { cout<<"Person(string,char *,int")<<endl; sex=s; name=new char[32]; strcpy(name,n); this->age=age; } }
// Create Object
Person jack0; // call the constructor with no arguments
Person jack ( "M", "jack", 18); // constructor arguments to
Constructor - initialize the members of the parameter list format:
Person(string s="男",char *n="jack",int age=19):sex(s),age(age){...}
The contents of brackets (parameters, constants ..) assigned to members outside the brackets, members of the initialization parameter list, create a class object in space at the same time when the application immediately (with brackets values) is initialized
Application: 1 member variables are constant
2. inherited when call the constructor of the parent class
Class member function declared in class and outside class implementation
// member function in the class declaration class the Person { public : The Person ( String S = " male " , char * the n-= " Jack " , int Age = 19 ) // declare a partial write default parameter ~ the Person (); void Show (); Private : int Age; char * name; String Sex ; protected : } // member function implemented outside the class the Person :: the Person ( String S, char * n-, int Age): Age (Age), Sex (S) {} // achieve initialization parameter list of some members of the Person :: ~ the Person () {} void the Person :: Show () {}
Copy constructor
1. constructor is a copy constructor, is a reference to an object
2. If no custom copy constructor, the system will automatically generate a default copy constructor (shallow copy)
3. The following situations will call the copy constructor
Person jack("男","jack",19); Person rose=jack; Person rose(jack);
--- The default is shallow copy, do not need to go to achieve, format: Person (Person & p);
class the Person { public : // constructor is declared the Person ( const char * name = " Jack " , String Sex = " man " , int Age = 18 is ); // copy constructor the Person (the Person & P); ~ the Person (); void Show (); Private : char * name; String Sex; int Age; } // constructor Dun implement the Person :: the Person ( const char * name, String Sex, int Age): Sex (Sex), Age (Age) { cout<<"Person()"<<endl; this->name=new char[32]; strcpy(this->name,name); } Person::Person(Person& p) { COUT << " the Person (the Person & P) " << endl; // shallow copy of an object member only assign the this -> Age = p.age; the this -> Sex = p.sex; the this -> name = p.name ; } Person::~Person() { delete []name; } void Person::show(){}
--- need deep copy themselves to achieve, while the space itself but also a copy of the object pointed to copy heap space members
#include <iostream> #include <cstring> using namespace std; class the Person { public : // Constructor declared the Person ( const char * name = " Jack " , String Sex = " man " , int Age = 16 ); // copy constructor the Person (the Person & P); ~ the Person (); void Show (); Private : char * name; String Sex; int Age; } // constructor implement the Person :: the Person ( const char * name, String Sex, int Age): Sex (Sex), Age (Age) { cout<<"person()"<<endl; this->name=new char[32]; strcpy(this->name,nama); } Person::Person(Person& p) { cout << " the Person (the Person & " << endl; // deep copy - a copy of a copy of the object space while also pointing to the heap of the members of the this -> Age = p.age; the this -> Sex = p.sex; the this -> name = p.name; strcpy(this->name,p.name); } Person::~Person() { cout<<"~Person()"<<endl; delete []name; } void Person::show(){} int main () { // create an object the Person Jack ( " man " , " Jack " , 20 ); jack.show(); // copy constructor create objects Person rose (jack); rose.show(); return 0; }
PS: Where if there is wrong, please correct me, we learn from each other.