Download presentation
Presentation is loading. Please wait.
Published byΑδελφά Λύκος Modified over 5 years ago
1
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 3: Structures in C++
2
Announcements/reminders
Program 1 due date TBD (within 2 weeks or so) All programs to be submitted via Blackboard Good C++ reference site: Basic tutorials + in-depth reference pages Relevant tutorials under “Reading” on schedule page Shubham Tikare assigned as grader for course Will announce his office hours ASAP Poll to be posted to schedule Exam 1 & 2 Final exam time set: Wednesday, 5/8, 3-6 PM 6/6/2019 Data Structures: Lecture 3
3
Data Structures: Lecture 3
Lecture outline Review Basic C++ program structure I/O in C++ Structs in C++ Working with multiple files 6/6/2019 Data Structures: Lecture 3
4
Review: basic program structure
Covered basic C++ program structure—very similar to C! #include directive: add libraries to program <iostream>: basic input/output Namespaces Introduced std namespace—includes cin, cout, etc. Could include entire namespace: using namespace std; Or, just include members being used: using std::cout; Using statements directly after relevant #include 6/6/2019 Data Structures: Lecture 3
5
Review: Hello World! in C++
#include <iostream> // C++ input/output library using namespace std; int main() { cout << "Hello World!\n"; return 0; } 6/6/2019 Data Structures: Lecture 3
6
Data Structures: Lecture 3
Review: Basic I/O Output (cout) streams Can output multiple values in same statement cout << "x= " << x << ", y=" << y << endl; Recall endl ≈ ’\n’ + flush output stream Input (cin) streams Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Can cause problems if input doesn’t match variable type 6/6/2019 Data Structures: Lecture 3
7
Data Structures: Lecture 3
Review: File I/O File stream objects Must include <fstream> ifstream (input) or ofstream (output) Have methods for file open/close File I/O syntax similar to standard I/O Replace cin/cout with appropriate stream object Same operators used: << >> 6/6/2019 Data Structures: Lecture 3
8
Data Structures: Lecture 3
File I/O example #include <fstream> #include <iostream> using std::ifstream; using std::ofstream; int main() { int x, y; ifstream infile; ofstream outfile; // Open input, output files infile.open("f1.txt"); outfile.open("f2.txt"); // Read integer values, then reprint to output infile >> x >> y; outfile << "x = " << x << ", y = " << y << endl; return 0; } 6/6/2019 Data Structures: Lecture 3
9
Review: Structures in C
User-defined types; example: typedef struct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; Can define variables of that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; Access members using Dot operator: student1.middle = ‘J’; Arrow (if pointers): sPtr->GPA = 3.5; Typically passed to functions by address 6/6/2019 Data Structures: Lecture 3
10
Data Structures: Lecture 3
Structures in C++ 99% the same as in C Only (visible) difference: typedef isn’t necessary Could rewrite previous structure definition as: struct StudentInfo { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; }; Declarations shown in previous slide still work You can use StudentInfo as type name In C, type would be struct StudentInfo 6/6/2019 Data Structures: Lecture 3
11
Data Structures: Lecture 3
Structure example #include <iostream> using namespace std; struct Point { double x; double y; }; int main() { Point p1 = {3, 5}; Point p2 = {7.8, 9.1}; cout << "p1: (" << p1.x << ", " << p1.y << ")\n"; cout << "p2: (" << p2.x << ", " << p2.y << ")\n"; // Assume user enters -1 1 cout << "Enter new x, y for p1: "; cin >> p1.x >> p1.y; p2 = p1; p2.x -= 1.2; p1.y *= 2; cout << "p1: (" << p1.x << ", " << p1.y << ")\n"; cout << "p2: (" << p2.x << ", " << p2.y << ")\n"; return 0; } 6/6/2019 Data Structures: Lecture 3
12
Example solution (user input underlined)
Enter new x, y for p1: -1 1 p1: (-1, 2) p2: (-2.2, 1) 6/6/2019 Data Structures: Lecture 3
13
Data Structures: Lecture 3
Nested structures Structures can contain other structures: struct Name { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name }; struct SINew { Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point Will need multiple dot operators to access field within nested structure Given SINew s1; s1.sname Name structure within s1 s1.sname.middle middle initial of name within s1 6/6/2019 Data Structures: Lecture 3
14
Typical file usage with structures
Each structure has its own .h/.cpp files .h file (i.e., Name.h, SINew.h) contains structure definition and prototypes of related functions .cpp file (i.e., Name.cpp, SINew.cpp) contains function definitions To use struct type and functions, include .h file SINew.h would include Name.h Allows Name variable inside SINew structures Main program could include both … … although that’s redundant—including SINew.h implicitly includes Name.h Will organize classes similarly 6/6/2019 Data Structures: Lecture 3
15
Avoiding multiple inclusion of .h files
Say we have two files that start as shown: file1.h: #include "file2.h" ... myprogram.cpp: #include "file1.h" What’s the problem? What does an #include directive really do? 6/6/2019 Data Structures: Lecture 3
16
Avoiding multiple inclusion of .h files
Anything in file2.h is included twice #include directive copies/pastes contents of file Compiler will think program redefines structures, function prototypes, etc. Solution: conditional compilation Header files include header guard code to ensure they’re only compiled once Example code for fictional file2.h: #ifndef file2_h // Start of file #define file2_h ... #endif // End of file Code between #ifndef & #endif only compiled once 6/6/2019 Data Structures: Lecture 3
17
Data Structures: Lecture 3
Final notes Next time: more on going from C to C++ Functions Argument passing in C++ (Maybe) I/O manipulators for output formatting Discussion of Program 1 requirements Reminders: Program 1 due date TBD (within 2 weeks or so) All programs to be submitted via Blackboard 6/6/2019 Data Structures: Lecture 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.