CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
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.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
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.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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 Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
1 C++ Classes and Data Structures Course link…..
Basic concepts of C++ Presented by Prof. Satyajit De
Test 2 Review Outline.
C++ Basic Input and Output (I/O)
MT262A Review.
Class and Objects UNIT II.
11 Chapter Structured Data
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Global & Local Identifiers
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
CS 1430: Programming in C++ No time to cover HiC.
Arrays Kingdom of Saudi Arabia
CS 1430: Programming in C++ No time to cover HiC.
Strings A collection of characters taken as a set:
Chapter 11: Structured Data.
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Namespaces How Shall I Name Thee?.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Standard Version of Starting Out with C++, 4th Edition
Class StudentList class StudentList { private: int numStudents;
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
CS150 Introduction to Computer Science 1
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

Student Data First Name Last Name ID GPA DOB Phone ... How to store student data in our programs?

Store Student Data in Variables // For one student at a time float gpa; string firstName, lastName, id; …

Store Students Data in Parallel Arrays const int MAX_SIZE = 30; // To keep data for all students Float gpas[MAX_SIZE]; string firstNames[MAX_SIZE], lastNames[MAX_SIZE], ids[MAX_SIZE]; . . .

C++ Class class Student { string id; string firstName, lastName; float gpa; }; // Student is a class (data type)! // By default, all fields are private. // Class Scope!

Create public functions (methods) to access private fields. C++ Class class Student { private: string id; string firstName, lastName; float gpa; public: . . . }; Create public functions (methods) to access private fields.

Class Methods class Student { private: string id; string firstName, lastName; float gpa; public: // Make sure the order is correct. void Read() cin >> id >> firstName >> lastName >> gpa; } };

Class Methods class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Print() cout << endl; cout << setw(9) << id << setw(20) << firstName << setw(20) << lastName << setw(5) << gpa; } };

Calling Class Methods Using the dot notation! Student s1; // Input data into object s1 s1.Read(); // Output data of object s1 s1.Print(); Using the dot notation!

Syntax and Style class Student { private: string id; string firstName, lastName; float gpa; public: void Read() . . . }; // class, private, public: key word // Student: Identifier, your choice // Fields : Declaring variables // Braces // Semicolon after } // Read, Print: class methods (function inside class) // Indentation

Semantics Student is a new data type! class Student { private: string id; string firstName, lastName; float gpa; public: void Read() cin >> id >> firstName >> lastName >> gpa; } . . . }; Student is a new data type! It has data fields and methods (functions) on the data.

Semantics class Student { private: string id; string firstName, lastName; float gpa; public: void Read() cin >> id >> firstName >> lastName >> gpa; } . . . }; Data fields have class scope and can be accessed from any class methods!

C++ Classes cin >> base; while ( !cin.eof() && (base < 2 || base > 9) ) { // display message cin.ignore(MAX_LINE_SIZE, ‘\n’); cin >> base: }

More Class Methods class Student { private: . . . public: string getFirstName() return firstName; } void setFirstName( string name ) firstName = name; };

More Class Methods class Student { private: . . . public: string getGPA() return gpa; } void setGPA( float value ) gpa = value; void updateGPA( float amount ) gpa += amount; };

Calling Class Methods // Comparing two students if ( s1.getGPA() > s2.getGPA() ) cout << “\nFirst student has higher GPA."; else if ( s1.getGPA() < s2.getGPA() ) cout << “\nSecond student has higher GPA."; else cout << “\nThe two student have the same GPA.";

Calling Class Methods // Updating students data s1.setGPA( 2.9 ); s1.updateGPA( 0.5 ); if ( s1.getGPA() > s2.getGPA() ) cout << “\nFirst student has higher GPA."; else if ( s1.getGPA() < s2.getGPA() ) cout << “\nSecond student has higher GPA."; else cout << “\nThe two student have the same GPA.";

class Student { . . . }; int main() Student s1, s2; s1.Read(); s2.Read(); s1.Print(); s2.Print(); // Comparing GPA of s1 and s2 s1.updateGPA( 0.5 ); return 0; }

//---------------------------------------- // Comment Block // Includes // constants class Student { . . . }; // Function prototypes int main() return 0; } // Function definitions

Schedule Quiz5-5: Due today Lab 7 Quiz7-1: Due Monday Test 2: 60 points Notes 11 - 22 Program 4 Due Thursday, Nov 5

Quiz2 Now