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

FPCPP C3

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)
26 views

FPCPP C3

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/ 9

1

FUNDAMENTALS OF

3
PROGRAMMING IN C++
STRUCTURES, UNIONS AND
ENUMERATIONS
Structures 2
 Structures are a collection of data items of different types.
 Structures can also be seen as a way of creating user defined data types.
 Syntax:
struct structure_tag{
// member variables
};
• In the above syntax:
• The keyword struct declares this is a structure definition.
• The name of the structure definition is called structure tag.
• Structure tag is a legal identifier (or name) of the structure to be defined.
• The structure definition must placed outside any block at the global scope.
… 3
• Once a structure has been defined, it can be used just like the predefined types.
• Example:
struct complex {
struct complex { int img;
int img; int real;
int real; };
}num1, num2; complex num1, num2;
• Accessing member values (or variables) of a structure can be done using dot(.) operator.

num1.img=2;
• Its possible to copynum1.real=4;
a structure variable to another structure variable of the same type. Like
num2 = num1;
Initializing… 4
• A structure variable can be initialized at the time that it is declared.
• To give a structure variable a value,
• you follow it by an equal sign and a list of the member values enclosed in braces.
• E.g. complex num3 = {7, 6};
• It is an error if there are more initializers than struct members and if orders don’t match.
• If there are fewer initializer values than struct members, the provided values are used to initialize
data members, in order.
• The remaining will be assigned to default values.
Nested structures… 5
• We can have a structure definition inside another structure declaration called nested structure.
• E.g.
• struct person{
• struct student{
• string name;
• string dept;
• int age;
• string ID;
• string birthplace;
• person pratts;
• };
• };

• To access nested structures:


• student s1;
• s1.pratts.name= “Selam Alemayehu”;
• s1.pratts.age= 21;
• s1.pratts.birthplace= “Gondar”;
Structures & Functions 6
• Structure variables can be passed to and returned from a function as a normal variables do.
E.g.
struct Bacc{
void Transfer(Bacc SAc, Bacc RAc,float amount){
int accno;
if(SAc.balance> amount){
float balance;
RAc.balance+=amount;
};
Sac.balance-=amount;
}else{
ERROR-MSG
}
}
UNIONS 7
• Union is a user-defined datatype.
• All the members of union share same memory location.
• Size of union is decided by the size of largest member of union.
• If you want to use same memory location for two or more members, union is the best for that.
• E.g.
• union save{
• int a; • struct sample{ • save s1; // requires 8byte of memory
• float b; • int a; • sample s2; // requires 12byte of memory
• }; • float b;
};

• Unions are similar to structures. Union variables are created in same manner as structure
variables.
Enumerations 8
• An enumeration type is a type whose values are defined by a list of constants of type int.
• An enumeration type is very much like a list of declared constants.
• When defining an enumeration type:
• you can use any int values and can have any number of constants defined in an enumeration type.
• For example: the following enumeration type defines a constant for the length of each month.
… 9

You might also like