| |

VerySource

 Forgot password?
 Register
Search
View: 934|Reply: 7

Wrong code

[Copy link]

3

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 14:20:01
| Show all posts |Read mode
#include <iostream.h>
class Location {
private:
int X, Y;
public:
void init (int initX, int initY);
int sumXY ();
};
void Location: init (int initX, initY)
{
X = initX;
         Y = initY;
}
int Location :: sumXY ()
{return X + Y;}
void main ()
{Location A1;
int x, y;
A1init (5,3);
x = A1.X, y = A1.Y;
cout << x + y << "" << A1.sumXY () << endl;
}


Is this void Location: init (int initX, initY) wrong? How to change

If not, how to change
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-7 18:45:01
| Show all posts
void Location :: init (int initX, int initY)
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-7 22:00:01
| Show all posts
#include <iostream.h>
class Location {
private:
int X, Y;
public:
void init (int initX, int initY);
int sumXY ();
};
void Location: init (int initX, int initY)
{
X = initX;
         Y = initY;
}
int Location :: sumXY ()
{return X + Y;}
void main ()
{Location A1;
int x, y;
A1.init (5,3);
x = A1.X, y = A1.Y; // X, Y is a private member and cannot be obtained through A1. You can define two GetX, GetY
                // public member function
cout << x + y << "" << A1.sumXY () << endl;
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-13 19:36:02
| Show all posts
The sentence you said is wrong, and the other is wrong, it should be like the following !!!!!!!

#include <iostream.h>
class Location {
private:
int X, Y;
public:
void init (int initX, int initY);
int sumXY ();
};
void Location :: init (int initX, int initY)
{
X = initX;
         Y = initY;
}
int Location :: sumXY ()
{return X + Y;}
void main ()
{Location A1;
int x, y;
A1init (5,3);

x = A1.X, y = A1.Y; // At this time, the object cannot access the private properties of the class and cannot be assigned this way. This statement should be deleted.

cout << x + y << "" << A1.sumXY () << endl;
}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-14 14:27:01
| Show all posts
Newcomer, come on. Very basic question.
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-16 08:36:02
| Show all posts
I changed:
#include <iostream.h>
class Location
{
public:
Int X, Y;
Void init (int initX, int initY);
Int sumXY ();
};
void Location :: init (int initX, int initY)
{
X = initX;
    Y = initY;
}
int Location :: sumXY ()
{
return X + Y;
}

void main ()
{
Location A1;
int x, y;
A1init (5,3);
x = A1.X, y = A1.Y;
cout << x + y << "" << A1.sumXY () << endl;
}
I don't know what your A1init is used for
So no change.
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-20 15:54:01
| Show all posts
FT >>>> is basically a simple question again. . . . .
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-22 21:36:01
| Show all posts
I think it is x = A1.X, y = A1.Y; the problem with this sentence is that private variables cannot be used directly
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list