Structures and Classes CS 16: Solving Problems with Computers I Lecture #15
|
|
- Dominic McGee
- 6 years ago
- Views:
Transcription
1 Structures and Classes CS 16: Solving Problems with Computers I Lecture #15 Ziad Matni Dept. of Computer Science, UCSB
2 WHAT THE NEXT 3 WEEKS LOOK LIKE MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY 20- Nov 21- Nov 22- Nov 23- Nov 24- Nov Lab 8 issued Lecture: Dynamic Arrays, Makefiles 27- Nov 28- Nov 29- Nov 30- Nov 1- Dec Lab 9 issued Lecture: Structures and Classes Lab8 due Lecture: Linked Lists Hw 8 due Hw 9 issued 4- Dec 5- Dec 6- Dec 7- Dec 8- Dec Lab aiendance is opjonal Lecture: Recursion, Search/Sort Lab 9 due Lecture: Review for Final Exam Hw 9 due 11- Dec 12- Dec 13- Dec 14- Dec 15- Dec FINAL EXAM, 4-7 PM 11/29/17 Matni, CS16, Fa17 2
3 Structures (Ch. 10.1) Defining structures Member variables and funcgons Structures in funcgons Hierarchy in structures IniGalizing structures Lecture Outline Classes (Ch. 10.2) Defining member funcgons and the :: operator Public vs. Private members Constructors 11/29/17 Matni, CS16, Fa17 3
4 What Is a Class? A class is a data type whose variables are called objects Some pre- defined data types you have used are: int, char, double Some pre- defined classes you have used are: ifstream, string, vector You can also define your own classes as well 11/29/17 Matni, CS16, Fa17 4
5 Class DefiniGons To define a class, we need to Describe the kinds of values the variable can hold Numbers? Characters? Both? Something else? Describe the member funcjons What can we do with these values? We will start by defining structures as a first step toward defining classes 11/29/17 Matni, CS16, Fa17 5
6 STRUCTURES 11/29/17 Matni, CS16, Fa17 6
7 Structures A structure s use can be viewed as an object Let s say it does not contain any member funcgons (for now ) It does contain mulgple values of possibly different types We ll call these member variables 11/29/17 Matni, CS16, Fa17 7
8 Structures These mulgple values are logically related to one another and come together as a single item Examples: A bank CerGficate of Deposit (CD) which has the following values: a balance an interest rate a term (how many months to maturity) A student record which has the following values: the student s ID number the student s last name the student s first name the student s GPA What kind of values should these be?! What kind of values should these be?! 11/29/17 Matni, CS16, Fa17 8
9 The CD Structure Example: DefiniGon The CerGficate of Deposit structure can be defined as struct CDAccount { double balance; // a dollar amount double interest_rate; // a percentage int term; // a term amount in months } ; Remember this semicolon! Keyword struct begins a structure definigon CDAccount is the structure tag this is the structure s type Member names are iden1fiers declared in the braces 11/29/17 Matni, CS16, Fa17 9
10 Using the Structure Structure definigon should be placed outside any funcgon definigon Including outside of main( ) This makes the structure type available to all code that follows the structure definigon To declare two variables of type CDAccount: CDAccount my_account, your_account; my_account and your_account contain disgnct member variables balance, interest_rate, and term 11/29/17 Matni, CS16, Fa17 10
11 Specifying Member Variables Member variables are specific to the structure variable in which they are declared Syntax to specify a member variable (note the. ) Structure_Variable_Name. Member_Variable_Name Given the declaragon: CDAccount my_account, your_account; Use the dot operator to specify a member variable, e.g. my_account.balance my_account.interest_rate is a double is a double my_account.term is an int 11/29/17 Matni, CS16, Fa17 11
12 Note the struct definijon is placed before main() 11/29/17 Matni, CS16, Fa17 12
13 Note the declarajon of CDAccount Note the calculajons done with the structure s member variables 11/29/17 Matni, CS16, Fa17 13
14 Note that the structure is passed into the funcjon as call- by- reference. You can also pass a structure call- by- value. Note the use of the structure s member variables with an input stream. 11/29/17 Matni, CS16, Fa17 14
15 Duplicate Names Member variable names duplicated between structure types are not a problem struct FertilizerStock { double quantity; double nitrogen_content; }; FertilizerStock super_grow; struct CropYield { int quantity; double size; }; CropYield apples; This is because we have to use the dot operator super_grow.quanjty and apples.quanjty are different variables stored in different locagons in computer memory 11/29/17 Matni, CS16, Fa17 15
16 Structures as Return FuncGon Types Structures can also be the type of a value returned by a funcgon Example: CDAccount shrink_wrap (double the_balance, double the_rate, int the_term) { CDAccount temp; } temp.balance = the_balance; temp.interest_rate = the_rate; temp.term = the_term; return temp; What is this funcjon doing? 11/29/17 Matni, CS16, Fa17 16
17 Example: Using FuncGon shrink_wrap shrink_wrap builds a complete structure value in the structure temp, which is returned by the funcgon We can use shrink_wrap to give a variable of type CDAccount a value in this way: CDAccount new_account; new_account = shrink_wrap( , 5.1, 11); 11/29/17 Matni, CS16, Fa17 17
18 Assignment and Structures The assignment operator (=) can also be used to give values to structure types Using the CDAccount structure again for example: CDAccount my_account, your_account; my_account.balance = ; my_account.interest_rate = 5.1; my_account.term = 12; your_account = my_account; Note: This last line assigns all member variables in your_account the corresponding values in my_account 11/29/17 Matni, CS16, Fa17 18
19 Hierarchical Structures Structures can contain member variables that are also structures struct Date { int month; int day; int year; }; struct PersonInfo { double height; int weight; Date birthday; }; struct PersonInfo contains a Date structure 11/29/17 Matni, CS16, Fa17 19
20 Using PersonInfo An example on. operator use A variable of type PersonInfo is declared: PersonInfo person1; To display the birth year of person1, first access the birthday member of person1 cout << person1.birthday (wait! not complete yet!) But we want the year, so we now specify the year member of the birthday member cout << person1.birthday.year; 11/29/17 Matni, CS16, Fa17 20
21 IniGalizing Structures A structure can be inigalized when declared Example: struct Date { int month; int day; int year; }; Can be inigalized in this way watch for the order!: Date due_date = {4, 20, 2018}; Date birthday = {12, 25, 2000}; 11/29/17 Matni, CS16, Fa17 21
22 CLASSES 11/29/17 Matni, CS16, Fa17 22
23 Main Differences: structure vs class Classes in C++ evolved from the concept of structures in C Both classes and structures can have member variables Both classes and structures can have member funcjons, ALTHOUGH classes are made to be easier to use with member funcgons Classes may not be used when interfacing with C, because C does not have a concept of classes (only structures) 11/29/17 Matni, CS16, Fa17 23
24 Example of a Class: DayOfYear DefiniGon class DayOfYear { public: void output( ); int month; int day; }; Member FuncGon DeclaraJon Member Variables DeclaraJon public vs private senngs for members public means these members can be accessed by a program private means they are only for use by the class itself (e.g. test code) 11/29/17 Matni, CS16, Fa17 24
25 Defining a Member FuncGon Member funcgons are declared in the class declaragon Member funcgon defini1ons idengfy class in which the funcgon is a member Note the use of the :: in the following example Member funcgon defini>on syntax: Returned_Type Class_Name::Function_Name(Parameter_List) { Function Body Statements } 11/29/17 Matni, CS16, Fa17 25
26 Defining a Member FuncGon Member funcgon defini>on syntax: Returned_Type Class_Name::Function_Name(Parameter_List) { Function Body Statements } EXAMPLE: void DayOfYear::output() { cout << month = << month <<, day = << day << endl; } 11/29/17 Matni, CS16, Fa17 26
27 The :: Operator :: is called the scope resolu1on operator Indicates what class a member funcgon is a member of Example: void DayOfYear::output( ) indicates that funcgon output is a member of the DayOfYear class The class name that precedes :: is called a type qualifier 11/29/17 Matni, CS16, Fa17 27
28 :: Operator vs.. Operator :: is used with classes to idengfy a member void DayOfYear::output( ) { // function body }. is used with variables to idengfy a member DayOfYear birthday; birthday.output( ); 11/29/17 Matni, CS16, Fa17 28
29 Calling Member FuncGons Calling the DayOfYear member funcgon output: DayOfYear today, birthday; today.output( ); birthday.output( ); Note that today and birthday have their own versions of the month and day variables for use by the output funcgon Also, note how similar this is to other class member funcgons call- outs that we ve done, such as: string Name = Jimbo Jones ; int stlen = Name.length( ); 11/29/17 Matni, CS16, Fa17 29
30 Member Variables/FuncGons Private vs. Public C++ can help us by restricgng the program from directly referencing certain member variables Private members of a class can only be referenced within the definigons of member funcgons and NOT by outside users of the class If the program tries to access a private member, the compiler will give an error message Private is the default senng in classes 11/29/17 Matni, CS16, Fa17 30
31 Public Variables Public variables are the only ones that can be accessed directly by the main program If we want the program to be able to change a class variables values, then they must be declared as public 11/29/17 Matni, CS16, Fa17 31
32 Public or Private Members The keyword private idengfies the members of a class that can be accessed only by member func>ons of the class Members that follow the keyword private are called private members of the class The keyword public idengfies the members of a class that can be accessed from outside the class Members that follow the keyword public are called public members of the class 11/29/17 Matni, CS16, Fa17 32
33 Example class DayOfYear { public: void input(); void output(); private: void check_results(); int var1, var2; }; The member funcgons input( ) and output( ) are accessible from the main( ) or other funcgons. The member funcgon check_results() is strictly to be used internally in DayOfYear class workings, as are int variables var1 and var2. 11/29/17 Matni, CS16, Fa17 33
34 Example from the Textbook: Display 10.4 The program takes in user input on today s date and compares it to J.S. Bach s birthday (i.e. a specific date of 3/21) UGlizes a user- defined class called DayOfYear which holds a date and a month, but ALSO does funcgons like: Input date Check date against set birthday Outputs results 11/29/17 Matni, CS16, Fa17 34
35 The main( ) funcgon int main () { DayOfYear today, bach_birthday; cout << Enter today s date:\n ; today.input(); cout << Today s date is: ; today.output(); bach_birthday.set(3, 21); cout << Bach s Birthday is: ; bach_birthday.output(); if ((today.get_month() == bach_birthday.get_month()) && (today.get_day() == bach_birthday.get_day()) { cout << Happy Birthday, J.S. Bach!!!\n ; } return 0; } Note today & bach_birthday are both objects of the class DayOfYear.input() and.output() are member func>ons of DayOfYear class. Must be public b/c main() is using them..set() is a public member func>on too..get_month() and get_day() are public member func>ons too. What variable types do they look like they return? 11/29/17 Matni, CS16, Fa17
36 DayOfYear Class DefiniGon class DayOfYear { public: void input(); void output(); void set(int newmonth, int newday); int get_month(); int get_day(); private: void check_date(); int month, day; } Q: Why didn t we see the member func>on check_date( ) or the member variables month or day in the main( ) part of the program? A: They re private! 11/29/17 Matni, CS16, Fa17 36
37 void input() { Define All The Member FuncGons input() STOP!!! } 11/29/17 Matni, CS16, Fa17 37
38 Define All The Member FuncGons input() void DayOfYear::input() { cout << Enter the month as a number: ; cin >> month; cout << Enter the day of the month: ; cin >> day; } check_date(); Calling a member func>on! Is this a private or a public one? 11/29/17 Matni, CS16, Fa17 38
39 Define All The Member FuncGons output() void DayOfYear::output() { cout << Month is: ; cout << month << endl; cout << Day of the month is: ; cout << day << endl; } 11/29/17 Matni, CS16, Fa17 39
40 Define All The Member FuncGons set(), get_month() and get_day() void DayOfYear::set(int newmonth, int newday) { month = newmonth; day = newday; check_date(); } int DayOfYear::get_month() { return month; } int DayOfYear::get_day() { return day; } 11/29/17 Matni, CS16, Fa17 40
41 Define All The Member FuncGons check_date() void DayOfYear::check_date() { if ( (month < 1) (month > 12) (day < 1) (day > 31) ) { cout << Illegal date. Aborting program!\n ; exit(1); } } 11/29/17 Matni, CS16, Fa17 41
42 Punng It All Together Check Display 10.4 Example in Textbook for full program. class DayOfYear definigon main() Looks familiar? All the member funcgons of class DayOfYear Same approach with defining funcgons in C++ 11/29/17 Matni, CS16, Fa17 42
43 Using Private Variables It is a pracgce norm to make all member variables private Although, this is not strictly required Private variables require member funcgons to perform all changing and retrieving of values 11/29/17 Matni, CS16, Fa17 43
44 Using Private Variables It is a pracgce norm to make all member variables private FuncGons that allow you to obtain the values of member variables are called accessor funcgons. Example: get_day in class DayOfYear FuncGons that allow you to also change the values of member variables are called mutator funcgons. Example: set in class DayOfYear 11/29/17 Matni, CS16, Fa17 44
45 Review: Declaring an Object Once a class is defined, an object of the class is declared just as variables of any other type This is similar to when you declare a structure in C++ Example: To create two objects of type Bicycle: class Bicycle { // class definition lines };... Bicycle my_bike, your_bike; 11/29/17 Matni, CS16, Fa17 45
46 The Assignment Operator Objects and structures can be assigned values with the assignment operator (=) Example: DayOfYear due_date, tomorrow; tomorrow.set(11, 19); due_date = tomorrow; 11/29/17 Matni, CS16, Fa17 46
47 Review: Calling Public Members Recall that if calling a member funcgon from the main funcgon of a program, you must include the the object name: account1.update( ); Again, just like when we used member funcgons of pre- defined classes, like string 11/29/17 Matni, CS16, Fa17 47
48 Calling Private Members When a member func>on calls a private member funcgon, an object name is not used Example: if fraction (double percent); is a private member of the class BankAccount AND if fraction is called by another member funcgon called update void BankAccount::update( ) { balance = balance + fraction(interest_rate)* balance; } NOT: BankAccount::fraction(interest_rate)*balance; 11/29/17 Matni, CS16, Fa17 48
49 Constructors A constructor can be used to ini>alize member variables when an object is declared A constructor is a member func>on that is usually public and is automagcally called when an object of the class is declared RULE: A constructor s name must be the name of the class A constructor cannot return a value No return type, not even void, is used in declaring or defining a constructor 11/29/17 Matni, CS16, Fa17 49
50 YOUR TO- DOs q Lab 8 due TOMORROW (Wed. 11/29) by noon q HW 9 due Thu. 12/7 q Lab 9 due Wed. 12/6 by noon q Read Ch. 13 on Linked Lists for Thursday q Visit Prof s and TAs office hours if you need help! q Smile! And make people wonder why the heck you re smiling 11/29/17 Matni, CS16, Fa17 50
51 11/29/17 Matni, CS16, Fa17 51
Chapter 10 Defining Classes
Chapter 10 Defining Classes What Is a Class? A class is a data type whose variables are objects Some pre-defined data types you have used are int char You can define your own classes define your own types
More informationStarting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,
Classes Starting Savitch Chapter 10 l l A class is a data type whose variables are objects Some pre-defined classes in C++ include int, char, ifstream Of course, you can define your own classes too A class
More informationCh 6. Structures and Classes
2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;
More informationChapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo
Chapter 6 Structures and Classes 1 Learning Objectives Structures Structure types Structures as function arguments Initializing structures Classes Defining, member functions Public and private members
More informationSoftware Design and Analysis for Engineers
Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 1 Date:
More informationLinked Lists CS 16: Solving Problems with Computers I Lecture #16
Linked Lists CS 16: Solving Problems with Computers I Lecture #16 Ziad Matni Dept. of Computer Science, UCSB Material: Everything we ve done Homework, Labs, Lectures, Textbook Tuesday, 12/12 in this classroom
More informationSimplest version of DayOfYear
Reminder from last week: Simplest version of DayOfYear class DayOfYear { public: void output(); int month; int day; }; Like a struct with an added method All parts public Clients access month, day directly
More informationArrays 2 CS 16: Solving Problems with Computers I Lecture #12
Arrays 2 CS 16: Solving Problems with Computers I Lecture #12 Ziad Matni Dept. of Computer Science, UCSB Material: Post- Midterm #1 Lecture 7 thru 12 Homework, Labs, Lectures, Textbook Tuesday, 11/14 in
More informationProgramming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11
Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Thursday, 5/17 in this classroom Starts at 2:00 PM **SHARP** Please
More informationMore Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6
More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB
More informationMore on Arrays CS 16: Solving Problems with Computers I Lecture #13
More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday,
More informationDynamic Arrays Makefiles and Mul3ple File Compiles
Dynamic Arrays Makefiles and Mul3ple File Compiles CS 16: Solving Problems with Computers I Lecture #14 Ziad Matni Dept. of Computer Science, UCSB WHAT THE NEXT 3 WEEKS LOOK LIKE MONDAY TUESDAY WEDNESDAY
More informationChapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.
Chapter 10 Defiig Classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types 10.4 Itroductio to Iheritace Copyright 2015 Pearso Educatio,
More informationMore on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11
More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Announcements Heads- Up: Midterm #2 is NEXT Tuesday (11/14) Covers everything
More informationProgramming & Abstraction
Classes CMSC 202 Programming & Abstraction All programming languages provide some form of abstraction. Also called information hiding Separates code use from code implementation Procedural Programming
More informationDesign and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8
Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB Outline Midterm# 1 Grades Review of key concepts Loop design help Ch.
More informationCall-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5
Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS!
More informationVectors and Pointers CS 16: Solving Problems with Computers I Lecture #13
Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Midterm grades will be available on Tuesday, 11/21 If you *need* to know
More informationOverloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6
Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB A reminder about Labs Announcements Please make sure you READ
More information(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17
(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17 Ziad Matni Dept. of Computer Science, UCSB Administrative Homework situation: Labs: NO MORE HOMEWORK! J Lab10
More informationFunc%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5
Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5 Ziad Matni Dept. of Computer Science, UCSB NO more adds for this class Announcements If you want to switch labs, switch with SOMEONE
More informationNumerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8
Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB Announcements We are grading your midterms this week! Grades
More informationPIC 10A. Lecture 15: User Defined Classes
PIC 10A Lecture 15: User Defined Classes Intro to classes We have already had some practice with classes. Employee Time Point Line Recall that a class is like a souped up variable that can store data,
More informationDot and Scope Resolution Operator
Dot and Scope Resolution Operator Used to specify "of what thing" they are members Dot operator: Specifies member of particular object Scope resolution operator: Specifies what class the function definition
More informationFile I/O CS 16: Solving Problems with Computers I Lecture #9
File I/O CS 16: Solving Problems with Computers I Lecture #9 Ziad Matni Dept. of Computer Science, UCSB I/O Data Streams and File I/O Lecture Outline An introduction to Objects and Member Functions Handling
More informationCharacter Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10
Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline Useful character manipulators & functions
More informationMore on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8
More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #7 due today Lab #4 is due on Monday at 8:00
More informationMore Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4
More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM
More informationExercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15
Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15 Ziad Matni Dept. of Computer Science, UCSB The head of a List The box labeled head, in Display 13.1, is not a node, but
More informationClasses and Data Abstraction. Topic 5
Classes and Data Abstraction Topic 5 Classes Class User Defined Data Type Object Variable Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Object Variable Data
More informationDesign and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7
Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7 Ziad Matni Dept. of Computer Science, UCSB Announcements We are grading your midterms this week!
More informationFlow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4
Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #3 due today Homework #4 is assigned Lab #2
More informationSolving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3
Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB A Word About Registration for CS16 FOR THOSE OF YOU NOT YET REGISTERED:
More informationUsing the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018
Using the MIPS Calling Convention Recursive Functions in Assembly CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB Administrative Lab
More informationFile Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9
File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9 Ziad Matni Dept. of Computer Science, UCSB Midterm Exam grades out! Announcements If you want to see your exams, visit
More informationFunctions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11
Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Administrative Lab 5 due end of day tomorrow Midterm: to
More informationDesigning Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6
Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Lab #3
More informationCompiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3
Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB Compiling Programs in C++ Input and Output Streams Simple Flow
More informationAdvanced Flow Control CS 16: Solving Problems with Computers I Lecture #5
Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5 Ziad Matni Dept. of Computer Science, UCSB Announcements Demos done in class can be found at: hcp://www.cs.ucsb.edu/~zmatni/cs16s17/demos
More informationAbstract Data Types. Different Views of Data:
Abstract Data Types Representing information is fundamental to computer science. The primary purpose of most computer programs is not to perform calculations, but to store and efficiently retrieve information.
More informationCompiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7
Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7 Ziad Matni Dept. of Computer Science, UCSB Programming in Multiple Files The Magic of Makefiles!
More informationIntroduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2
Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB Administrative This class is currently FULL and
More informationLecture 7. Log into Linux New documents posted to course webpage
Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next
More informationClasses and Data Abstraction. Topic 5
Classes and Data Abstraction Topic 5 Introduction Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes The data and functions of a class
More informationFunction Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10
Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline More on MIPS Calling Convention Functions calling functions
More informationLecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1
Lecture 12 Log into Linux. Copy files on csserver in /home/hwang/cs215/lecture12/*.* Reminder: Practical Exam 1 is Wednesday 3pm-5pm in KC-267. Questions about Project 2 or Homework 6? Submission system
More informationCIS 190: C/C++ Programming. Classes in C++
CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationWelcome to Solving Problems with Computers I
Welcome to Solving Problems with Computers I CS 16: Solving Problems with Computers I Lecture #1 Ziad Matni Dept. of Computer Science, UCSB Image from agorolabs on slideshare.com A Word About Registration
More informationSoftware Design and Analysis for Engineers
Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:
More informationIntroduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12
Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12 Ziad Matni Dept. of Computer Science, UCSB Announcements MIDTERM #2 on THURSDAY Homework #11 due
More informationOperator overloading: extra examples
Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading
More informationPre- Defined Func-ons in C++ Review for Midterm #1
Pre- Defined Func-ons in C++ Review for Midterm #1 CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Homework #6 issued
More informationSpring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques
1 CPSC2620 Advanced Programming Spring 2003 Instructor: Dr. Shahadat Hossain 2 Today s Agenda Administrative Matters Course Information Introduction to Programming Techniques 3 Course Assessment Lectures:
More informationPhysics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu
Physics 2660: Fundamentals of Scientific Computing Lecture 7 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Reminder HW06 due Thursday 15 March electronically by noon HW grades are starting to appear!
More informationCall- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9
Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #8 due today
More informationCh 7. Constructors and Other Tools
2013-2 Ch 7. Constructors and Other Tools September 3, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;
More informationMore on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10
More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Announcements Heads- Up: Midterm #2 is on Tuesday 11/14 Found evidence that
More informationMIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8
MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8 Ziad Matni Dept. of Computer Science, UCSB CS 64, Spring 18, Midterm#1 Exam
More informationMultiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationAPCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
More informationAlgorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14
Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14 Ziad Matni Dept. of Computer Science, UCSB Administra:ve Turn in Homework #12 Homework #13 is due Tuesday Lab
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationFlow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4
low Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #3 due today Please take out any staples or paper clips Lab #2
More informationFriends and Overloaded Operators
Friend Function Friends and Overloaded Operators Class operations are typically implemented as member functions Some operations are better implemented as ordinary (nonmember) functions CSC 330 OO Software
More informationFinal Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Interfaces vs. Inheritance Abstract Classes Inner Classes Readings This Week: No new readings. Consolidate! (Reminder: Readings
More informationFriends and Overloaded Operators
Friends and Overloaded Operators Friend Function Class operations are typically implemented as member functions Some operations are better implemented as ordinary (nonmember) functions CSC 330 OO Software
More informationCS 106X, Lecture 14 Classes and Pointers
CS 106X, Lecture 14 Classes and Pointers reading: Programming Abstractions in C++, Chapter 6, 11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationObject-Oriented Programming
Object-Oriented Programming In C++ classes provide the functionality necessary to use object-oriented programming OOP is a particular way of organizing computer programs It doesn t allow you to do anything
More informationProgramming Assignment #2
Programming Assignment #2 Due: 11:59pm, Wednesday, Feb. 13th Objective: This assignment will provide further practice with classes and objects, and deepen the understanding of basic OO programming. Task:
More informationCS 106B, Lecture 1 Introduction to C++
CS 106B, Lecture 1 Introduction to C++ reading: Programming Abstractions in C++, Chapters 1 & 2 This document is copyright (C) Stanford Computer Science and Ashley Marty Stepp, Taylor, licensed under Creative
More informationAnatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation
Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:
More informationtypedef int Array[10]; String name; Array ages;
Morteza Noferesti The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; Length a, b, len ; Length
More informationReview. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2
Review Fall 2013 CISC2200 Yanjun Li 1 Outline Array Pointer Object-Oriented Programming Fall 2013 CISC2200 Yanjun Li 2 1 Array Arrays are data structures containing related data items of same type. An
More informationReview. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2
Review Fall 2017 CISC2200 Yanjun Li 1 Outline Array Pointer Object-Oriented Programming Fall 2017 CISC2200 Yanjun Li 2 1 Computer Fall 2017 CISC2200 Yanjun Li 3 Array Arrays are data structures containing
More informationPython Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13
Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB Administrative Hw07 out today DUE ON MONDAY 3/11 Lab07 will
More informationStrings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6
Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Administrative Turn in Homework #2 today Homework #3 is assigned and
More informationUniversity of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections
University of Maryland Baltimore County CMSC 202 Computer Science II Fall 2004 Mid-Term Exam Sections 0201 0206 Lecture Hours: Monday Wednesday 5:30 PM 6:45 PM Exam Date: Wednesday 10/20/2004 Exam Duration:
More informationAnnouncements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy
CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only
More informationCS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types
CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 8: Abstract Data Types Object- Oriented Programming Model the applica6on/so9ware as a set of objects
More informationWeek 4 EECS 183 MAXIM ALEKSA. maximal.io
Week 4 EECS 183 MAXIM ALEKSA maximal.io Agenda Functions Scope Conditions Boolean Expressions Lab 2 Project 2 Q&A Lectures 15% 36% 19% 8:30am 10:00am with Bill Arthur 10:00am 11:30am with Mary Lou Dorf
More informationLECTURE 11 STRUCTURED DATA
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 11 STRUCTURED DATA
More informationCS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4
CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4 Recap Functions Parameter passing: pass by reference Strings Letter to digit and vice-versa Library functions: string
More informationConstants, References
CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const
More information} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.
Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.
More informationChapter 11: Structured Data
Chapter 11: Structured Data 11.1 Abstract Data Types (ADT's) Abstract Data Types A data type that specifies values that can be stored attributes operations that can be done on the values behaviors User
More informationLecture 3. The syntax for accessing a struct member is
Lecture 3 Structures: Structures are typically used to group several data items together to form a single entity. It is a collection of variables used to group variables into a single record. Thus a structure
More informationAgenda: Notes on Chapter 3. Create a class with constructors and methods.
Bell Work 9/19/16: How would you call the default constructor for a class called BankAccount? Agenda: Notes on Chapter 3. Create a class with constructors and methods. Objectives: To become familiar with
More informationBasic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2
Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB Your Instructor Your instructor: Ziad Matni, Ph.D(zee-ahd
More informationCS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.
CS 31 Discussion 1A, Week 8 Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. Today s focus Pointer Structure Clarifications Pointer A pointer is the memory address of a variable.
More informationFile Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10
File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #9 due today Homework #10 is out Midterm #2 is on
More informationIntroduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2
Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB A Word About Registra>on for CS16 FOR THOSE OF YOU NOT YET REGISTERED: There s a LONG waitlist
More informationObject Oriented Design
Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use
More informationChapter 11: Structured Data
Chapter 11: Structured Data 11.1 Abstract Data Types Abstract Data Types A data type that specifies values that can be stored operations that can be done on the values User of an abstract data type does
More informationCS349/SE382 A1 C Programming Tutorial
CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005 Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop Comments
More informationA class is a user-defined type. It is composed of built-in types, other user-defined types and
Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.
More informationFile I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11
File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #10 due today Homework #11 is out Midterm #2 is on
More informationFile I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12
File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12 Ziad Matni Dept. of Computer Science, UCSB Administrative Homework #7 is DUE on MONDAY (3/12) Lab
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More informationCS 32. Lecture 2: objects good?
CS 32 Lecture 2: objects good? Double Vision This course has two main tracks Unix/shell stuff Object-Oriented Programming Basic C++ familiarity Off by one! Another Troika This class has three main texts
More information