Home > Back-end >  This problem is in the test object as a function parameter is what mean?
This problem is in the test object as a function parameter is what mean?

Time:04-26

. Write a program to define a class Point, containing the abscissa and ordinate two integer data members, design an exchange of horizontal, vertical, the function of the swap, requires the use of the reference object as a parameter of the function, and a display coordinate Point data of the member function print, p1, creating an object to invoke a member function to realize exchange of horizontal ordinate, requires the output before the exchange, after p1 cross, ordinate values, examining object (as a function of parameters)

CodePudding user response:

//write a program to define a class Point, containing the abscissa and ordinate two integer data members, 
//design an exchange, ordinate function swap, requires the use of object reference as the function parameter,
//and a display coordinate point data of the member function print, p1, creating an object called a member function realization of exchange horizontal ordinate,
//request output before the exchange, after p1 cross, ordinate values, examining object (as a function of parameters)
# include & lt; Iostream>
using namespace std;

Class Point {//define a class Point
Public:
Point () {
This - & gt; X=0;
This - & gt; Y=0;
}
Point (int x, int y) {
This - & gt; x=x;
This - & gt; Y=y;
}
The static void swap (Point & amp; P) {//exchange of horizontal, vertical, and use the object reference for the function of parameter
Int TMP.
TMP=p.x;
P.x=p.y;
P.y=TMP;
}
Void the print () {//display the coordinate points data
Cout & lt; <"(" & lt; }
Private:
int x; Abscissa//
int y;//ordinate
};
Int main () {
Point p1=Point (1, 2);//create the object p1
P1. The print ();//output before the exchange p1 cross, ordinate values,
Point: : swap (p1);//call the member function to realize the exchange of horizontal ordinate
P1. The print ();//output after the exchange of p1 horizontal ordinate values,
return 0;
}
//(1, 2)
//(2, 1)
//

CodePudding user response:

reference 1/f, zhao teacher reply:
//write a program to define a class Point, containing the abscissa and ordinate two integer data members, 
//design an exchange, ordinate function swap, requires the use of object reference as the function parameter,
//and a display coordinate point data of the member function print, p1, creating an object called a member function realization of exchange horizontal ordinate,
//request output before the exchange, after p1 cross, ordinate values, examining object (as a function of parameters)
# include & lt; Iostream>
using namespace std;

Class Point {//define a class Point
Public:
Point () {
This - & gt; X=0;
This - & gt; Y=0;
}
Point (int x, int y) {
This - & gt; x=x;
This - & gt; Y=y;
}
The static void swap (Point & amp; P) {//exchange of horizontal, vertical, and use the object reference for the function of parameter
Int TMP.
TMP=p.x;
P.x=p.y;
P.y=TMP;
}
Void the print () {//display the coordinate points data
Cout & lt; <"(" & lt; }
Private:
int x; Abscissa//
int y;//ordinate
};
Int main () {
Point p1=Point (1, 2);//create the object p1
P1. The print ();//output before the exchange p1 cross, ordinate values,
Point: : swap (p1);//call the member function to realize the exchange of horizontal ordinate
P1. The print ();//output after the exchange of p1 horizontal ordinate values,
return 0;
}
//(1, 2)
//(2, 1)
//

Why the swap function to add a static before

CodePudding user response:

reference 1/f, zhao teacher reply:
//write a program to define a class Point, containing the abscissa and ordinate two integer data members, 
//design an exchange, ordinate function swap, requires the use of object reference as the function parameter,
//and a display coordinate point data of the member function print, p1, creating an object called a member function realization of exchange horizontal ordinate,
//request output before the exchange, after p1 cross, ordinate values, examining object (as a function of parameters)
# include & lt; Iostream>
using namespace std;

Class Point {//define a class Point
Public:
Point () {
This - & gt; X=0;
This - & gt; Y=0;
}
Point (int x, int y) {
This - & gt; x=x;
This - & gt; Y=y;
}
The static void swap (Point & amp; P) {//exchange of horizontal, vertical, and use the object reference for the function of parameter
Int TMP.
TMP=p.x;
P.x=p.y;
P.y=TMP;
}
Void the print () {//display the coordinate points data
Cout & lt; <"(" & lt; }
Private:
int x; Abscissa//
int y;//ordinate
};
Int main () {
Point p1=Point (1, 2);//create the object p1
P1. The print ();//output before the exchange p1 cross, ordinate values,
Point: : swap (p1);//call the member function to realize the exchange of horizontal ordinate
P1. The print ();//output after the exchange of p1 horizontal ordinate values,
return 0;
}
//(1, 2)
//(2, 1)
//

And why do you want to define two point function, the first constructor can omit?

CodePudding user response:

//write a program to define a class Point, containing the abscissa and ordinate two integer data members, 
//design an exchange, ordinate function swap, requires the use of object reference as the function parameter,
//and a display coordinate point data of the member function print, p1, creating an object called a member function realization of exchange horizontal ordinate,
//request output before the exchange, after p1 cross, ordinate values, examining object (as a function of parameters)
# include & lt; Iostream>
using namespace std;

Class Point {//define a class Point
Public:
//Point () {
//this - & gt; X=0;
//this - & gt; Y=0;
//}
Point (int x, int y) {
This - & gt; x=x;
This - & gt; Y=y;
}
/* */static void swap (Point & amp; P) {//exchange of horizontal, vertical, and use the object reference for the function of parameter
Int TMP.
TMP=p.x;
P.x=p.y;
P.y=TMP;
}
Void the print () {//display the coordinate points data
Cout & lt; <"(" & lt; }
Private:
int x; Abscissa//
int y;//ordinate
};
Int main () {
Point p2.//u compiler error error C2512: "Point" : no suitable default constructor available
Point p1=Point (1, 2);//create the object p1
P1. The print ();//output before the exchange p1 cross, ordinate values,
Point: : swap (p1);//call the member function to realize the exchange painted horizontal ordinate compiler error error C2352: "Point: : swap" : illegal non-static member function call
P1. The print ();//output after the exchange of p1 horizontal ordinate values,
return 0;
}
  • Related