You are on page 1of 13

Structure

CHAPTER 6
Structure Definitions
 A structure is a set of diverse types of data with different
lengths grouped under a unique declaration.
 The syntax is: struct model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;
 model_name is a name for the model of the structure type.
 object_name is a valid identifier (or identifiers) for structure objects.
 Within key brackets { } the types and their names corresponding to
the elements that compose the structure are listed.
Computer Programming 12/20/2022 2
continued

 If the structure definition includes the parameter model_name,


that parameter becomes a valid type name equivalent to the
structure.
 For example: struct products {
char name [35];
float price;
} ;
.
.
// declaring three objects of that type
products apple;
products orange, melon;

Computer Programming 12/20/2022 3


Example 1:
#include <iostream> cout << "Enter age: ";
using namespace std; cin >> p1.age;
cout << "Enter salary: ";
struct Person cin >> p1.salary;
{ cout << "\nDisplay Info."
char name[50]; << endl;
int age; cout << "Name: "
<< p1.name
float salary;
<< endl;
}; cout <<"Age: "
int main() << p1.age
<< endl;
{
cout << "Salary: "
Person p1; << p1.salary;
cout << "Enter Name: "; return 0;
cin.get(p1.name, 50); }
Computer Programming 12/20/2022 4
Structure and Function
 A structure variable can be passed to a function in similar way
as normal argument.
For example:
#include <iostream>
using namespace std;

struct Person
{
char name[50];
int age;
float salary;
};

void displayData(Person); // Function declaration


Computer Programming 12/20/2022 5
Continued
int main() {
Person p; Enter Full name: Abe Kebe
cout << "Enter Full name: "; Enter age: 20
Enter salary: 823.50
cin.get(p.name, 50);
cout << "Enter age: "; Displaying Information.
cin >> p.age; Name: Abe Kebe
cout << "Enter salary: "; Age: 20
Salary: 823.50
cin >> p.salary;
// Function call with structure variable as an argument
displayData(p);
return 0;
}
void displayData(Person p) {
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary << endl;
}
Computer Programming 12/20/2022 6
Example 2:
Person getData(Person p) {
#include <iostream> cout << "Enter Full name: ";
using namespace std; cin.get(p.name, 50);
cout << "Enter age: ";
struct Person { cin >> p.age;
char name[50]; cout << "Enter salary: ";
int age; cin >> p.salary;
float salary; return p;
}; }
void displayData(Person p) {
Person getData(Person); cout << "\nDisplay Information."
void displayData(Person); << endl;
int main() { cout << "Name: " << p.name
Person p; << endl;
p = getData(p); cout <<"Age: " << p.age << endl;
displayData(p); cout << "Salary: " << p.salary
return 0; << endl;
} }
Computer Programming 12/20/2022 7
Pointers to structures
 Like any other type, structures can be pointed by pointers.

 The rules are the same than for fundamental data types,

 The pointer must be declared as pointer to the structure:

For example:
struct products {
char name [35];
float price;
} ;

products a_product;
// a_product is an object of struct type products
products* p_product;
// p_product is a pointer to point to objects of products
Computer Programming 12/20/2022 8
Example 3:
#include <iostream> cout << "Enter feet: ";
using namespace std; cin >> (*ptr).feet;
cout << "Enter inch: ";
struct Distance { cin >> (*ptr).inch;
int feet;
float inch; cout << "Displaying
}; information." << endl;
cout << "Distance = "
int main() << (*ptr).feet
{ << " feet "
Distance *ptr, d; << (*ptr).inch
<< " inches" << endl;
ptr = &d;
return 0;
}

Computer Programming 12/20/2022 9


Enumeration Definition

 An enumeration is a user-defined data type that consists


of integral constants. To define an enumeration, keyword
enum is used.
 The syntax is: enum enum_name {const1, const2, const3,..., const-n};

Computer Programming 12/20/2022 10


#include <iostream>
Example 4
using namespace std;
enum designFlags {
BOLD = 1,
ITALICS = 2,
UNDERLINE = 4
};
int main() {
int myDesign = BOLD | UNDERLINE;
// 00000001
// | 00000100
// ___________
// 00000101
cout << myDesign;
return 0;
}
Computer Programming 12/20/2022 11
#include <iostream> Example 5
using namespace std;
enum Season { null, Summer, Rainy, Autumn, Winter, Spring};

int main()
{
Season season;

cout<<"What is your favorite season\n"


" 1. Summer\n"
" 2. Rainy\n"
" 3. Autumn\n"
" 4. Winter\n"
" 5. Spring\n"
"> ";
int ch;
cin >> ch;
Computer Programming 12/20/2022 12
Example 5
switch (ch){
case 1: season=Summer;
break;
case 2: season=Rainy;
break;
case 3: season=Autumn;
break;
case 4: season=Winter;
break;
case 5: season=Spring;
break;
default: season=null;
}
if(season==null) cout<<"Hey, that's not a fruit!\n";
else cout<<"That's my favourite too'!\n";
}
Computer Programming 12/20/2022 13

You might also like