Nested Structure Initialisation – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Solved Problem! #CPP#2928 siteicon   siteicon   siteicon  

Problem Statement - Nested Structure Initialisation

Write the output of the following program.

TC++ #2928

 

  Related (?) :

Solution


Run Output

Aluminium, 20, 302.25
Wood, 0, 400

struct Furniture { char material[40]; int weight;};
This structure will be used as a nested item in structure following next

struct Table { Furniture furn; float price;};
Here Table structure has Furniture structure nested into it. Additional variable price is also defined.

Table round_table={{“Wood”,20}},
The item round_table which is an instance of Table has been partially initialised. round_table.furn.material will become “Wood”round_table.furn.weight will become 20. The value round_table.price will become 0 as it has not been initialised.

study_table={{“Wood”},400};
More partial initialisation has been done here with study_table.furn.material becoming “Wood”study_table.furn.weight will become 0 as it has not been initialised. and study_table.price will become 400.

strcpy(round_table.furn.material,”Aluminium”);
Value in round_table.furn.material has been updated to “Aluminium” so the initial value will be changed.

round_table.price=302.25;
This value will also be updated to given value 302.25 as against 0 which was initially set.

cout<<round_table.furn.material<<“, “<<round_table.furn.weight<<“, “
<<round_table.price<<endl;
The values of round_table are printed here.

cout<<study_table.furn.material<<“, ” <<study_table.furn.weight<<“, “
<<study_table.price<<endl;
The values of study_table are printed here. Notice that study_table.furn.weight will print 0 as its value was never updated later.

Notes

  • Partial initialisation leads to initialisation with 0 or null as the case may be while if nothing is initialised junk values will result. This effect can be studied further from #2799


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