C++ Structures

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CHAPTER 6

▪ 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 3/20/2021 2
▪ 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 3/20/2021 3


#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 3/20/2021 4
▪ 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 3/20/2021 5
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 3/20/2021 6
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 3/20/2021 7
▪ 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 3/20/2021 8
#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 3/20/2021 9


▪ An enumeration is a user-defined data type that consists
of integral constants. To define an enumeration, keyword
enum is used.
▪ The syntax is: { , , , ,
};

Computer Programming 3/20/2021 10


#include <iostream>
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 3/20/2021 11


#include <iostream>
using namespace std;
enum Season { null, Summer, Rainy, Autumn, Winter, Spring};
int main()
{
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 3/20/2021 12
switch (ch){
case 1: =Summer;
break;
case 2: =Rainy;
break;
case 3: =Autumn;
break;
case 4: =Winter;
break;
case 5: =Spring;
break;
default: =null;
}
if( ==null) cout<<"Hey, that's not a fruit!\n";
else cout<<"That's my favourite too'!\n";
}
Computer Programming 3/20/2021 13

You might also like