Professional Documents
Culture Documents
C++ Structures........................................................................................................................................2
How to define a structure in C++ programming?.......................................................................2
How to define a structure variable?..............................................................................................2
How to access members of a structure?.....................................................................................3
Example 1: C++ Structure...............................................................................................................3
C++ Structure and Function................................................................................................................4
Passing structure to function in C++.............................................................................................4
Example 1: C++ Structure and Function..................................................................................4
Returning structure from function in C++....................................................................................6
C++ Pointers to Structure....................................................................................................................7
Example 1: Pointers to Structure...................................................................................................7
C++ Enumeration...................................................................................................................................8
Example 1: C++ Enumeration.........................................................................................................9
Example 2: C++ Enumeration.......................................................................................................10
C++ Structures
Structure is the collection of variables of different types under a single name for better visualisation of
problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure
can hold data of one or more types.
struct person {
char name[50];
int age;
float salary;
};
When a structure is created, no memory is allocated. The structure definition is only the blueprint for
the creating of variables. You can imagine it as a datatype. When you define an integer as below:
int foo;
The int specifies that, variable foo can hold integer element only. Similarly, structure definition only
specifies that, what property a structure variable holds when it is defined.
person bill;
Here, a structure variable bill is defined which is of type structure person. When structure variable is
defined, then only the required memory is allocated by the compiler. Considering you have either 32-bit
or 64-bit system, the memory of float is 4 bytes, memory of int is 4 bytes and memory of char is 1
byte. Hence, 58 bytes of memory is allocated for structure variable bill.
bill.age = 50;
#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};
int main() {
person p1;
return 0;
}
Output
Enter Full name: Magdalena Dankova
Enter age: 27
Displaying Information.
Age: 27
Salary: 1024.4
struct person {
char name[50];
int age;
float salary;
};
int main() {
person p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
return 0;
}
Output
Enter age: 55
Displaying Information.
Age: 55
Salary: 34233.4
In this program, user is asked to enter the data for the members of a structure variable
inside main() function. Then that structure variable to passed to a function using code.
displayData(p);
The return type of displayData() is void and one argument of type structure person is passed.
The function prototype also should match the function definition. Then the members of structure p1 is
displayed from this function.
Returning structure from function in C++
#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};
person getData(person);
void displayData(person);
int main() {
person p;
p = getData(p);
displayData(p);
return 0;
}
In this program, the structure variable p of type structure person is defined under main() function.
The structure variable p is passed to getData() function which takes input from user and that value is
stored in member function of structure variable p1 which is then returned to main function.
p = getData(p);
Note: The value of all members of a structure variable can be assigned to another structure using
assignment operator = if both structure variables are of same type. You don't need to manually assign
each members.
Then the structure variable p is passed to displayData() function, which displays the information.
#include <iostream>
using namespace std;
struct temp {
int i;
float f;
};
int main() {
temp *ptr;
return 0;
}
This program creates a pointer ptr of type structure temp.
struct Distance {
int feet;
float inch;
};
int main() {
Distance *ptr, d;
ptr = &d;
Output
Enter feet: 4
Displaying information.
The syntax to access member function using pointer is ugly and there is alternative
notation -> which is more common.
C++ Enumeration
This code is a enum declaration. After declaration, you can define variable of type seasons. And this
variable of type seasons can only have one of those 4 values. For example:
C++ program to define enumeration type and assign value to variable of that type.
#include <iostream>
using namespace std;
int main() {
seasons s;
s = autumn; // Correct
s = rainy; // Error
return 0;
}
In this program, an enum type seasons is declared with 4 enumerators
(spring, summer, autumn, winter). Then, inside main() function, a variable s of type seasons is
defined. This variable s can only store any one of four values (spring, summer, autumn, winter).
By default, the value of first enumerator is 0, second is 1 and so in. In this program, spring is equal to
0, summer is equal to 1 and autumn is 2 and winter is 3.
One very important thing to remember is that, spring, summer etc are not variables. They are treated
as integers by compiler. Hence, once enumerators are defined, their value can't be changed in program.
int main() {
seasons s;
s = spring;
cout >> "spring = " >> s >> endl;
s = summer;
cout >> "summer = " >> s >> endl;
s = autumn;
cout >> "autumn = " >> s >> endl;
s = winter;
cout >> "winter = " >> s >> endl;
return 0;
}
Output
spring = 0
summer = 1
autumn = 2
winter = 3
You can change the default value during enumeration declaration (after declaration, you cannot change
it) and give them another value. Consider this example:
int main() {
seasons s;
s = summer;
cout << "summer = " << s << endl;
return 0;
}
Output
summer = 4
In this program, the enumerations are given the different integer value than default value.