Structure instance copying – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2916 siteicon   siteicon   siteicon  

Structure instance copying

Copying one instance of structure to another for the instances derived from the same structure definition

Learning Objectives

  • Copying one object/instance based on any structure to another instance derived from the same structure.

Source Code

TC++ #2916

 

Source Code

Run Output

Details of Mobile 1
Screen = 1280x720, Price = 7000
Details of Mobile 2
Screen = 1280x720, Price = 8000

Code Understanding

struct Mobile { int screen_h; int screen_w; float price;};
A structure with three variables have been declared here.

Mobile m1,m2; //Two instances of structure named m1, m2 have been created.
m1.screen_h=1280; m1.screen_w=720; m1.price=7000;
Instance m1 has been filled with some data.

m2=m1;
Instance m1 has been copied to instance m2 by using the assignment operator

m2.price=8000;
Only one of the variable in instance m2 has been changed. Other two have been kept same to see the effect of copying.

cout<<“Details of Mobile 1″<<endl; cout<<“Screen = “<<m1.screen_h<<“x”<<m1.screen_w<<“, Price = “<<m1.price<<endl;
Data from instance m1 has been output.

cout<<“Details of Mobile 2″<<endl; cout<<“Screen = “<<m2.screen_h<<“x” <<m2.screen_w<<“, Price = “<<m2.price<<endl;
Data from instance m2 has been output. Since the copying occurred successfully thus two data items screen_h and screen_w are same as in instance m1. The last item price was modified only in the instance m2 so its change is appearing properly.

 

Notes

  • The copying can occur only when the derived instances are from the same definition of structure. It can not happen between instance derived from two structures even if there variable declarations are same.


Suggested Filename(s): strucopy.cpp



Share

sunmitra| Created: 22-Jan-2018 | Updated: 22-Jan-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 2D Array Memory Addressing 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 Multi-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