C++使用带参构造函数初始化对象问题,求问! 10
#include<iostream>usingnamespacestd;classPoint{public:Point(){x=0;y=0;cout<<"Defaultc...
# include <iostream>
using namespace std;
class Point
{
public:
Point()
{
x = 0;
y = 0;
cout << "Default constructor called" << endl;
}
Point(int newX, int newY)
{
x = newX;
y = newY;
cout << "Constructor called" << endl;
}
~Point()
{
cout << "Destructor called" << endl;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
void Move(int newX, int newY);
private:
int x,y;
};
void Point::Move(int newX,int newY)
{
cout << "Moving the point to (" << newX << "." << newY << ")" << endl;
x = newX;
y = newY;
}
void main()
{
Point pointArray[4] = { Point(1, 1),Point(2, 2)}; //意义?????
for(int i = 0; i < 4; i++)
{
pointArray[i].Move(i + 10, i + 20);
}
system("pause");
}
倒数第七行中用Point给数组赋值的意义是?如果将其删去,程序不会出错且结果不变= =,拜谢! 展开
using namespace std;
class Point
{
public:
Point()
{
x = 0;
y = 0;
cout << "Default constructor called" << endl;
}
Point(int newX, int newY)
{
x = newX;
y = newY;
cout << "Constructor called" << endl;
}
~Point()
{
cout << "Destructor called" << endl;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
void Move(int newX, int newY);
private:
int x,y;
};
void Point::Move(int newX,int newY)
{
cout << "Moving the point to (" << newX << "." << newY << ")" << endl;
x = newX;
y = newY;
}
void main()
{
Point pointArray[4] = { Point(1, 1),Point(2, 2)}; //意义?????
for(int i = 0; i < 4; i++)
{
pointArray[i].Move(i + 10, i + 20);
}
system("pause");
}
倒数第七行中用Point给数组赋值的意义是?如果将其删去,程序不会出错且结果不变= =,拜谢! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询