C ++ basics, important technologies, and basic technologies

Source: Internet
Author: User
C ++ foundation and important technology, basic technology
1. Classes and objects 1.1 Related implementations of classes

The first method is to define the member functions of the class in the class body, which will not be introduced here.
The second method is to place the implementation of the member function inside the class body outside the class body. At this time, the domain operator "::" is required, which is the same as the first effect.

class apple
{
    public:
// data member
    int index;
// member function
    int getindex ();
};
// Class member function implementation
int apple :: getindex ()
{
    return index;
}
C ++ language generally puts the function declaration in the header file and the function implementation in the cpp file. You can also put the class definition in the header file, implement the member functions of the class in the implementation file, and store the class header file. And the implementation file is best the same as the class name. E.g:

//apple.h file
#include
#include ....
class apple
{
    public:
    int index;
    int getindex ();
}
...
//apple.cpp file
#include
...
int apple :: getindex ()
{
    return index;
}
...
1.2 Constructor
#include
class apple
{
    public:
        apple ();
        apple (int iindex, short isize);
        int index;
        short size;
    ...
}
// Default constructor initialization
apple :: apple ()
{
    index = 1;
    size = 2;
}
// initialized in the constructor with parameters
apple :: apple (int iindex, short isize)
{
    index = iindex;
    size = iisize;
}
int apple :: getindex ()
{
    return index;
}
...
void main ()
{
    apple a1;
    cout <1.3 members
If no keywords are added during class definition, the default state members are in the private area.

1.3.1 Static Class Members
If you define a class member as a static class member, you can use the class name to access it directly, presumably because only one of them has the same value. You do n’t need to worry about confusion when using direct class access. Static class members are defined in class members Before using the static keyword identification. When you define a static data member, you usually need to initialize the static data member outside the class. E.g:

class apple
{
    public:
    static unsigned int price; // Define a static data member
}
unsigned int apple :: price = 10; // initialize static data members
...

int main (...)
{
    apple a1;
    cout <
When defining the data members of a class, you can specify default parameters for member functions. The parameter default values can be static data members of the class, but ordinary data members cannot be used as function default parameters.

In addition, static member functions can only access static data members

When defining a static data member function, if the function's implementation code is outside the class body, the static keyword cannot be marked in the function's implementation part.

class apple
{
    public:
    static unsigned int price;
    int abc;
    void output (int data = price) // take static data as default parameter
    {
    cout <
We will add more information about nested classes and friends when needed.

2. Inheritance and Derivation 2.1 Class Inheritance
There are three types of inheritance, public, protected, private, ":" indicates the inheritance relationship between the base class and the derived class, for example:

class phone
{
    public:
    int size;
    char name [10];
};
class iphone: public phone
{
    public:
    char version [10];
    int sizes;
}

Note: Private members cannot be inherited. If a subclass defines a member function with the same name as the parent class during derivation, the subclass will hide the member function of the parent class.


2.2 Accessibility after inheritance
What kind of base class should be like after the public type is derived, private is still not accessible

After the protected derivation, the original public and protected members are protected in the derived class, but they can be accessed only when the derived class is defined. Objects defined with the derived class cannot be accessed and cannot be accessed outside the class. That is, protected members Can be used by all derived classes of the base class, but cannot be changed.

Private in the private derived base class is still not accessible.


2.3 constructor access order
When a subclass is derived from the parent class and a subclass object is declared, it will first call the constructor of the parent class, and then call the constructor of the current class to create the object. When the object is released, the analysis of the current class is called first. Constructor, and finally the destructor of the parent class.

No matter what kind of constructor is called, the default constructor of the parent class will be called first. If you want to use the constructor of the parent class with parameters, you need to explicitly call it. E.g:


#include ....
...
class father
{
    public:
    int ID;
    char name [10];
    father (char name [10])
    {
    strcpy (char name []);
    }
    father ()
    {
    strcpy (name, "HH");
    }
    ...
};
class son: public father
{
    public:
    char smallname [10];
    son (char name []): father (name)
    {
    cout << ..... <
To be continued ...
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.