0% found this document useful (0 votes)
13 views

Programming Fundamentals 11

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Fundamentals 11

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

COMPUTER

PROGRAMMING
STRUCTURES
• We often come around situations where we need to store a group of data whether of
similar data types or non-similar data types. We have seen Arrays in C++ which are used
to store set of data of similar data types at contiguous memory locations.
Unlike Arrays, Structures in C++ are user defined data types which are used to store
group of items of non-similar data types.
C++ STRUCTURES

• Structures (also called structs) are a way to group several related variables into one place.
Each variable in the structure is known as a member of the structure.
• Unlike an array, a structure can contain many different data types (int, string, bool, etc.).
CREATE A STRUCTURE

• The ‘struct’ keyword is used to create a structure. The general syntax to create a structure is as shown below:

struct structureName{
member1;
member2;
member3;
.
memberN;
};
• Structures in C++ can contain two types of members:
• Data Member: These members are normal C++ variables. We can create a structure with
variables of different data types in C++.
• Member Functions: These members are normal C++ functions. Along with variables, we
can also include functions inside a structure declaration.
CODE EXPLANATION:

1. Include the iostream header file in our program to use the functions defined in it.

2. Include the std namespace to use its classes without calling it.

3. Create a struct named Person.

4. The beginning of the struct body.

5. Create a struct member named citizenship of type integer.

6. Create a struct member named age of type integer.

7. End of the struct body.

8. Call the main() function. The program logic should be added within the body of this function.

9. Create an instance of the struct Person and giving it the name p.

10. Set the value of struct member citizenship to 1.

11. Set the value of struct member age to 27.

12. Print the value of the struct member citizenship on the console alongside some other text.

13. Print the value of the struct member age on the console alongside some other text.

14. The program should return a value if it runs successfully.

15. End of the main() function.


EXAMPLE

Struct Phone
{
Int ncode;
Int acode;
Long number;
};
Int main () {
Phone p1, p2= {092, 051, 333333};
Cout<<“enter national code”;
cin>>p1.ncode;
Cout<<“enter area code”;
cin>>p1.acode;
Cout<<“enter Phone Number 1:”;
cin>>p1.number;
Cout<<p1.ncode<<“-“<<p1.acode<<“-“<<p1.number<<endl;
Cout<<“enter Phone Number 2:”;
cin>>p1.number;
Cout<<p2.ncode<<“-“<<p2.acode<<“-“<<p2.number<<endl;
Return 0;
}
LIMITATION OF A C++ STRUCTURE

• The following are the limitations of structures:

• The struct data type cannot be treated like built-in data types.
• Operators like + -, and others cannot be used on structure variables.
• Structures don’t support data hiding. The members of a structure can be accessed by any
function regardless of its scope.
• Static members cannot be declared inside the structure body.
• Constructors cannot be created inside a structure.
SUMMARY

• A struct is a data structure that stores data elements belonging to different types.
• Whereas an array stores data elements of a similar type, a struct stores data elements of different types.
• A struct should be used when the data elements are not expected to change value.
• The members of a struct are accessed using the dot (.) operator.
• We have to create an instance of the struct.
• To create a C++ struct, we use the struct keyword.
• Pointers pointing to a struct are created similarly to how pointers which is pointing to regular types are
created.
• A struct can be passed as an argument to a function in the same way ordinary functions are passed.

You might also like