定义一个点类,如下: 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 According to the problems encountered in the debugging process, this experiment consolidated the knowledge points related to the default parameters of the function: By —-Suki 2020 4 14
[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
在实验过程中,写构造函数的定义时我误将构造函数参数列表也写成了构造函数声明(即指定了默认参数)导致编译出错。
Later, errors were found and corrected during debugging.
后来调试过程中发现并更正了错误。Empirical conclusion
本次实验根据调试过程中遇到的问题对函数的默认参数相关知识点进行了巩固:
(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.
更加熟悉了在类中使用静态成员。例如,静态数据成员无法在类内赋值。
以后更博客我都基本上会以中英双译的形式,对代码的重复理解与总结,加上对更多专业计算机英语的学习。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算