Classes and objects (additional)---day3

Source: Internet
Author: User
Tags constructor shallow copy

1, this pointer and the memory structure distribution of class and object

1) This pointer is defined in the method of the class

2) is a hidden pointer.

3) After creating the class object, the this pointer does not point to exactly where, and if the method of the class is not called, the this pointer does not point to the action function.

When a class object invokes the method of a class to manipulate its own memory space, the this pointer points to the object of the class.

4) is the only pointer to a class that switches in real time when working with different class objects.

App.h

#ifndef App_h
#define App_h
#include <iostream>
using namespace Std;
Class Point
{
Private
    int x;
    int y;
Public:            //The operation method is generally set to common, and is called by an external function
member functions of the Ordinary class
    void SetXY (int a,int b)
    {
        this->x=a;   This pointer is hidden and can be used without writing
        this->y=b;
    }
    void Disxy ()
    {
        cout<< "x=" <<x<<endl;
        cout<< "y=" <<y<<endl;
    }
};
#endif//App_h
Class is the existence of the test segment,

Main.cpp

#include "App.h"

int main ()
{
    cout << "Hello world!" << Endl;
    Point P;      Allocate a memory space, no content, need to initialize
    P.setxy (18,27);
    P.disxy ();
    return 0;
}

P is a data type, a local variable. Executes the point P statement to allocate memory space for p based on the data member of the class. You can view the size of the space with size.

P.setxy is the starting address for a code.

2. Structure and folding function (copy construction)

2.1 constructor: is used to initialize an in-memory variable of the class's object.

#include <iostream>
using namespace Std;
Class Point
{
Private
    int x;
    int y;
Public
    Defines a constructor for a class that is used to initialize the
    Point (int a,int b)
    {
        cout<< "Constructor" <<endl;
        X=a;
        Y=b;
    }
    /*
    void SetXY (int a,int b)
    {
        this->x=a;
        this->y=b;
    }*/
    void Disxy ()
    {
        cout<< "x=" <<x<<endl;
        cout<< "y=" <<y<<endl;
    }
};
int main ()
{
    cout << "Hello world!" << Endl;
    Point P (20,90); Calling the constructor of a class directly
    P.setxy (18,27);
    P.disxy ();
    return 0;
}
Output: Constructor

X=20

Y=90

Features: 1) Defines a constructor that automatically invokes a construct on a class object when it is created to initialize a class object member.

2) function name is the same as the class name

3) No return value

4) If the definition is outside the class, add the class dependency

5) constructors can be overloaded (same as function name, but with different parameters)

What is overloading: example

	Point ()
    	{
       		cout<< "no param" <<endl;
       		x=66;
        	y=45;
   	}
   	Point (int a)
    	{
        	cout<< "one param" <<endl;
        	X=a;
        	y=88;
    	}
    	Point (int a,int b)
    	{
        	cout<< "Constructor" <<endl;
        	X=a;
        	Y=b;
    	}

Point P;

P.disxy ();

When you create a class object, the constructor is found in the class, and if you do not define a constructor, the system automatically assigns a non-argument. No error when running. If so, there will be an error.


2.2 destructor: Clears the memory of an already existing class object

 	~point ()//define destructors inside a class
    	{
        	cout<< "Clean All" <<endl;
    	}

1) No reference, no return value, but to bring a "~" declaration is a destructor

2) Each time a constructor is called to create a class object, the destructor must be automatically called before exiting to clear the object memory.

3) If the class does not have a destructor defined, the system will automatically assign a destructor.

2.3 Copy Constructors

1) The process of initializing a variable's content copy process to a new variable

Defines a copy constructor within a class

Point (const point &p)

{

cout<< "Copy Construnstor" <<endl;

x=p.x+11;

y=p.y+44;

}

Point P2 = p1; or point P2 (p1), which invokes the above function.

2.4-Depth copy constructor

1) Shallow copy vs deep copy

Char *pdest; The pointer points to a memory address that may have content. (class member)

Main.cpp: Point	p2 = p1;//copy function

    Define a copy constructor point
(const point &p)
{
cout<< "copy constructor" <<endl;
Xres = p.xres;
Yres = p.yres;


Will make P1 and P2 share to the same piece of memory, this practice is called shallow copy construction	
//debug 1:
//pdest = p.pdest;
	
Deep copy procedure
//debug 2:
pDest = new Char[strlen (p.pdest) + 1];
if (pDest! = 0)
{
strcpy (pDest, p.pdest);
}
}
	Destructors need to free up memory
	~point ()
    	{
        	cout<< "Clean All" <<endl;
        	Delete pdest;
   	}

Shallow copy hides bugs, rarely used.


3. Initialization list

Implementation of Class 3.1 member functions

Initializes a class object by invoking its member function.

P.setxy ();

3.2 Constructor Function implementation

Constructors for initialization

      Point (int a,int B): X (a), Y (b) {}  //This form, the efficiency is relatively fast

The constructor is called automatically when the class object is created, enabling initialization.

A. One is a construct, and the other is a normal member function.

B. The constructor initializes the memory space, and the member function allocates memory space for the object that defines the class first, and then initializes the member with a different frequency.

Features: Faster initialization of individual variables of class objects

The initialization list is used under what circumstances.

1. Variables declared with const are initialized,

2. To initialize a variable that uses a reference declaration

const int x;

3. Classes in a derived class to initialize part of the content of the base class

Class Space:public Point

{

Private

int z;

Public

void Setz (int c);

}

Space::space (int c):p oint (x, y)

{

z = c;

}

4. Keyword Analysis

4.1static keywords

1) can be used to declare a data member of a class

Class Point

{

Private

int x; Non-static data members

static int y; Static data members

}

If the static declaration is made, the compiler will connect this variable to the data segment at compile time.

The benefit: It will be processed during the compilation phase (allocating its own space);

Placed in the data section, after loading is a stand-alone segment, is accessible to all programs, equivalent to global variables.

How to use:

Out-of-class initialization

Declare with Static

Can be used directly: if the private declaration, only through the class's public function operation, if it is declared, can be called directly through the class, or through the class's public function operation.

#ifndef Init_h
#define Init_h
#include <iostream>
#include <string>
using namespace Std;
Class computer
{
Private
    float Unit_price;    Non-static member functions
    static float Total_price;   Static member functions
Public
    static float Total_price;
    computer (int price)
    {
        Unit_price = Price;
        Total_price + = Price;
    }
    void Disinfo ()
    {
        cout<< "Unit_price =" <<unit_price<<endl;
 cout<< "Total_price =" <<total_price<<endl 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.