Creating global instances or objects of structures – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#2777 siteicon   siteicon   siteicon  

Creating global instances or objects of structures

A technique to create instances of a structure which can be used by many functions,

Learning Objectives

  • Learning to declare global objects/instances of a structure along with the structure definition itself.

Source Code

TC++ #2777


Source Code

Run Output

Fruit           Weight  Price
Banana 45 3.25
Apple 55 10.5

Code Understanding

void setApple(void); //this function declaration is used to set values in apple object of struct
struct Fruit //This a struct which define some variables related to Fruit.
int weight;   //An integer variable that will contain the fruit weight
float price;  //A float variable that will contain the price of the fruit.
}apple,banana;  //These two are global instances/objects declared along with the structure itself.

int main() {
banana.weight=45; banana.price=3.25;
These variables of object banana are set in the main routine.

cout<<“FruitttWeighttPrice”<<endl; cout<<“—————————–“<<endl;
This will print as heading line of information we shall print further.

cout<<“Bananatt” <<banana.weight<<“t”<<banana.price<<endl;
This line prints the details picked from the banana object.

This function set the values in the apple object. The apple object is outside main() and setApple() functions still they can be accessed anywhere.

cout<<“Applett” <<apple.weight<<“t”<<apple.price<<endl;
Value of apple object are printed here.

void setApple() {apple.weight=55; apple.price=10.50;}
This function sets the variables in the apple object which is a global object. If object would have been declared inside the main routine these variables will not accessible.



  • Other than declaring global objects along with structure syntax near the ending braces, the global objects can also be declared in the global area above the main routine as follows –
    Fruit apple, banana;
    int main()
    { ………… }

Common Errors

  • Some student declare global objects after the semicolon of the structure as follows –
    struct Name{…};a,b
    This is incorrect. This should be  –
    struct Name { … }a,b;

Suggested Filename(s): stru-global-obj.cpp, strucglo.cpp


sunmitra| Created: 18-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