Structure TDate struct TDate { int year, month, day; }; // Define a new data type.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Computer Science 1620 Structures. Suppose I wish to store information about a student: name:string student ID:integer gpa:double.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
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.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Edited from Powerpoint Slides provided by Thomson Learning
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
CS 1430: Programming in C++.
Data Types Storage Size Domain of all possible values Operations 1.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Functions & Strings CIS Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
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.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
REVISION CSC 138 – Structured Programming One Dimensional Array
CO1401 Program Design and Implementation
Structures - Part II aggregate operations arrays of type struct
Data Types – Structures
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Data Types – Structures
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
Strings A collection of characters taken as a set:
CS 1430: Programming in C++.
CS148 Introduction to Programming II
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Introduction to Programming
Introduction to Programming
Function Overloading.
EECE.2160 ECE Application Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Structure (i.e. struct) An structure creates a user defined data type
CSCE 206 Lab Structured Programming in C
Selection Sorting S[] : array of int or float
Presentation transcript:

Structure TDate struct TDate { int year, month, day; }; // Define a new data type

// // The function inputs data to a TDate struct // The input is in the format: mm/dd/yyyy. // Parameter: ( out ) // void ReadDate(TDate& someDate) { char slash; cin >> someDate.month >> slash >> someDate.day >> slash >> someDate.year; } // Why using slash? // Input format: 11/21/2006 struct TDate { int year, month, day; };

// // The function displays a TDate struct // in the format mm/dd/yyyy. // Parameter: (In) // void WriteDate(const TDate& date) { cout << date.month << ‘/’ << date.day << ‘/’ << date.year; return; } void WriteDate(const TDate& date) { char slash = ‘/’; cout << date.month << slash << date.day << slash << date.year; return; }

Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; };

Accessing Members of Nested Structures StudentType s1; cin >> s1.TDate; // Valid? cin >> s1.DOB; // Valid? cin >> s1.DOB.day; // Valid? cin >> StudentType.DOB.day; // Valid?

// // The function inputs data to a StudentType struct // in the order: id, firstName, lastName, gpa, // DOB (month, day, year) and returns the struct. // Parameter: () // StudentType GetStudent() { StudentType s; cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; cin >> s.DOB.month >> s.DOB.day >> s.DOB.year; return s; }

// // The function inputs data to a StudentType // struct in the order: id, firstName, // lastName, gpa, DOB (month, day, year). // Parameter: (out) // void GetStudent(StudentType& s) { cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; cin >> s.DOB.month >> s.DOB.day >> s.DOB.year; return; }

// // The function inputs data to a StudentType struct // in the order: id, firstName, lastName, gpa, // DOB (using function). // void ReadDate(TDate& someDate) // Parameter: (out) // void GetStudent(StudentType& s) { cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; ReadDate(s); // Valid? ReadDate(DOB); // Valid? ReadDate(s.TDate); // Valid? ReadDate(s.DOB); // Valid? return; }

Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType { int numStudents; StudentType students[30]; };

Accessing Memebrs of Nested Structures SectionType cs143; InputSection(cs143); // Display the birth year of the last student cout << students[numStudents].TDate.year; // Valid? cout << cs143.students[numStudents - 1].TDate.year; // Valid? cout << cs143.students[cs143.numStudents - 1].TDate.year; // Valid? cout << cs143.students[cs143.numStudents - 1].DOB.year; // Valid?

Accessing Memebrs of Nested Structures SectionType cs143; InputSection(cs143); // Display the DOB of the first student // void WriteDate(const TDate& date) WriteDate(cs143.students[0]); // Valid? WriteDate(cs143.students[0].TDate); // Valid? WriteDate(cs143.students[0].DOB); // Valid?

Function Prototypes // Functions on TDate void ReadDate(TDate& someDate); void WriteDate(const TDate& date); // Functions on StudentType StudentType GetStudent() void DisplayStudent(const StudentType& s); void ModifyGPA(StudentType& s); // Functions on SectionType void InputSection(SectionType& sec); int IndexMaxGPA(const SectionType& sec); int IndexOfID(const SectionType& sec, string theID);