0% found this document useful (0 votes)
40 views22 pages

Chapter Six - Structures - All Parts

Haramaya University Cpp Pdf

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views22 pages

Chapter Six - Structures - All Parts

Haramaya University Cpp Pdf

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer

Programming {
Chapter Six: Structures
in C++
{
1
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Outline
1.Specifying simple structure
2.Defining a structure variable
3.Accessing structure variable

2
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Records
▪ Aggregates of several items of possibly different types that represent related
information called RECORD
E.g
▪ Student Record:
▪ Name a string
▪ HW Grades an array of 3 doubles
▪ Test Grades an array of 2 doubles
▪ Final Average a double
▪ Bank Record:
▪ Customer Name
▪ Account Balance
▪ Amount
3
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Records
▪ Arrays
▪ Recall that elements of arrays must all be of the same type

scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99

▪ In some situations, we wish to group elements of different types

employee R. Jones 123 Elm 6/12/55 $14.75


4
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structures in C++
▪ A Structure is a container, it can hold a bunch of things. These things can be of any
type.

▪ They are commonly used when you want to represent an object with multiple
attributes

▪ Structures are used to organize related data (variables) into a nice and neat package.

▪ A struct is heterogeneous in that it can be composed of data of different types.

▪ Structure is declared using C++ ‘struct’ keyword

▪ A structure definition is a user-defined variable type: You can create variables from it
just like you would from any other type
5
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structures in C++
▪ Before creating a structure variable you must create a structure definition
▪ Syntax:
struct structname Tag
{
datatype1 variable1;
Members
datatype2 variable2;
};
▪ This is simply a data blue print
▪ Individual components of a struct type are called members (or fields).
▪ Each member has a name, a type - Names follow the rules for variable names.
▪ Types can be any defined type
6
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
struct Examples
struct Date { struct StudentGrade{
char Name[15];
int day; The “Date” structure The “StudentGrade”
char Course[9];
int month; has 3 members, int Lab[5]; structure has 5
int year; day, month & year. int Homework[3]; members of
}; int Exam[2]; different array types.
};

struct StudentInfo{
int Id; struct BankAccount{
The “StudentInfo” char Name[15]; The “BankAcount”
int age; structure has simple,
structure has 4 int AcountNo[10];
char Gender; array and structure
members double balance;
double CGPA; types as members.
Date Birthday;
}; of different types.
};
7
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Declaring and using structs
▪ By defining a structure you create a new data type.
▪ Once you have defined a structure you can create a variable from it just
as you would any other variable. struct StudentRecord
▪ Syntax: <struct-type> <identifier_list>; {
char Name[15];
▪ Example: StudentRecord Student1, Student2; int Id;
char Dept[5];
char Gender;
};
Name Name

Student1 Id Gender Id Gender Student2


Dept Dept

Student1 and Student2 are variables of StudentRecord type. 8


Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Declaring and using structs
▪ The members of a struct type variable are accessed with
the dot (.) operator:
Student1
syntax: <struct-variable>.<member_name>;
▪ Example: Name

Student1.Name= “Kibru G."; Id Gender


Student1.Id = 12345; Dept
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
Kibru G.
cout << "The student is ";
12345 M
switch (Student1.gender){
COMP
case 'F': cout << "Ms. "; break;
case 'M': cout << "Mr. "; break; }
cout << Student1.Name << endl;
9
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
struct-to-struct assignment
▪ The values contained in one struct type variable can be assigned to
another variable of the same struct type.
▪ Example:
strcpy(Student1.Name, “Kibru G."); Student1
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
Student2 = Student1;

Copies the entire structure from Student1 to Student2


Student2

10
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structure Example
#include<iostream.h>
cin>>s2.id;
struct student
{ cout<<"\nEnter Name";
int id;
char name[15]; cin>>s2.name;
}; cout<<"\nStudents Information";
int main()
{ cout<<"\n Student id\t Student Name";
//creating three student variables cout<<endl<<s1.id<<"\t\t"<<s1.name;
student s1,s2;
cout<<"\n Enter Student Id"; cout<<endl<<s2.id<<"\t\t"<<s2.name;
cin>>s1.id; return 0;
cout<<"\nEnter Name";
cin>>s1.name; }
cout<<"\n Enter Student Id";

11
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Aggregate Operations with Structures
Limitations on aggregate operations
▪ no I/O cout << s1;
cin >> s1;

▪ no arithmetic operations old_part = s1 + s2;

▪ no comparisons if (s1 < s1)


cout << ...;
12
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Aggregate Operations with Structures
▪ struct variables must be compared member-wise.

▪ To compare the values of student and newStudent, you must


compare them member-wise, as follows:

if(student.firstName == newStudent.firstName &&

student.lastName == newStudent.lastName) ...

13
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Array of structs
▪ What if we have a record of 120100 students

▪ Array of records

▪ First declare a struct (such as student)

▪ Then specify an array of that type

▪ student stud1[50];

▪ Declares an array of 50 student data type


14
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Array of structs Example
#include<iostream.h> cout<<"\nEnter Name";
struct student { cin>>s[i].name;
int id; }
char name[15]; }; cout<<"\n Displaying student Info";
int main() { cout<<"\nStudent Id \t
//creating 10 student using an array Student Name";
cout<<"\n============================
student s[10]; ";
int i; for( i = 0; i < 5; i++)
for(i=0; i < 10; i ++) { cout<<endl<<s[i].id<<"\t\t\t"<<s[i].name;
cout<<"\n Enter Student Id"; return 0;
cin>>s[i].id; }
15
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Declaring a struct as a member of struct
▪ struct employee {
string name;
string id;
date birthdate;
}emp1,emp2;
▪ struct date{
int day,month,year;
};
▪ To access the year that emp1 was born
▪ emp1.birthdate.year
▪ emp2.birthdate.month // access the month
16
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Defining a structure in a structure
▪struct employee {
string name;
string id;
struct date{
int day,month,year;
}; birthdate;
}emp1,emp2;
▪emp1.birthdate.year;
▪emp2.birthdate.month;
17
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Accessing Struct Members using pointres
▪ Individual members of a struct variable can be accessed using
pointers if a pointer to the struct has been declared and
initialized
▪ Example:
▪ Some_name *myptr = &mystruct ;
by using the structure pointer operator (the “->“):
myptr -> letter ;
which could also be written as:
(*myptr).letter ;
18
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Sample Program
#include <iostream.h>
struct personal //Create a struct but don’t reserve space.
{ long id;
float gpa; };
struct identity //Create a second struct that includes the first one.
{ char name[30];
struct personal person;
};
int main() {
19
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Sample Program
struct identity js,*ptr = &js ;

cout<<"enter student name: ";

cin>>js.name;

cout<<"enter student id: ";

cin>>js.person.id;

cout<<"enter student gpa: ";

cin>>js.person.gpa;

cout<<ptr->name<<" "<<ptr->person.id<<" "<<ptr->person.gpa<<endl;

return 0; }
20
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Structure with Member Functions
#include <iostream>
// Member function to display student details
#include <string> void displayInfo() {
struct Student { std::cout << "Student Name: " << name << std::endl;
std::string name; std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "Grade: " << grade << std::endl;
int rollNumber;
}
char grade;
};
// Member function to display student details int main() {
void registerStudent() { Student student1;
std::cout << "Student Name: "<<std::endl; // Using the member function
student1.registerStudent();
std::cin>>name;
student1.displayInfo();
std::cout << "Roll Number: " <<std::endl;
return 0;
std::cin>>rollNumber; }
std::cout << "Grade: "<< std::endl;
std::cin>>grade;
}
21
Computer Programming Chapter Six: Structures in C++ By: Kibru G.
Chapter 6;
}
22

You might also like