Initialising structure variables while creating instances – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2795 siteicon   siteicon   siteicon  

Initialising structure variables while creating instances

A demonstration of initialisation of values of variables while creating the instances of the structures.

Learning Objectives

  • Initialising of structure variables at the time of instance creation.

Source Code

TC++ #2795

 

Source Code

Run Output

Monument                Year Completed
--------------------------------------
Taj Mahal 1653
Eiffel Tower 1889
Statue of Liberty 1886

Code Understanding

struct Monument { char name[50]; int year; };
Structure declaration with two variables char array and int.

Monument m1={“Taj Mahal”,1653};
Monument m2={“Eiffel Tower”,1889};
Monument m3={“Statue of Liberty”,1886};
These are three instance creations with corresponding initialisation. The character array can only be filled while instance creation. It can only be filled later by user input commands or by string copy or memory copy operations.

cout<<“MonumentttYear Completedt”<<endl;
cout<<“————————————–“<<endl;
These are header lines to printed above the output data. Watch out for t (tab) for proper formatting.

cout<<m1.name<<“tt”<<m1.year<<endl;
cout<<m2.name<<“tt”<<m2.year<<endl;
cout<<m3.name<<“t”<<m3.year<<endl;
Three output strings and integer values are printed here with proper tab based formatting. The last line here is longer so only one tab character is used.

 


Suggested Filename(s): struc-obj-init.cpp,sobjnit.cpp



Share

sunmitra| Created: 19-Jan-2018 | Updated: 2-Feb-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