1 Chapter 1 C++ Basics Review Sections 1.4 and 1.5 and additional material from these slides See examples under Lec3.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Introduction to C++ An object-oriented language Unit - 01.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
CSE Lecture 10 – Functions
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Inheritance and Composition (aka containment) Textbook Chapter –
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Chapter 5 Functions.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Function Part II: Some ‘advanced’ concepts on functions.
Programming Functions: Passing Parameters by Reference.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 12 Classes and Abstraction
C++ Programming: Presentation 1
Introduction to C++ (Extensions to C)
Chapter 7: User-Defined Functions II
Topic Pre-processor cout To output a message.
Chapter 1 C++ Basics Review
Andy Wang Object Oriented Programming in C++ COP 3330
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 2 part #1 C++ Program Structure
Compilation and Debugging
Compilation and Debugging
Chapter Structured Types, Data Abstraction and Classes
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Separate Compilation and Namespaces
Name: Rubaisha Rajpoot
Starting Out with C++: From Control Structures through Objects
Chapter 3: Input/Output
Code Organization CSCE 121 J. Michael Moore.
Control Structures Part 3
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS 144 Advanced C++ Programming February 21 Class Meeting
Data Structures and ADTs
Presentation transcript:

1 Chapter 1 C++ Basics Review Sections 1.4 and 1.5 and additional material from these slides See examples under Lec3

2 Interface Vs. Implementation Interface typically defined in.h files –#included in.cpp file Preprocessor commands –Guards against multiple inclusion of.h files Interface

3 Interface Vs. Implementation (contd.) Scoping operator –To identify the class corresponding to each function Remember –Function signatures must match in both interface and implementation –Default parameters are specified only in the interface Implementation

4 Problems without Information Hiding Change in implementation affects the user of a class –What will happen if the name ‘OtherData’ is changed to ‘Info’? Debugging is difficult –Where will you place breakpoints to figure out when results went wrong? class NHRecord { public: int SocialSecurityNumber; int OtherData; NHRecord(int SSN, int Data); }; #include "nhrecord.h" NHRecord::NHRecord(int SSN, int Data) { SocialSecurityNumber = SSN; OtherData = Data; } #include #include "nhrecord.h" using namespace std; int main() { NHRecord nhr( , 1); cout << "InitialInfo " << nhr.OtherData << '\n'; nhr.OtherData = 2; cout << "UpdatedInfo " << nhr.OtherData << '\n'; }

5 Information Hiding and Accessors Get/Set with private data provide access while overcoming the limitations mentioned above –The user is not affected if the name ‘OtherData’ is changed to ‘Info’. –Breakpoints on the accessors help with debugging class HRecord { int SocialSecurityNumber; int OtherData; public: HRecord(int SSN, int Data); void SetData(int Data); int GetData(); }; void HRecord::SetData(int Data) {OtherData = Data;} int HRecord::GetData() { return OtherData;} int main() { HRecord hr( , 1); cout << "InitialInfo " << hr.GetData() << '\n'; hr.SetData(2); cout << "UpdatedInfo " << hr.GetData() << '\n'; return 0; }

6 vector and string in C++ STL Replace built-in C++ arrays and strings, respectively –Built-in arrays/string do not act as proper C++ objects Standard vector class –Gives a size() function –Can be assigned using = Standard string class –Compared with ==, <, etc. –Can be assigned using = –Gives length() function Avoid C++ built-in arrays and strings –Instead, use vector and string classes –Exception: code optimized for speed

7 Common Errors with Pointers Many of these errors may not be detected when you run your program, which make them particularly dangerous int *i; *i = 1; // Wrong. No memory has been assigned to i. i = new int; *i = 1; // OK delete i; if(i != 0) cout << *i << "\n\n"; // Wrong. i is still non-zero, but memory it points to has been deallocated. i = 0; // It is safer to assign 0 after deallocation. cout << *i; // Wrong. Dereferencing the null pointer. if(i != 0) cout << *i; // Not executed i = new int[10]; delete [] i; // Do not use delete i;

8 More Errors with Pointers int i1=1, i2=2; swap(i1, i2); cout << i1 << " " << i2 << "\n\n"; swap(&i1, &i2); cout << i1 << " " << i2 << "\n\n"; i1 = 1; i2 = 2; swapr(i1, i2); cout << i1 << " " << i2 << "\n\n"; void swap(int t1, int t2) { int t = t1; t1 = t2; t2 = t; } void swap(int *t1, int *t2) { int t = *t1; *t1 = *t2; *t2 = t; } void swapr(int &t1, int &t2) { int t = t1; t1 = t2; t2 = t; }

9 Don’t Return Pointers to Temporary Locations i = BadReturn(); // Call to a dangerous function cout << *i << "\n\n"; // Wrong. Dereferencing an invalid pointer. i = OKReturn(); cout << *i << "\n\n"; delete i; i = 0; int *BadReturn() // Dangerous error. { int Answer = 2; return &Answer; } int *OKReturn() { int *Answer; Answer = new int; *Answer = 2; return Answer; }

10 Shallow Copy Shallow copy can have unexpected consequences with dynamically allocated data class Node { public: char *name; int age; Node(char *n, int a) { name = new char[strlen(n) + 1]; strcpy(name, n); age = a; } ~Node() { cout << "Deleting " << (void *) name << "\n"; delete [] name; } }; Node n1("Gates", 50), *n2 = new Node(n1); cout name age << "\n"; strcpy(n2->name, "Bill"); n2->age = 55; cout name age << "\n"; delete n2; // The destructor frees name, which is common to n1 and to n2 Output Gates 50 Bill 50 Bill 55 Deleting 0x191d0010

11 Deep Copy Node(const Node &n) { cout << "In copy constructor\n"; name = new char[strlen(n.name)+1]; strcpy(name, n.name); age = n.age; } Node &operator=(const Node &n){ cout << "In assignment\n"; if(this != &n) { if(name) delete [] name; name = new char[strlen(n.name)+1]; strcpy(name, n.name); age = n.age; } return *this; } Node n1("Gates", 50), *n2 = new Node(n1); cout name age << "\n"; strcpy(n2->name, "Bill"); n2->age = 55; cout name age << "\n"; delete n2; // The destructor frees name, which is common to n1 and to n2 Output In copy constructor Gates 50 Gates 50 Bill 55 Deleting 0x4f87050 Deleting 0x4f87010