Structure of the 3-c++ program 1.2

Source: Internet
Author: User
Tags access properties gety

Life cycle of an object

Can be divided into static life cycle and dynamic life cycle

1. Static life cycle

If the lifetime of an object is the same as the run time of the program, we call it a static Lifetime. Objects declared in the scope of a file have a static Lifetime. If you want to declare an object with a static lifetime in the block of a function, use the keyword Static.

2, Dynamic Survival Time

In addition to both of these cases, the rest of the objects have dynamic Lifetimes. The dynamic Lifetime object is born at the declaration point, ending at the end of the identifier Scope.

Static members of a class

In a structured program design, the basic unit of a program module is a function, then the sharing of in-memory data between modules is achieved through data sharing between functions and functions, including parameter passing and global Variables.

A static member is a problem that resolves data and function sharing between different objects of the same class. For example, to abstract all employees of a company as follows,

Class Employee

{

Private

int EmpNo;

int ID;

Char *name;

//...

}

At this time, if you need to count the total number of employees, how to deal with it, if directly add a variable into the class, it is bound to cause each object will contain this variable, and easy to cause inconsistency, The ideal scenario is that all objects of the class together have a data member for the total number of storage, This is the need to use static data Members.

1. Static data members

We say that "all objects of a class have the same attribute", that is, the number of attributes, the name, the same data type, the property values of each object can be different, such that the property in the Object-oriented method is called "instance properties". Represented in a C + + program as a non-static data member of a class.

There is also the concept of "class attributes" in object-oriented methods. If a property is common to the entire class and does not belong to any specific object, static members are declared using the static Keyword. Static members have only one copy in each class, which is maintained and used by all objects of the class, thus enabling data sharing between different objects of the same Category. A class property is a data item that describes the common characteristics of all objects of a class, and its properties are the same for any object instance.

Static data members have a static life Cycle. It can only be accessed through the class name, and the general usage is "class name:: identifier". in the definition of a class, only static data members are declared, and static data members must be defined at some point in the scope of the file, which can also be initialized .

2. Static function members

The so-called static member function is a function member declared with the static keyword, which is shared by all objects of the same class, as in the case of statically data members, the static member function page belongs to the entire class.

As a member function, its access properties can be tightly controlled by the class, and for a public static member function, it can be called by a class name or object name, whereas a generic non-static member function can only be called by object Name.

Static member functions can directly access static data and function members of a class. Access to non-static data members must be given the object name through parameter passing, and then accessed through the object Name. Such as

Class A

{

Public

static void F (a a);

Private

int x;

};

void A::f (a A)

{

cout<<x;//a reference to X is incorrect

Cout<<a.x;//correct

}

As you can see, accessing Non-static members through static function members is rather cumbersome, and in general it is primarily used to access static data members in the same class, maintaining data shared between objects .

#include <iostream>
Using namespace std;
Class Point//point Classes Definition
{
Public
Point (int xx=0,int yy=0) {x=xx; y=yy;countp++;} constructor function
Point &p;//copy Constructor
~point () {countp--;}
int GetX () {return X;}
int GetY () {return Y;}
static void GetC () {cout<< "Object id=" <<countp<<endl;

Private
int x, y;
static int countp;

};
Point::P oint (point &p)
{
x=p.x;
y=p.y;
countp++;
}
int point::countp=0; static data member definition and initialization, initialization using class name qualification
int main ()
{
Point A (4,5);
cout<< "point A," <<a.getx () << "," <<a.gety ();
A.getc ();
Point B (A);
cout<< "point B," <<b.getx () << "," <<b.gety ();
Point::getc ();
Return 0;
}

when using static data members, It should also be noted that if you want to access static data members through Non-static functions, you should use Non-inline functions, and access static data member functions whose definition of function body should be written in the same file as the static member amount initialization .

Structure of the 3-c++ program 1.2

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.