G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Similar documents
Introduction to Programming session 24

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Chapter 17: Linked Lists

C++ (Non for C Programmer) (BT307) 40 Hours

And Even More and More C++ Fundamentals of Computer Science

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Classes and Data Abstraction

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Classes: A Deeper Look

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

C++ Classes, Constructor & Object Oriented Programming

Chapter 10 Introduction to Classes

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

More C++ Classes. Systems Programming

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Fundamentals of Programming Session 24

COMP 2355 Introduction to Systems Programming

Chapter 17: Linked Lists

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Lecture 18 Tao Wang 1

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

57:017, Computers in Engineering C++ Classes

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Introduction to Classes

A Deeper Look at Classes

Intermediate Programming & Design (C++) Classes in C++

IS 0020 Program Design and Software Tools

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS201 Latest Solved MCQs

Chapter 9 Classes : A Deeper Look, Part 1

FORM 2 (Please put your name and form # on the scantron!!!!)

Constants, References

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Interview Questions of C++

CSCE 110 PROGRAMMING FUNDAMENTALS

clarity. In the first form, the access specifier public : is needed to {

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

SAURASHTRA UNIVERSITY

Chapter 1: Object-Oriented Programming Using C++

Pointers and Strings Prentice Hall, Inc. All rights reserved.

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

CS304 Object Oriented Programming Final Term

Topics. bool and string types input/output library functions comments memory allocation templates classes

Introduction to Programming Using Java (98-388)

Introduction Of Classes ( OOPS )

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator


Tokens, Expressions and Control Structures

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

Chapter 11. Abstract Data Types and Encapsulation Concepts

Object-Oriented Principles and Practice / C++

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

More C++ : Vectors, Classes, Inheritance, Templates

Homework #3 CS2255 Fall 2012

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

G52CPP C++ Programming Lecture 13

Procedural programming with C

CS 161 Exam II Winter 2018 FORM 1

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Overloading Operators

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Chapter 18 - C++ Operator Overloading

THINK LIKE CREATIVE PROBLEM SOLVING V. ANTON SPRAUL

Pointers. Developed By Ms. K.M.Sanghavi

Object Oriented Programming. Solved MCQs - Part 2

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CS 161 Intro to CS I. Finish Pointers/Start Recursion

EL2310 Scientific Programming

TDDE18 & 726G77. Functions

Object-Oriented Programming

Lecture 05 POINTERS 1

ADT Sorted List Operations

CMSC 341 Lecture 10 Binary Search Trees

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

Transcription:

G205 Fundamentals of Computer Engineering CLASS 3, Wed. Sept. 15 2004 Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Function Calls A function is called via its name Actual parameters are passed for the formal parameters An environment is created in memory that hosts parameters variables local to the function The environment is cancelled upon return 9/15/2004 2

C++ Basics: Recursion, 1 A function can call itself (except main) = RECURSION Recursion allows natural implementation of recursive definitions. For each n >= 0: n! = n*(n-1)! 0!=1!=1 int recfact(int n) { if (n<2) return 1; else return n*recfact(n-1); } 9/15/2004 3

C++ Basics: Recursion, 2 Alternative for ITERATIVE definition: n! = n*(n-1)* *2, n > 2, 0!=1!=1 int factorial(int n) { int fact=1; for (int c=n; c>1;c--) fact *= c; return fact; } 9/15/2004 4

Rec vs. Iter: Running Times Recursive factorial: T r (n) = c if n < 2 T r (n) = T r (n-1)+c if n > 1 (*) (*) is called a recurrence relation It is solved by repeated substitutions 9/15/2004 5

Repeated Substitutions Assume n > 2: T r (n) = T r (n-1)+c T r (n) = T r (n-1)+c = T r (n-2)+c+c = T r (n-3)+c+c+c = T r (n-3)+3c = = T r (n-k)+kc Termination: n-k=1 k=n-1 T r (n)=t r (n-k)+kc=t r (n-n+1)+(n-1)c= T r (1)+(n-1)c = c+(n-1)c = cn in O(n) 9/15/2004 6

Recursion vs. Iteration The running time T i (n) of the iterative factorial is in O(n) (induced by the for loop) factorial and recfact have the same running time, BUT factorial is based on a simple repetition structure recfact uses repeatedly the function call mechanism ( stack of environments ) Time and space consuming 9/15/2004 7

Other C++ Function Features Empty parameter list inline functions Parameter passing Pass-by-value ( int c, ) Pass-by-reference ( int &c, ) Default arguments Function overloading Function templates 9/15/2004 8

Arrays, 1 Data structures of related (same name) data items of the same type Static data structures (remain the same size) Elements of an array are referred to by their position (index): A[0 n] has n+1 items: A[0],A[1],,A[n] 9/15/2004 9

Arrays, 2 Declaration: type arrayname[ arraysize ] int c[12]; Passing arrays to functions InsertionSort(c,12); // function call Always passed by reference Multiple-Subscripted Arrays int M[row][col] void testfun( int N[][13] ); 9/15/2004 10

Pointers, 1 A pointer is a memory address A pointer variable contain a memory address as its value, a non-pointer variable contains a value Declaration: double *ptr; ptr is the address of a location that contains a value of type double 9/15/2004 11

Pointers, 2 Init: 0 or NULL or an address int *yptr = NULL; yptr points to nothing int y = 5; yptr = &y; Dereferencing operator *yptr is usable as y 9/15/2004 12

Structures, 1 Aggregate data types, from C struct Time { int hour; int minute; int second; }; Declaration: Time ts; // A variable of type Time Time tsa[15]; // An array of structures 9/15/2004 13

Structures, 2 Accessing structure members: Dot (.) operator: ts.hour; Arrow (->) operator: access via a pointer Time *timeptr = &ts; timeptr -> hour; (= (*timeptr).hour;) Defines a data type, not the operations Not the best for Abstract Data Types 9/15/2004 14

Abstract Data Types A type (set of values) whose internal form is hidden behind a set of access functions. Objects of the type are created and inspected only by calls to the access functions. This allows the implementation of the type to be changed without requiring any changes outside the module in which it is defined. 9/15/2004 15

Implementing ADTs with a CLASS Central concept to OOP: implements objects Data+Operations, implements ADTs Classes are user-defined types which define data AND the set of (all and only) functions that manipulates the data 9/15/2004 16

Class Interface class Time { public: Time(); void settime( int, int, int ); private: int hour; int minute; int second; }; 9/15/2004 17

Class Implementation Time::Time() { // Constructor hour = minute = second = 0; } void Time::setTime(int h, int m, int s) { hour = (h>=0 && h < 24)? h : 0; minute = (m>=0 && h < 60)? m : 0; second = (s>=0 && h < 60)? s : 0; } 9/15/2004 18

Class Use Legal and illegal operations: int main() { Time t; // Instantiate and initialize an object of class time t.settime(13,27,6); // Change time cout << t.hour; // Attempt to access private member } 9/15/2004 19

Other Class Features Access functions and utility functions User-defined constructors Default arguments Destructors Friend functions Const objects and const functions Operator overloading 9/15/2004 20

Assignments Deitel & Deitel book, chapters 5 and 6 Homework 2: Due in class 9/22/2004 Updated information on the class web page: www.ece.neu.edu/courses/eceg205/2004fa 9/15/2004 21