A “User – Defined Data Type”

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
True or false A variable of type char can hold the value 301. ( F )
Chapter 11: Records (structs)
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
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.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
Input & Output: Console
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
1 CS161 Introduction to Computer Science Topic #15.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Bill Tucker Austin Community College COSC 1315
Pointers What is the data type of pointer variables?
Structured Data.
LESSON 06.
Classes and Data Abstraction
CO1401 Program Design and Implementation
Two-Dimensional Arrays Lesson xx
Chapter 11: Structured Data.
Chapter 5 Classes.
Global & Local Identifiers
Student Data Score First Name Last Name ID GPA DOB Phone ...
Structures Lesson xx In this module, we’ll introduce you to structures.
Returning Structures Lesson xx
C++ Functions, Classes, and Templates
CS 1430: Programming in C++.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Review for Final Exam.
Functions Pass By Value Pass by Reference
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
CPS120: Introduction to Computer Science
Review for Final Exam.
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Using string type variables
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

A “User – Defined Data Type” Creating Structures A “User – Defined Data Type”

Structure Concepts Structures use the reserved word struct Structures are defined just after using namespace std; and before function prototypes

Structure Concepts (cont’d) Structures package related data together Ex. Description, price, quantity might be combined to represent an item in a store Syntax:

Structure Concepts (cont’d) Remember – for all practical purposes a structure is a data type so the structure identifies can not be used as a variable. Variables of the structure data type have to be created Example - name a_name; item an_item;

Use of “dot notation” Dot notation is used to identify the data member to be accessed. Example - getline(cin, a_name.first); cout << “price: “<< an_item.price; Data members can be used just like standard variables of the same data type – tot_value = an_item.price * an_item.qoh;

Structure Declaration and Initialization To declare a variable of a structure type and initialize it – a data set has to be used. Braces are used to define the data set. Example - name a_name = {“XX”, “XX”, “XX”}; item block={“cheek”, 5, 45.50}; Let’s say the price of the block increased by 30%. We could now do something like – block.cost = block.cost * 1.3;

If we just sold 3 blocks we could – block.qoh = block.qoh – 3; Another example – We want to print out the data in a_name in the form of last name, first name then middle name – cout << a_name.last<<“, “<<a_name.first <<“ “<<a_name.mi<< endl; So we have packaged data together into a single data unit – a data structure.

Now the Huge Advantage Remember the structure variable is a SINGLE value that has several components that can be accessed. Remember that a function can return only a single value which is created (or modified) within the function

Assume the following function heading – name make_name( ) This function is going to return a single value of name type – remember that each name variable has 3 parts (refer to slide 3) packaged into a single name

So we might define the make_name function below – name make_name ( ) { name a_name; cout << “First name: “; getline(cin, a_name.first); cout << “Last name: “; getline(cin, a_name.last); cout << “Middle name: “; getline(cin, a_name.mi); return a_name; }//End of name make_name ( )