Question

can someone explain what is going on inside the strcut? c++ struct Pos{ int x; int...

can someone explain what is going on inside the strcut? c++

struct Pos{
int x;
int y; // coordinates
Pos(const Pos &p, int dx=0, int dy=0){ *this = p; x+=dx; y+=dy;}
  
Pos(int _x, int _y){ x=_x; y=_y; } //new location
  
bool operator<(const Pos & p) const { return (x < p.x) || (x==p.x && y < p.y); }
  
  
bool operator==(const Pos & p) const { return x==p.x && y==p.y; }
Pos(){x=-1;y=-1;}
};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//do comment if any problem arises
//You has to have some knowledge of object oriented programming
// in order to fully understand this Code
#include <iostream>
using namespace std;
struct Pos
{
   // coordinates
   int x;
   int y;
   //this is constructor of structure Pos which takes another Pos and assigns it to current Pos
   //and then it takes one x and y value and increement x and y of current pos by respective values
   //of x and y
   Pos(const Pos &p, int dx = 0, int dy = 0) { *this = p; x += dx; y += dy;}
   //this structure takes 2 values of x and y and initialises
   //x and y of current Pos object with those values
   Pos(int _x, int _y) { x = _x; y = _y; }
   //this is overloaded operator < which returns true if value of current x is <
   //value of x in given Pos or if x are equal then checks y
   bool operator<(const Pos & p) const { return (x < p.x) || (x == p.x && y < p.y); }
   //this is overloaded == operator which returns true if both x and y values of
   //current and given Pos in arguments are equal
   bool operator==(const Pos & p) const { return x == p.x && y == p.y; }
   //this is default constructor which initialises x and y with -1
   Pos() {x = -1; y = -1;}
};
//demonstrating above struct
int main()
{
   //initialises first with x=2 and y=3
   Pos first(2, 3);
   //this line initialises second = first, then
   //increement x of second with 1 and y with also 1
   Pos second(first, 1, 1);
   //printing first Pos
   cout << "Position of first Pos: \nx: " << first.x << "\ny: " << first.y << endl;
   //printinf second Pos
   cout << "Position of second Pos: \nx: " << second.x << "\ny: " << second.y << endl;
   string isEaual = first == second ? "Yes\n" : "No\n";
   //printing if first is equal to second
   cout << "Is first equal to second? " << isEaual;
   string isless = first < second ? "Yes\n" : "No\n";
   //printing if first is equal to second
   cout << "Is first less than second? " << isless;
}

Output:

Position of first Pos: X: 2 y: 3 Position of second Pos: X: 3 y: 4 Is first equal to second? No Is first less than second? Ye

Add a comment
Know the answer?
Add Answer to:
can someone explain what is going on inside the strcut? c++ struct Pos{ int x; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider: typedef struct { int x; int y; } Point; Point p; Point* pp; Assign the...

    Consider: typedef struct { int x; int y; } Point; Point p; Point* pp; Assign the coordinates (10, 20) to point p. choose the following Multiple choice question 1.) p[x] = 10; p[y] = 20; 2.) p->x = 10; p->y = 20; 3.) p(x) = 10; p(y) = 20; 4.) p.x = 10; p.y = 20;

  • Write a generic function int find_lower_bound(T seq[], int n, const T& value). The function returns the...

    Write a generic function int find_lower_bound(T seq[], int n, const T& value). The function returns the index of the largest element in the given sequence that is less than the given value. If multiple elements satisfy, return the one with smallest index. Return -1 if no such element exists. //main.cpp #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; #include "source.h" struct Point{    int x,y;    bool operator<(const Point& p) {        return (x<p.x || (x==p.x&&y<p.y));    } }; int...

  • Need Help! in English Can not get program to run  plz include detailed steps as comments of...

    Need Help! in English Can not get program to run  plz include detailed steps as comments of what i did not do what I have done so far class Point: def __init__(self): self._x = 0 self._y = 0 def getX(self): return self._x def setX(self, val): self._x = val def getY(self): return self._y def setY(self, val): self._y = val def __init__(self, initX = 0, initY = 0): self._x = initX self._y = initY def __str__(self): return '<'+str(self._x)+', '+str(self._y)+ '>' def scale(self, factor):...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

  • Write a complete C program. Define a structure type element_t to represent one element from the...

    Write a complete C program. Define a structure type element_t to represent one element from the periodictable of elements. Components should include the atomic number (aninteger); the name, chemical symbol, and class (strings); a numeric field forthe atomic weight; and a seven-element array of integers for the number ofelectrons in each shell. The following are the components of an element_t structure for sodium.11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 Have the user enter the data...

  • Question 2: Virus Wars A popular subgenre of strategy game is the so-called Virus War format,...

    Question 2: Virus Wars A popular subgenre of strategy game is the so-called Virus War format, where the player is shown a field of cells, each with a virus count, and may attack other cells within a certain range. We are going to write some classes in Python to implement a simple Virus Wars game. 2a) The position class (9 points) Everything within this game is going to require a position, so it makes sense to define it up front....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT