Starting to think about objects...

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.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
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.
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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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 in C++.
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
C Programming Lecture-13 Structures
CPS120: Introduction to Computer Science
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
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:

Starting to think about objects... C++ Structures Starting to think about objects... C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Structure A Structure is a container, it can hold a bunch of things. These things can be of any type. Structures are used to organize related data (variables) in to a nice neat package. C++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

Example Structure Definition struct StudentRecord { char *name; // student name double hw[3]; // homework grades double test[2]; // test grades double ave; // final average }; C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Structure Assignment You can use structures just like variables: StudentRecord s1,s2; s1.name = "Joe Student"; … s2 = s1; Copies the entire structure C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

Using the member function doubleStudentRecord stu; … // set values in the structure stu.print_ave(); C++ winter2008(by:J.Razjouyan)