Ditte Software College C + + Course Learning Notes Summary

Source: Internet
Author: User
Tags shallow copy

Depth profiling C + +

1, the class is a model, this model can create a corresponding entity. A class does not necessarily have a corresponding entity, but an entity must belong to a class.

2. Classes are used to abstract describe the properties and behaviors held by a class of things, and objects are specific things that have all the properties and behaviors described in the owning class.

3. Classes must all originate from life, and two object instances cannot be identical.

4. Basic relationships between classes: inheritance and composition.

Inheritance: There is an inheritance relationship (IS-A) between the class that is broken down from the existing parent class and the original class, and the inherited subclass has all the properties and methods of the parent class .

Composition: The existence of some classes must depend on other classes, and the combined classes are made up of other classes on a local level, and the combined relationship is the relationship between the whole and the local (life and death, such as the computer class by CPU class, memory class, graphics card class, etc.)

5, a class is usually divided into the following two parts: the implementation details of the class (the implementation of the Class) and how the class is used (the declaration of the Class); the classes in C + + support the separation of declarations and implementations; The implementation and definition of a class is separated by the.

The public level of properties and behaviors must be defined in the representation of the class, similar to the permissions of files in the file system. One thing to note is that access rights public and private are for access outside of the class, and both public and private are accessible to member variables and member functions, meaning that the scope of the class member is not related to the access level.

6, the main difference between struct and class in C + + is that the default access level of member variable and member function is different, the default access level of struct is public, and class is private.

7, from the point of view of program design, the object is only a variable, his type is a class type, so when creating an object on the stack, the member variable initial value is a random value (Eg:test t), when the object is created on the heap, the member variable initial value is a random value (Eg:test T =test () or Test *t = New Test, note the difference between the two, the former is a copy of a temporary object (the constructor is essentially a temporary object), the latter is the definition of the class object pointer); When creating an object in a static store, the member variable has an initial value of 0 (eg: global variable).

8. The constructor initializes the state of the object after the object is created , and the initialization list is executed before the constructor, which initializes the object when the object is created .

9, the constructor does not return a value, but can have parameters, which can be overloaded, and the destructor has no return value and no parameters.

Classic code Example:

#include <stdio.h>
#include <iostream>
/*
#include "DataStruct.h"
#include <array>
*/
class Test
{
Private:
int i;
Int J;
Public :
Test ()
{
i=0;
j=0;
}
Test (int v1,int v2)
{
I=v1;
J=v2;
}
~test ()
{
printf ("I am the destructor \ n");
}
void Sleep ();
void SetValue (int m,int n);
int getvalue_i ();
int Getvalue_j ();
void Eat ();
};
void Test::sleep ()
{
printf ("I am the sleeping action \ n");
}

void Test::eat ()
{
printf ("I'm a meal action \ n");
}
void Test::setvalue (int m,int N)
{
i=m;
j=n;
}
int test::getvalue_i ()
{
return i;
}
int Test::getvalue_j ()
{
return J;
}

int main ()
{
//①test t=test ();
//②test t;
//③test t (n);
test *t=new test; //④
(t->setvalue);
printf ("i=%d\n", T->getvalue_i ());
printf ("j=%d\n", T->getvalue_j ());
t->~test (); //Call destructor
System ("pause");
return 0;
}

10. For constructors, multiple overloaded constructors can exist in a class (the parameters and their types can be different), and the overloads of the constructors follow the C + + overloaded rules. Note: Unlike the definition of an image and the declaration of an object, an object is defined as a space to request a corresponding object and a constructor is called, whereas an object declaration tells the compiler that an object exists . The call to the constructor is triggered when the object is defined, and in some cases the constructor can be called manually.

11, two special constructors: (1) parameterless constructor (2) copy constructor ( The constructor copies the state of the object when the object is created )

(1) The parameterless constructor is a constructor with no arguments, and the function body is empty Test () {}

(2) The copy constructor is the constructor of the parameter (const class_name &obj), and when the copy constructor is not defined in the class, the compiler provides a copy constructor by default, simply copying the value of the member variable (   Shallow copy ). "The compiler provides a shallow copy. 】

The meaning of the copy function: It is compatible with the initialization mode of C language, and the initialization behavior can conform to the expected logic.

The copy constructor contains a shallow copy ( the physical state of the object after the copy is the same ) and a deep copy ( the logical state of the copied object is the same ); sometimes a shallow copy of the resource allocation can cause problems, such as in a class where I randomly define a pointer, This pointer points to a memory address of 0x1234; When a shallow copy is used (we copy an object to another object), the pointer in the copied object also points to the memory address 0x1234; Then we can release the pointer space only once,

This is a copy of the physical meaning of the shallow copy, and if it is a deep copy, we are going to copy the pointer of one object to another, we will find an area in the memory space, put the value in, and release the pointer twice, which is the deep copy in the logical state.

(3) When do I need a deep copy? In general, a member of the object refers to the resources in the system (including dynamic memory space, pointer definition, file open, network port usage, etc.) need to deep copy, when we customize the copy constructor in the class, it is necessary to implement a deep copy (otherwise, using the compiler provides a shallow copy).

code example:

Classic code example: developing an array class to solve the security problem of the native array.

Ditte Software College C + + Course Learning Notes Summary

Related Article

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.