Output Writing – Pixel structure passed by reference – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #CPP#2987 siteicon   siteicon   siteicon  

Problem Statement - Output Writing – Pixel structure passed by reference

Write down the output of the program and give steps in arriving at such output.

struct Pix {int x,y;};
void show(Pix &p)
{
  cout<<p.x++<<","<<++p.y<<endl;
}
int main()
{
  Pix P={50,60},Q,R;
  R=P;
  show(R);
  P.x+=10;P.y+=30;
  Q=R;
  show(P); show(Q); show(R);
  return 0;
}

 

Solution

TC++ #2987

 

Run Output

50,61
60,91
51,62
51,62

Steps in arriving at output:

Pix P={50,60},Q,R; 
Here P will contain P.x=50 and P.y=60. Q.x,Q.y and R.x and R.y will have indeterminate values.

R=P;
Here R.x and R.y will have values same as P.x and P.y so they will be 50 and 60 respectively.

show(R);
Since value of y has a pre-increment so the value printed would be 50,61

P.x+=10;P.y+=30;
Now P.x will become 50+10 = 60 and P.y will become 60+30=90.

Q=R;
Here values of R.x and R.y will be copied to Q.x and Q.y so they will become 51 (due to previous post increment) and 61 as fixed earlier.

show(P);
This should print 60 and 91 (due to pre-increment in show)

show(Q);
This should print 51 and 62 due to pre increment in show

show(R);
This should also print 51 and 62 again to due to pre-increment in show.


Share

sunmitra| Created: 24-Jan-2018 | Updated: 18-Dec-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back