对类的静态成员概念的理解掌握的一次实验作业

[Experiment topic 1]

定义一个点类,如下:
class Point
{
int x,y;
static int countp;//统计点对象个数
public:
Point(int x=1,int y=1);
void Setxy(int xx,int yy);
void Getxy(int &xx,int &yy);//引用参数有什么作用?
static int Get_countp(); //只是返回点对象个数,其中不能写输出语句
};
编程完成各成员函数的定义及countp的初始化,编写main()函数,要求:
1)main()中第一条语句就是通过调用Get_countp()获取点对象个数,然后在主函数中输出显示当前点对象个数;
2)然后定义一个点对象数组,再修改其中第2个点对象元素的坐标,输出显示这个点对象修改后的坐标值;
3)最后再通过调用Get_countp(),输出显示当前点对象个数。
3)除了上述成员函数及主函数,不允许再增加任何函数!

Experimental procedure

#include<iostream>
using namespace std;
class Point
{
    int x, y;
    static int countp;//统计点对象个数
public:
    Point(int x = 1, int y = 1);
    void Setxy(int xx, int yy);
    void Getxy(int& xx, int& yy);//引用参数有什么作用?
    static int Get_countp(); //只是返回点对象个数,其中不能写输出语句
};
int Point::countp = 0;//初始化静态数据成员为0
Point::Point(int x , int y ) {
    this->x = x; this->y = y;
    countp++;//点个数+1
}
void Point::Setxy(int xx, int yy) {
    x = xx;
    y = yy;
}
void Point::Getxy(int& xx, int& yy) {
    xx = x; yy = y;//通过将引用变量用作参数,函数将使用原始数据,而不是其副本。
}
int Point::Get_countp() { return countp; }
int main() {
    int X, Y;
    cout << "The number of current point objects is:"
        << Point::Get_countp() << endl;
    Point p[3];
    p[1].Setxy(2, 2);
    p[1].Getxy(X, Y);
    cout << "The coordinates of the second point object element are " << X << " " << Y << endl;
    cout << "The number of current point objects is " << Point::Get_countp() << endl;
}

Analysis of experimental results

在这里插入图片描述
(1)The initialization of static data members cannot be ignored, otherwise compilation errors will be caused;
(2) When a function needs more than one return value, it is much more convenient to pass by reference;
(3) When defining member functions and constructors outside the class, remember to add the class scope qualifier::;
(1) 不能忽视了静态数据成员的初始化不然会导致编译错误;
(2) 当函数需要多个返回值时,使用按引用传递会方便很多;
(3) 在类外定义成员函数和构造函数时切记加上类作用域限定符::;

Experimental process analysis

During the experiment, when I wrote the definition of the constructor, I mistakenly wrote the constructor parameter list into the constructor declaration (i.e. the default parameter was specified), which resulted in compilation errors
在实验过程中,写构造函数的定义时我误将构造函数参数列表也写成了构造函数声明(即指定了默认参数)导致编译出错。
在这里插入图片描述
Later, errors were found and corrected during debugging.
后来调试过程中发现并更正了错误。

Empirical conclusion

According to the problems encountered in the debugging process, this experiment consolidated the knowledge points related to the default parameters of the function:
本次实验根据调试过程中遇到的问题对函数的默认参数相关知识点进行了巩固:
(1) If the default parameter is set during function definition, it cannot be set again in function declaration, and vice versa;
(2) You can set multiple default parameters. To set a default value for a parameter, all the parameters on the right must be default;
(3) The default value can be a constant, a global variable, or even a function call (the call argument must be an expression of a constant or a global variable), not a local variable;
(1) 如果在函数定义时设置了默认参数,那么就不能在函数声明中再次设置,反之亦然;
(2) 可以设置多个默认参数,要为某个参数设置默认值,则它右边的所有参数必须都是默认参数;
(3) 默认值可以是常量、全局变量、甚至是一个函数调用(调用实参必须是常量或全局变量的表达式),不可以是局部变量;
I’m more familiar with using static members in classes. For example, a static data member cannot be assigned within a class.
更加熟悉了在类中使用静态成员。例如,静态数据成员无法在类内赋值。

By ----Suki 2020 4 14
以后更博客我都基本上会以中英双译的形式,对代码的重复理解与总结,加上对更多专业计算机英语的学习。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值