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...

  • QUESTION 6 What is the output of following C code? struct numbers     {         int x...

    QUESTION 6 What is the output of following C code? struct numbers     {         int x = 2;         int y = 3;     } int main() {   struct numbers nums;         nums.x = 110;         nums.y = 100;         printf("%d\n%d", nums.x, nums.y);         return 0; } A. Compile-time Error B. 110 100 C. 2 3 D. Run-time Error 2 points    QUESTION 7 What is the output of following C code? typedef struct student {         char *stud; }s1; int main() {   s1 s;         s.stud...

  • C Programming Explain what these function(s) do: struct thing{ int x; float y; }; struct node...

    C Programming Explain what these function(s) do: struct thing{ int x; float y; }; struct node { struct thing *t; struct node *next; struct node *prev; }; struct dll{ struct node *head; struct node *tail; int length; }; struct thing *func( struct dll *list, int num) { struct node *curr = list ->head; while(curr != list ->tail) { if(curr ->t->x== num) return curr->t; curr=curr->next; } return NULL; }

  • 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 <<...

  • Which of the following is true about interfaces: An interface can have only non abstract methods....

    Which of the following is true about interfaces: An interface can have only non abstract methods. All methods in an interface must be abstract. A class can only implement one interface. None of the items listed. Can not contain constants but can have variables. What is the rule for a super reference in a constructor? It must be in the parent class' constructor. You cannot use super in a constructor. It must be the last line of the constructor in...

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