CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE 2. 141003.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
CS 240: Data Structures Tuesday, June 19 th ADT Requirements (Card), I/O.
ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between kloc.
C++ crash course Class 3 designing C++ programs, classes, control structures.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
ITEC 320 C++ Examples.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C++ Workshop Sujana Jyothi Dept. of Computer Science.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Lecture 7 : Intro. to STL (Standard Template Library)
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
System Programming Practical Session 7 C++ Memory Handling.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
11 Introduction to Object Oriented Programming (Continued) Cats.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Introduction to Object Oriented Programming Chapter 10.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
CSS342: Introduction1 Professor: Munehiro Fukuda.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
5.13 Recursion Recursive functions Functions that call themselves
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
C++ in 90 minutes.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Code::Block vs Visual C++
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Pointers and dynamic objects
ASEE / GradSWE C++ Workshop
CMSC 341 C++ and OOP.
Presentation transcript:

CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE

Today’s Agenda Collect topics / questions Encapsulation Present / code snippets.h files Arrays Vectors Building from command line Linux Box access / porting Pointers FileIO Constructor Invocation Timing In-class assignment: team roster

Encapsulation and Data public: contract exposes functions for class The What Actions Designed first Private: All data, helper functions Base types like int, double, … Arrays Vectors Linked Lists Queues Stacks Trees Graphs

example.h #ifndef EXAMPLE_H #define EXAMPLE_H Full.h file #endif

Arrays Arrays: reservation and construction of indexed set of objects or built-in types int x=5; int arr1[100] int arr2[3] = {34, 7, 34}; char cArr1[7]; char cArr2[10][5]; arr1[x] = 32; arr2[0] = arr1[x]; cArr2[3][3] = ‘a’; Arrays v Pointers (following are equivalent) void Foo(int arr[]) { } void Foo(int *arr) { } MyFooClass theFooObj; MyFooClass arrFoo[200]; //default constructors run MyFooClass *pFoo; pFoo = arrFoo; arrFoo[54] = theFooObj; pFoo = &theFooObj;

Array Example Write a function which reverses an integer array Write a function which prints out an array

void PrintArray(int [], int ); void ReverseArray(int *, int); int _tmain(int argc, _TCHAR* argv[]) { int arr1[6] = { 0, 1, 3, 6, 8, 9 }; PrintArray(arr1, 6); ReverseArray(arr1, 6); cout << endl << "Reversed: " << endl; PrintArray(arr1, 6); return 0; } void ReverseArray(int *array, int length) { for (int i = 0; i < length / 2; i++) { int temp = array[i]; array[i] = array[length i]; array[length i] = temp; } void PrintArray(int array[], int length) { for (int i = 0; i < length; i++) { cout << "array[" << i << "] = " << array[i] << endl; }

Vectors #include Using namespace std; { vector first; // empty vector of ints vector second (4,100); // four ints with value 100 vector third (second.begin(),second.end()); // iterating through second vector fourth (third); // a copy of third int myints[] = {16,2,77,29}; vector fifth (myints, myints + sizeof(myints) / sizeof(int) ); for (int i=0; i < second.size(); i++) { cout << second.at(2); cout << second[2]; } second.push_back(56); int temp = second.pop_back();

Vectors Excellent data structure choice Fast, safe-access, good memory characteristics Built on dynamic array Templatized so that vector vFoo(4);

Creating and running C++ from command line Visual studio command prompt window Create helloworld.cpp #include int main() { std::cout << "Hello World. It's Me!" << std::endl; return 0; } Cl helloworld.cpp Hellowworld.exe

Accessing Linux Lab and compiling Good Sites for Linux access: Install putty on your system Transfer files with filezilla: Compile and link all.cpp files (list as many.cpp files as you have for your program) then create an executable file called a.out: g++ file1.cpp file2.cpp Trick will in getting.h files correct

Build a stand-alone program on linux box

Let’s build Rational in Linux Lab

Computer Scientist of the week Marc Andreessen University of Illinois CU grad! Author of Mosaic Founder of NetScape Board of directors of Facebook, eBay, HP Venture Capitalist Facebook, foursquare, Twitter, Pinterest Skype

Pointers 1.Pointer variablesint *p, *q; 2.Int allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx

Pointer C++ Examples We’ll use a pointer.cpp file Compile with command line Bring into VS environment Play with pointers Why does this work: char* InitialInput = "Adam";

File IO In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; orFileInputStream infile = istream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew ObjectInputStream( infile ); fscanf( fp, “%d”, &i );inFile >> i;try { i = input.readInt( ); fclose(fp);inFile.close( );} catch ( EOFException e ) { }}input.close( ); } Note: for output, use ofstream.

int temp; ifstream inFile; string fileName = "c:\\tmp\\parts.dat"; inFile.open(fileName); inFile >> temp; cout << temp; cin >> temp;

Today’s In-class Assignment Implement a team roster class The members of a team (Players) have a first name, last name, and number

Today’s In-class Assignment Implement a team roster class The members of a team (Players) have a first name, last name, and number Use an Array as a data structure in roster Read in players from a file Players.txt (don’t worry about bad input) Tony Blair 14 Spiro Agnew 7 Clint Dempsey 6 …..

Today’s In-class Assignment Implement a team roster class The members of a team (Players) have a first name, last name, and number Use an Array as a data structure Read in players from a file Tony Blair 14 Spiro Agnew 7 Clint Dempsey 6 ….. overload << so that the roster is printed out

Today’s In-class Assignment Implement a team roster class The members of a team (Players) have a first name, last name, and number Use an Array as a data structure Read in players from a file Tony Blair 14 Spiro Agnew 7 Clint Dempsey 6 ….. overload << so that the roster is printed out in alphabetical order