Lab 11 - ocal.info

Lab 11
Reusing member names
Structure variables in
assignment statements
Semicolon in a structure
definition
Functions can return structures
Hierarchical Structures
Initializing Structures
Program 1
 Write a C++ program that includes a definition for a
structure type for students consisting of a number, name
and grade. The name of the structure is “Student”. Print
the member values.
Solution to Program 1
#include <iostream>
using namespace std;
struct Student
{
int number;
string name;
char grade;
};
int main()
{
Student student1;
student1.number=1234;
student1.name="ahmed";
student1.grade='b';
cout<<student1.number<<" "<<student1.name<<" "<<student1.grade;
return 0;
}
Program 2
 Write a C++ program that includes a definition for a
structure type for mobiles consisting of a name and price.
The name of the structure is “MobileType”. Receive the
name and the price from the user and then print them.
Solution to Program 2
#include<iostream>
using namespace std;
struct MobileType
{
double price;
string name;
};
int main()
{
MobileType mobile1;
cout<<"please enter the price and name"<<endl;
cin>>mobile1.price>>mobile1.name;
cout<<"The price is "<<mobile1.price<<" and the name is
"<<mobile1.name<<endl;
}
Program 3
 Write a C++ program that includes a definition for
a structure type for employee consisting of a ID,
age and wage. The name of the structure is
“Employee”. Create a void function named
“printInformation” with one argument of type
Employee that prints the member names.
Initialize the structure in one line and then call the
function.
Solution to Program 3
#include <iostream>
using namespace std;
struct Employee
{
int id;
int age;
double wage;
};
void printInformation(Employee employee)
{
cout << "ID: " << employee.id << "\n";
cout << "Age: " << employee.age << "\n";
cout << "Wage: " << employee.wage << "\n";
}
int main()
{
Employee employee1 = { 14, 32, 24.15 };
printInformation(employee1);
return 0;
}
Program 4
 Write a C++ program that includes a definition for a
structure type for employee consisting of a ID, age, wage
and birthday. The name of the structure is “Employee”.
Write a definition for a structure for date consists of day,
month and year. The name of the structure is “Date”.
Create a void function named “printInformation” with one
argument of type Employee that prints the member names.
Initialize the structure in one line and then call the
function.
Solution to Program 4
#include <iostream>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
struct Employee
{
int id;
int age;
double wage;
Date birthday;
};
void printInformation(Employee employee)
{
cout << "ID: " << employee.id << "\n";
cout << "Age: " << employee.age << "\n";
cout << "Wage: " << employee.wage << "\n";
cout << "Birthday: " << employee.birthday.day << "/"<<employee.birthday.month<<"/"<<employee.birthday.year<<endl;
}
int main()
{
Employee employee1 = { 14, 32, 24.15,1,1,2000 };
printInformation(employee1);
return 0;
}
Program 5
 Write a C++ program that includes a definition for
a structure type for fruits consisting of a number
and country. The name of the structure is “fruit”.
Create two structure variables, one is called
“apples” and second is called “oranges”. Copy the
member values from oranges to apples. Print the
values of both structures.
Solution to Program 5
#include <iostream>
using namespace std
struct fruit
{
int number;
string country;
};
int main()
{
fruit oranges={5,"egypt"};
fruit apples;
apples=oranges;
cout<<oranges.number<<" "<<oranges.country<<endl;
cout<<apples.number<<" "<<apples.country<<endl;
return 0;
}
Program 6
 Write a C++ program that includes a definition for a
structure type for database consisting of id_number and
age, salary .
 The name of the structure is “database”, Use this structure
in the main program and store the ID number, age and
salary of an employee and print it.
Program 7
 Write a C++ program that includes a definition for a
structure type for Books consisting of a title, author,
subject.
 The name of the structure is “Books”. Create a void
function named “printBook” with one argument of type
Books that prints the Book details. Initialize the structure
in the main and then call the function.
 The Output should looks like:
Program 8
 Write a C++ program that includes a definition for a
structure type for Movie consisting of a title and year .
 The name of the structure is “movie”. Create two
structure variables, one is called “yours” and second is
called “mine”.
 Take input from user with his favorite movie and its date.
 The output should looks like:
Program 9
 Write a C++ program that includes a definition for
a structure for university consisting of a name,
location and phone number. The name of the
structure is “University”. Receive the inputs from
the user and then print them.
Program 10
 Write a C++ program that includes a definition for
a structure type for city consisting of a name,
location, number of people and country. The
name of the structure is “city”. Print the member
values.