Structures in C++.

Slides:



Advertisements
Similar presentations
Chapter 7 Completing a Program
Advertisements

1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Introduction to Programming Lecture 39. Copy Constructor.
Structures Spring 2013Programming and Data Structure1.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
Chapter 11: Records (structs)
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
1 C++ Structures Starting to think about objects...
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
 Review structures  Program to demonstrate a structure containing a pointer.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Programmer Defined Structures (Records)
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Struct Data Type in C++ What Are Structures?
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Pointers and Dynamic Arrays
Classes and Data Abstraction
11 Chapter Structured Data
Command Line Arguments
Learning Objectives Pointers Pointer in function call
Chapter 11: Structured Data.
Structs versus Classes
Anatomy of a class Part I
Structures.
Structures Lesson xx In this module, we’ll introduce you to structures.
Variables with Memory Diagram
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Array of Structures A structure holds only one record But a record
Chapter 11: Structured Data.
Starting to think about objects...
Structure As Function Arguments
Pointers Lecture 2 Tue, Jan 24, 2006.
Introduction to Programming
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
CPS120: Introduction to Computer Science
C++ Pointers and Strings
Introduction to Programming
EECE.2160 ECE Application Programming
Starting to think about objects...
C Programming Lecture-13 Structures
CPS120: Introduction to Computer Science
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Passing Arguments and The Big 5
Standard Version of Starting Out with C++, 4th Edition
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
C Programming Lecture-14 Unions
Structure (i.e. struct) An structure creates a user defined data type
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Structures in c By Anand George.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ Pointers and Strings
Anatomy of a class Part I
Programming Fundamental
Programming Fundamental
Presentation transcript:

Structures in C++

Definition A Structure is a container, it can hold a bunch of variables of different types Structures are used to organize related data (variables) in to a nice neat package. C++ Spring 2000 Structures

Example - Student Record Name a string HW Grades an array of 3 doubles Test Grades an array of 2 doubles Final Average a double C++ Spring 2000 Structures

Structure Members Each thing in a structure is called member. Each member has a name, a type and a value. Names follow the rules for variable names. Types can be any defined type. C++ Spring 2000 Structures

Example Structure Definition struct StudentRecord { char *name; // student name double hw[3]; // homework grades double test[2]; // test grades double ave; // final average }; C++ Spring 2000 Structures

Using a struct By defining a structure you create a new data type. Once a struct is defined, you can create variables of the new type. StudentRecord stu; C++ Spring 2000 Structures

cout << stu.name << endl; Accessing Members You can treat the members of a struct just like variables. You need to use the member access operator '.' (pronounced "dot"): cout << stu.name << endl; stu.hw[2] = 82.3; stu.ave = total/100; C++ Spring 2000 Structures

Structure Assignment You can use structures just like variables: StudentRecord s1,s2; s1.name = "Joe Student"; … s2 = s1; Copies the entire structure C++ Spring 2000 Structures

Be Careful If a member is a pointer, copying means copying the pointer (not what is pointed to). "Joe Student" name hw test ave C++ Spring 2000 Structures

Probably not what you want StudentRecord s1,s2; s1.name = "Joe Student"; … s2 = s1; s2.name = "Jane Doe"; // now s1.name and s2.name are both // "Jane Doe" C++ Spring 2000 Structures

Pointers to Structures Pointers to structures are used often. There is another member access operator used with pointers: -> StudentRecord *sptr; … cout << "Name is" << sptr->name; cout << "Ave is " << sptr->ave; it looks like a "pointer"! C++ Spring 2000 Structures

Sample Function (won't work!) void update_average( StudentRecord stu) { double tot=0; for (int i=0;i<3;i++) tot += stu.hw[i]; tot += stu.test[i]; stu.ave = tot/5; } C++ Spring 2000 Structures

This one works void update_average( StudentRecord *stu) { double tot=0; for (int i=0;i<3;i++) tot += stu->hw[i]; tot += stu->test[i]; stu->ave = tot/5; } C++ Spring 2000 Structures

Or use a reference parameter void update_average( StudentRecord &stu) { double tot=0; for (int i=0;i<3;i++) tot += stu.hw[i]; tot += stu.test[i]; stu.ave = tot/5; } C++ Spring 2000 Structures

Other stuff you can do with a struct You can also associate special functions with a structure (called member functions). A C++ class is very similar to a structure, we will focus on classes. Classes can have (data) members Classes can have member functions. Classes can also hide some of the members (functions and data). C++ Spring 2000 Structures

Quick Example struct StudentRecord { char *name; // student name double hw[3]; // homework grades double test[2]; // test grades double ave; // final average void print_ave() { cout << "Name: " << name << endl; cout << "Average: " << ave << endl; } }; C++ Spring 2000 Structures

Using the member function doubleStudentRecord stu; … // set values in the structure stu.print_ave(); C++ Spring 2000 Structures