Purpose of the experiment:
Further deepen the understanding of classes and objects
Master several methods of object transfer
Master the concept and use of static members
Experiment content part a
Understand how three different objects are passed
#include<iostream> using namespace std; class Tr { public: Tr(int n) { i = n; } void set_i(int n) { i = n; } int get_i() { return i; } private: int i; }; //First: void sqr_it(Tr ob) { ob.set_i(ob.get_i() * ob.get_i()); cout << "In function sqr_it Inside, parameter object ob Data members for i The value of is:" << ob.get_i(); cout << endl; } int main() { Tr obj(10); cout << "Call function sqr_it front,Argument object obj Data members for i The value of is:"; cout << obj.get_i() << endl; sqr_it(obj); cout << "Call function sqr_it after,Argument object obj Data members for i The value of is:"; cout << obj.get_i() << endl; return 0; } //Second: //void sqr_it(Tr* ob) //{ // ob->set_i(ob->get_i() * ob->get_i()); // Cout < < in the function sqr_it, the value of the data member I of the formal parameter object OB is: "< ob - > get_ i(); // cout << endl; //} //int main() //{ // Tr obj(10); // Cout < < "before calling the function sqr_it, the value of the data member i of the argument object obj is:"; // cout << obj.get_i() << endl; // sqr_it(&obj); // Cout < < "after calling the function sqr_it, the value of the data member i of the argument object obj is:"; // cout << obj.get_i() << endl; // return 0; //} //Third: //void sqr_it(Tr& ob) //{ // ob.set_i(ob.get_i() * ob.get_i()); // Cout < < in the function sqr_it, the value of the data member I of the formal parameter object OB is: < < ob.get_ i(); // cout << endl; //} //int main() //{ // Tr obj(10); // Cout < < "before calling the function sqr_it, the value of the data member i of the argument object obj is:"; // cout << obj.get_i() << endl; // sqr_it(obj); // Cout < < "after calling the function sqr_it, the value of the data member i of the argument object obj is:"; // cout << obj.get_i() << endl; // return 0; //}
The first transfer method: value transfer
When the main program calls the actual parameters of the function, the system will transfer and copy the values of the actual parameters to the corresponding formal parameters in the function,
After the execution of the formal parameter in the function, the contents of the variable itself in the main program will not be modified.
The function of value passing is to let the function know the value of external parameters. Value passing is one-way and can only be passed from arguments to formal parameters.
Experiment content part b
Master the concept and use of static members
Members decorated with the keyword static are called static class member s
Static members are shared by all objects, and only one is stored in public memory
Static members include static data members and static function members
//Experiment content part b #include <iostream> using namespace std; class Ctest { static int count; public: Ctest() { ++count; cout << "Number of objects=" << count << '\n'; } }; int Ctest::count = 0; int main(void) { Ctest a[3]; return 0;
explain:
1. The definition of static data members is similar to that of ordinary data members, but the static keyword should be added in front of them.
2. The initialization of static data members is different from that of ordinary data members. Static data member initialization should be performed separately outside the class and before defining the object. It is generally defined and initialized in a special area before the main() function and after the class declaration.
3. Static data members belong to a class (to be exact, they belong to a collection of objects in the class), rather than an object like ordinary data members. Therefore, you can use "class name::" to access static data members. The format is as follows: Class Name:: static data member name.
4. Static data members, like static variables, are created and initialized at compile time. It exists before any object of this class is created. Therefore, shared static data members can be accessed before the object is defined. After the object is defined, common static data members can also be accessed through the object.
The following describes the use of static member functions:
1. In general, static function members are mainly used to access static member functions. When it is used with static data members, it achieves the purpose of sharing data between objects in the same class.
2. Private static member functions cannot be accessed by functions and objects outside the class.
3, one reason for using static member functions is that it can call static member functions before processing any objects to handle static data members, which is a function that ordinary member functions can not implement.
4. The compilation system limits the static member function to internal connection, that is, the function with the same name in other files connected with the current file will not conflict with the function, maintaining the security of the function, which is another reason for using the static member function.
5. Static member functions are part of a class, not an object. If you want to call a public static member function outside the class, it is better to use the following format: Class Name:: static member function name ()
Experiment content part c
#pragma once #include <iostream> using namespace std; class Student { public: void InitStudent(char name[]); void ExpendMoney(float a); void ShowMoney(); private: static float m_ClassMoney; char Name[20]; }; float Student::m_ClassMoney = 1000; void Student::InitStudent(char name[]) { int i = 0; while (name[i]) { Name[i] = name[i]; i++; } } void Student::ExpendMoney(float a) { m_ClassMoney += a; } void Student::ShowMoney() { cout << "The flight fee is left" << m_ClassMoney << endl; } int main() { Student A, B, C; A.ExpendMoney(-50); A.ShowMoney(); B.ExpendMoney(-98.5); B.ShowMoney(); C.ExpendMoney(-500.53); C.ShowMoney(); }