Experiment 4 Static Members and Friends Experiment 4 Static Members and Friends

Experiment 4 Static members and friends

1. The purpose and requirements of the experiment

    Understand the characteristics of member functions and master concepts such as static members and friends.

2. Experimental content

1. Debug the following program, write out the output, and analyze the output.


Running result graph:


Analysis of output results: When creating object P, call the constructor to initialize it to get A=6, B=BA=100-6=94, and then create object Q, call the constructor to initialize it, A=8, B= BA=94-8=86, and finally the static member function static void fun() is called, and because B is a static data member, this result is output.

2. Analyze and debug the program and complete the following questions.


(1) Point out all the constructors and what role do they play in this program?

        The constructor My(double i=0) is used to initialize m1, m2, My(double i, double j) is used to initialize m3, and the copy constructor My(My&m) is used to initialize m4.

(2) Indicate the constructor that sets the default parameters.

        My(double i=0) is a constructor with default parameters.

(3) Point out the friend function. Put the friend function in the private section and see if the result changes.

       dist() is a friend function, put it in the private part, the compilation is still correct, because the declaration of the friend function is still consistent with the ordinary function, so there will be no error during compilation.

(4) Write the output and analyze the output.


3. Define a Student class, which includes a data member score (score), two static data members total (total score) and the number of students count; the member function scoretotalcount (float s) is used to set the score, find the total Points and cumulative number of students; the static member function sum() is used to return the total score; the static member function average() is used to calculate the average. In the main() function, input the grades of a certain class, and call the above function to find the total and average scores of the students in the class.



Running result graph:


4. Declare two classes, Book and Ruler, both of which have a weight attribute, define a friend function totalWeight() of the two, and calculate the weight sum of the two.


Running result graph:


3. Analysis and discussion

1. How to define static data members and member functions?

      Answer: A static data member does not belong to any object. It is not created by the creation of the object, nor deleted by the destructor of the object. It is a part of the class definition, so the use of static data members will not destroy the concealment of the class. Static data members in a class are different from general static variables and different from other class data members. It is created when the program starts running, not when the object is created. The recovery of the space it occupies is not carried out at the time of the destructor but at the end of the program. Member functions are used to describe the behavior of objects. Like ordinary functions, they can be overloaded, can use default parameters, and can also be declared as inline functions.

2. How to initialize static data members?

       Answer: The initialization of static data members is different from general data members, and its initialization cannot be performed in the constructor. The format of static data member initialization is: <data type><class name>::<static data member>=<initial value>; The scope operator "::" here is used to indicate the class to which the static data member belongs.

3. What is the difference between static member functions accessing static members and non-static members?

       Answer: Static member functions in C++ cannot access non-static members, but vice versa.
Because static members belong to a class, they can be accessed when the class object is not initialized, while non-static members must be created and initialized after the class object is initialized, so static functions cannot access non-static members in C++ .

4. How to call static member function?

      Answer: The format of calling a static member function is: <class name>::<static member function name>(<parameter list>) or <object name>::<static member function name>(<parameter list>) Static member function The main function is to access static members of the same class and maintain the number of objects shared between objects.

5. How to understand "static members do not belong to an object, but belong to all objects of the class." This sentence?

        Answer: Static members refer to class members declared as static, including static data members and static member functions. All objects within the scope of the class share the data.

6. Compare the similarities and differences between friend functions and general functions in terms of definition and calling.

      Answer: A function defined outside a class is a general function; a general function declared in a class, plus the keyword friend, becomes a friend function of the class, which can access all members of the class. Friend functions are called in exactly the same way as normal functions are implemented. An ordinary function can be defined as a friend function of a class, and a member function of one class can also be defined as a friend function of another class.

Fourth, the experimental summary

        In this experiment, I know the concept of static members, friends, and the characteristics of member functions. In the process of debugging the program, I learned a lot of new knowledge and gained a lot in the process of constantly finding and solving problems.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853888&siteId=291194637