Structs in C++ Programming

 
Structs
 
COMP 11
Example: Celebrity Heights
#name              feet  inches
Danny_Devito    5   0
Oprah_Winfrey   5   7
Tom_Cruise      5   7
Nicole_Kidman   5   10.5
Bernie_Mac      6   2.5
 
Q: How should we store the data?
A: 3 arrays of same length
Names
Feet
Inches
Task: Write a program that reads in a list of
celebrity heights from a file and outputs
***insert generic statistic here***.
A: 1 array of “packaged” data
Structs
 
A “struct” is a package used to hold different types of data.
-
can be passed around like any other variable
-
we can access “members” of a struct individually
-
need to define the struct’s “type” first
Names
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Components of a struct definition:
2.
Name (typically capitalized)
3.
Member declarations (in { })
4.
Semicolon after { }
}
1.
struct
 keyword
Working with struct
variables
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  
cout
 << 
"Name: "
 << one_celeb.name << 
endl
;
  
cout
 << 
"Height: "
 << one_celeb.feet << 
" feet, "
;
  
cout
 << one_celeb.inches << 
" inches"
 << 
endl
;
}
Names
Feet
10.5
one_celeb
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
struct
 Professor {
  string name;
  string courses[
10
];
  
int
 age;
};
int
 
main
() {
  Celeb one_celeb;
  Professor richard;
  one_celeb.name = 
"Nicole_Kidman"
;
  richard.name = 
"Richard"
;
  richard.courses[
0
] = 
"COMP 11"
;
}
Names
Totally fine! Member names are 
local
to a given struct.
Different structs, same members?
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
Structs, Functions
Names
Feet
 
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
 
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  return
 
0
;
}
 
Structs, Functions
 
Names
 
Feet
This modifies a 
copy
 of the struct passed
as an argument, not the original.
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
Structs, Functions
Names
Feet
10.5
5
Nicole_Kidman
10.5
5
Nicole_Kidman
c
 
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
 
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
 
Names
 
Feet
10.5
5
Nicole_Kidman
10.5
5
Nicole_Kidman
 
 
Structs, Functions
 
 
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
 
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
 
Names
 
Feet
10.5
5
Nicole_Kidman
10.5
105
Nicole_Kidman
 
 
Structs, Functions
 
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
Names
Feet
10.5
5
Nicole_Kidman
10.5
105
Nicole_Kidman
 
Structs, Functions
 
 
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  one_celeb.name = 
"Nicole_Kidman"
;
  one_celeb.feet = 
5
;
  one_celeb.inches = 
10.5
;
  one_celeb = grow(one_celeb);
  cout << one_celeb.name << 
" is now "
;
  cout << one_celeb.feet << 
" feet tall!";
  cout << endl;
  
return
 
0
;
}
 
Names
 
Feet
 
 
Structs, Functions
 
 
Celeb grow(Celeb c) {
  c.feet += 
100
;
  
return
 c;
}
10.5
105
Nicole_Kidman
Names
Feet
Exercise: Structs, Functions
1.
Which part of this code
defines a struct type?
1.
How many members does
the Bulb type have?
1.
What does this code print?
struct
 Bulb {
  
int
 watts;
  
int
 lumens;
};
Bulb 
dim
(Bulb b) {
  Bulb c = b;
  c.watts += 
10
;
  
return
 b;
}
int
 
main
() {
  Bulb lightA;
  lightA.watts = 
20
;
  Bulb lightB = dim(lightA)
  
cout
 << lightB.watts << 
" "
 << lightB.lumens;
  
return
 
0
;
}
Names
Feet
 
Exercise: Structs, Functions
 
1.
Which part of this code
defines a struct type?
1.
How many members does
the Bulb type have?
1.
What does this code print?
struct
 Bulb {
  
int
 watts;
  
int
 lumens;
};
Bulb 
dim
(Bulb b) {
  Bulb c = b;
  c.watts += 
10
;
  
return
 b;
}
int
 
main
() {
  Bulb lightA;
  lightA.watts = 
20
;
  Bulb lightB = dim(lightA)
  
cout
 << lightB.watts << 
" "
 << lightB.lumens;
  
return
 
0
;
}
Two members
20  <???>
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  one_celeb = grow(one_celeb);
  //Do stuff with one_celeb
  
return
 
0
;
}
Names
Feet
10.5
5
Nicole_Kidman
A: 
Every
 array element is copied!
Q: What happens on the stack when
we pass this struct as an argument?
 
movies[0]
movies[1]
movies[99]
Lots of array
elements...
So many more
copies of
array
elements...
Structs, Functions
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
 
Names
 
Feet
10.5
5
Nicole_Kidman
movies[0]
movies[1]
movies[99]
Lots of array
elements...
 
Structs, Functions
 
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  one_celeb = grow(one_celeb);
 
  //Do stuff with one_celeb
  
return
 
0
;
}
 
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
Names
Feet
Structs, Functions, Pointers
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
Celeb grow(Celeb c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  one_celeb = grow(one_celeb);
  //Do stuff with one_celeb
  
return
 
0
;
}
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
Pass a struct pointer to
avoid excessive copying!
10.5
5
Nicole_Kidman
movies[0]
movies[1]
movies[99]
Lots of array
elements...
Names
Feet
10.5
5
Nicole_Kidman
movies[0]
movies[1]
movies[99]
Lots of array
elements...
celeb_p
c
Structs, Functions, Pointers
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
void
 grow(Celeb *c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  Celeb *celeb_p = &one_celeb;
  grow(celeb_p);
  //Do stuff with one_celeb
  
return
 
0
;
}
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
void
 grow(Celeb *c) {
  (*c).feet += 
100
;
}
Pass a struct pointer to
avoid excessive copying!
Names
Feet
Structs, Functions, Pointers
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
void
 grow(Celeb *c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  Celeb *celeb_p = &one_celeb;
  grow(celeb_p);
  //Do stuff with one_celeb
  
return
 
0
;
}
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
void
 grow(Celeb *c) {
  (*c).feet += 
100
;
}
Pass a struct pointer to
avoid excessive copying!
10.5
105
Nicole_Kidman
movies[0]
movies[1]
movies[99]
Lots of array
elements...
celeb_p
c
Structs, Functions, Pointers
struct
 Celeb {
  string name;
  
int
 feet;
  
float
 inches;
  string every_movie[
100
];
};
void
 grow(Celeb *c);
int
 
main
() {
  Celeb one_celeb;
  //Initialize one_celeb
  Celeb *celeb_p = &one_celeb;
  grow(celeb_p);
  //Do stuff with one_celeb
  
return
 
0
;
}
Names
Feet
Structs are passed-by-value: 
every
member is copied when passed
to/returned from a function.
 
void
 grow(Celeb *c) {
  c->feet += 
100
;
}
Pass a struct pointer to
avoid excessive copying!
Access member 
mem
 of struct pointer
p
 with either 
(*p).mem
 OR 
p->mem
.
10.5
105
Nicole_Kidman
movies[0]
movies[1]
movies[99]
Lots of array
elements...
celeb_p
Demo: structs, functions, and pointers
Names
Feet
Task: Write a program that reads in a list of celebrity heights from a file and outputs
***insert generic statistic here***. The file will start with some integer 
n
, then the
following 
n
 lines will each have the height info for one celebrity.
Slide Note

When we started talking about pointers, one of the main motivations I gave for their use was to return multiple values from a function. Remember the “swap” function? It used pointer arguments to modify variables outside of the function’s scope.

Structs provide another way of dealing with multiple values at once, and can help better structure our code and our problem solving process.

To help motivate them, I have an insanely vapid example problem.

Embed
Share

Learn about structs in C++ programming, how to define and work with them, store and manipulate data using structs, and the differences with functions. Explore examples of working with struct variables, different structs with the same members, and utilizing functions with structs in C++.

  • C++
  • Programming
  • Data Structures
  • Structs

Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Structs COMP 11

  2. Example: Celebrity Heights #name feet inches Task: Write a program that reads in a list of celebrity heights from a file and outputs ***insert generic statistic here***. Danny_Devito 5 0 Oprah_Winfrey 5 7 Q: How should we store the data? A: 3 arrays of same length Tom_Cruise 5 7 Nicole_Kidman 5 10.5 A: 1 array of packaged data Bernie_Mac 6 2.5

  3. Structs A struct is a package used to hold different types of data. - can be passed around like any other variable - we can access members of a struct individually - need to define the struct s type first Components of a struct definition: 1. struct keyword struct Celeb { string name; int feet; float inches; }; } 2. Name (typically capitalized) 3. Member declarations (in { }) 4. Semicolon after { }

  4. Working with struct variables struct Celeb { string name; int feet; float inches; }; Nicole_Kidman one_celeb 5 int main() { Celeb one_celeb; 10.5 *bookkeeping* one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; return value return address argv cout << "Name: " << one_celeb.name << endl; cout << "Height: " << one_celeb.feet << " feet, "; cout << one_celeb.inches << " inches" << endl; argc }

  5. Different structs, same members? struct Celeb { string name; int feet; float inches; }; struct Professor { string name; string courses[10]; int age; }; Totally fine! Member names are local to a given struct. int main() { Celeb one_celeb; Professor richard; one_celeb.name = "Nicole_Kidman"; richard.name = "Richard"; richard.courses[0] = "COMP 11"; }

  6. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; }

  7. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } This modifies a copy of the struct passed as an argument, not the original. one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; }

  8. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } Nicole_Kidman c 5 10.5 one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; Nicole_Kidman one_celeb 5 10.5 }

  9. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } Nicole_Kidman 5 10.5 one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; Nicole_Kidman 5 10.5 }

  10. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } Nicole_Kidman 105 10.5 one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; Nicole_Kidman 5 10.5 }

  11. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } Nicole_Kidman 105 10.5 one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; Nicole_Kidman 5 10.5 }

  12. Structs, Functions struct Celeb { string name; int feet; float inches; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; one_celeb.name = "Nicole_Kidman"; one_celeb.feet = 5; one_celeb.inches = 10.5; Celeb grow(Celeb c) { c.feet += 100; return c; } one_celeb = grow(one_celeb); cout << one_celeb.name << " is now "; cout << one_celeb.feet << " feet tall!"; cout << endl; return 0; Nicole_Kidman 105 10.5 }

  13. Exercise: Structs, Functions struct Bulb { int watts; int lumens; }; 1. Which part of this code defines a struct type? Bulb dim(Bulb b) { Bulb c = b; c.watts += 10; return b; } 1. How many members does the Bulb type have? 1. What does this code print? int main() { Bulb lightA; lightA.watts = 20; Bulb lightB = dim(lightA) cout << lightB.watts << " " << lightB.lumens; return 0; }

  14. Exercise: Structs, Functions struct Bulb { int watts; int lumens; }; Two members 1. Which part of this code defines a struct type? Bulb dim(Bulb b) { Bulb c = b; c.watts += 10; return b; } 1. How many members does the Bulb type have? 1. What does this code print? int main() { Bulb lightA; lightA.watts = 20; Bulb lightB = dim(lightA) cout << lightB.watts << " " << lightB.lumens; return 0; } 20 <???>

  15. So many more copies of array elements... Structs, Functions struct Celeb { string name; int feet; float inches; string every_movie[100]; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; //Initialize one_celeb Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 5 10.5 movies[0] movies[1] Lots of array elements... one_celeb = grow(one_celeb); Q: What happens on the stack when we pass this struct as an argument? //Do stuff with one_celeb return 0; } A: Every array element is copied! movies[99]

  16. Structs, Functions struct Celeb { string name; int feet; float inches; string every_movie[100]; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; //Initialize one_celeb Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 5 10.5 movies[0] movies[1] Lots of array elements... one_celeb = grow(one_celeb); //Do stuff with one_celeb return 0; } movies[99]

  17. Structs, Functions, Pointers struct Celeb { string name; int feet; float inches; string every_movie[100]; }; Celeb grow(Celeb c); int main() { Celeb one_celeb; //Initialize one_celeb Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 5 10.5 movies[0] Pass a struct pointer to avoid excessive copying! movies[1] Lots of array elements... one_celeb = grow(one_celeb); //Do stuff with one_celeb return 0; } movies[99]

  18. Structs, Functions, Pointers c celeb_p struct Celeb { string name; int feet; float inches; string every_movie[100]; }; void grow(Celeb *c); int main() { Celeb one_celeb; //Initialize one_celeb Celeb *celeb_p = &one_celeb; grow(celeb_p); Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 5 10.5 movies[0] Pass a struct pointer to avoid excessive copying! movies[1] Lots of array elements... void grow(Celeb *c) { (*c).feet += 100; } //Do stuff with one_celeb return 0; } movies[99]

  19. Structs, Functions, Pointers c celeb_p struct Celeb { string name; int feet; float inches; string every_movie[100]; }; void grow(Celeb *c); int main() { Celeb one_celeb; //Initialize one_celeb Celeb *celeb_p = &one_celeb; grow(celeb_p); Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 105 10.5 movies[0] Pass a struct pointer to avoid excessive copying! movies[1] Lots of array elements... void grow(Celeb *c) { (*c).feet += 100; } //Do stuff with one_celeb return 0; } movies[99]

  20. Structs, Functions, Pointers celeb_p struct Celeb { string name; int feet; float inches; string every_movie[100]; }; void grow(Celeb *c); int main() { Celeb one_celeb; //Initialize one_celeb Celeb *celeb_p = &one_celeb; grow(celeb_p); Nicole_Kidman Structs are passed-by-value: every member is copied when passed to/returned from a function. 105 10.5 movies[0] Pass a struct pointer to avoid excessive copying! movies[1] Lots of array elements... void grow(Celeb *c) { c->feet += 100; } //Do stuff with one_celeb return 0; } Access member mem of struct pointer p with either (*p).mem OR p->mem. movies[99]

  21. Demo: structs, functions, and pointers Task: Write a program that reads in a list of celebrity heights from a file and outputs ***insert generic statistic here***. The file will start with some integer n, then the following n lines will each have the height info for one celebrity.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#