Ⅰ Class and Object ②

2.3 Copy constructor

2.3.1 Function

Assign the data of the previous object to the new object

The copy function (formal parameter) is a reference to the object of this class &

2.3.1 When is it used?

 

 

 

2.4 Shallow copy and deep copy

 

If you modify the contents of s, scopy will also change accordingly. What is the difference between copied and not copied? .

Reason: the two pointers refer to the same place, the memory area is unchanged (s = sObj.s;)

 

 The two refer to different places, s new char has a new place, it will not change with s

Example 1: Design circle

 

#include <iostream>
using namespace std;

const float PI = 3.1416;

class CCircle
{
    float x, y;
    unsigned long clr;
    float r;
public:
    CCircle(float _x=0, float _y=0, float _r=0, unsigned long _clr=0x00FF00): x(_x), y(_y), clr(_clr), r(_r)
    {                        //构造函数,默认初始化,
    }
    double GetArea()
    {
        return PI*r*r;
    }
    double GetPerimeter()
    {
        return 2*PI*r;
    }
    void Move(float dx, float dy)
    {
        x += dx;
        y += dy;
    }
    void Scale(float s)
    {
        r = s*r;
    }
    void Draw()
    {
        cout << "Circle:" << x << "," << y << ", " << r << endl;
    }
};

int main()
{
    CCircle myCircle(300.0f, 400.0f, 1.5f, 0xFF0000);

    myCircle.Move(3.0f, 4.0f);
    myCircle.Scale(2.0f);

    myCircle.Draw();

    cout << myCircle.GetPerimeter() << ", " << myCircle.GetArea() << endl;

    return 0;
}

 

#include <iostream> 
#include <graph2d.h>    // Written by the teacher, it is amazing, there are some 
using  namespace std;
 using  namespace graph;   // emmm like this. . 

class CCircle 
{ 
    float x, y; 
    unsigned long clr;
     float r;
 public : 
    CCircle ( float _x = 0 , float _y = 0 , float _r = 0 , unsigned long _clr = 0x00FF00  ): x (_x), y (_y) , clr (_clr), r (_r)
    { 
    } 
    double GetArea()
    {
        return PI*r*r;
    }
    double GetPerimeter()
    {
        return 2*PI*r;
    }
    void Move(float dx, float dy)
    {
        x += dx;
        y += dy;
    }
    void Scale(float s)
    {
        r = s*r;
    }
    void Draw()
    {
        setColor(clr);
        fillCircle(x,y,r);
    }
};

CCircle myCircle(300,400,50.5f,0xFF00FF);  //布吉岛啥意思

void display()
{
    myCircle.Draw();
}

void keyboard(unsigned char key)
{
    switch(key)
    {
    case 'w':
        myCircle.Move(0,5);
        break;
    case 's':
        myCircle.Move(0,-5);
        break;
    case 'a':
        myCircle.Move(-5,0);
        break;
    case 'd':
        myCircle.Move(5,0);
        break;
    case 'z':
        myCircle.Scale(1.05);
        break;
    case 'x':
        myCircle.Scale(0.95);
        break;
    }
}

int main()
{
    myCircle.Move(3.0f, 4.0f);
    myCircle.Scale(2.0f);

    cout << myCircle.GetPerimeter() << ", " << myCircle.GetArea() << endl;
    initGraph(display,keyboard);
    return 0;
}

 

 

 

 ## 1. Structure and function

 

 ## Second, coordinate system and function naming

Function naming adopts camel-style nomenclature, that is, when the function name is connected by one or more words, the first word starts with a
lowercase letter, and the first letter of the following word is capitalized, as shown in the showCoordinate function of the display coordinate system above.

The coordinate system is normal, the origin is in the lower left corner

Press Esc to exit the interface, press F1 to display the help information, press F2 to display the coordinate system, press 'q' to
switch between the window and the full screen

## 3. Operating Mechanism

Callback:

The pointer of function 1 is passed as a parameter to function 2, and function 1 is controlled by manipulating function 2

Not directly called by the user, but when a specific event or condition occurs (such as a keyboard being pressed, a mouse click, etc.), the
operating system calls and sends a message to the function to respond to the event or condition.

 

Guess you like

Origin www.cnblogs.com/syzyaa/p/12690894.html