1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
C++ Programming Languages
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
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,
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
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.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Memory Management in Java Mr. Gerb Computer Science 4.
FASTFAST All rights reserved © MEP Make programming fun again.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 2 Objects and Classes
Constructors and Destructors
Introduction to Computers Computer Generations
Programming with ANSI C ++
C Basics.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Java Programming Language
CSE 303 Concepts and Tools for Software Development
Constructors and Destructors
Classes, Constructors, etc., in C++
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

C and C++ C evolved from BCPL and B. BCPL was developed by Martin Richards in Ken Thompson based B on BCPL and used it to write very early versions of UNIX. These were "typeless" languages. All data occupied same amount of memory, and programmer had to deal with data type differences. C developed in 1972 by Dennis Ritchie at Bell Lab. C++ developed by Bjarne Stroustrup in ealry 1980s at Bell Labs to support OO programming

Areas C/C++ differ from Java® Not all code in objects header files preprocessor Call by reference allowed Pointers and pointer arithmetic Array bounds checking Garbage collection, destructors, memory leaks Templates (parameterized data types) Automatic instances of objects within a scope implicit type conversion operator overloading multiple inheritance virtual and non virtual methods

C(++) programs without classes

Call by reference example

Call with pointer example

Pointer arithmetic

Pointer arithmetic, p.2

Bracket Program (C, C++)

Main() for bracket

Passing arguments: value, reference

Point Class

Point Class, cont.

Point Program Example

Copy constructors in C++ In C++, objects are passed as copies in call by value By default, objects are copied "bitwise" Objects that allocate memory in their constructors (arrays, other objects) need a special constructor that is equivalent of the clone( ) method in Java® Copy constructor takes reference to object, and returns a new object of same class that is an independent copy. Copy constructors often get invoked implicitly. For example, a method that returns a new object will implicitly invoke copy constructor if one exists.

Why copy constructors? Object of class A, when constructed, includes an array. In a method, we declare a newObj is a local variable, so its destructor method will be called when method returns. Memory allocated by constructor will be returned to memory pool unless there is a copy constructor for class A that allocated new memory.

An Exquisite Point Class

An Exquisite Point Class, p.2

An Exquisite Point Class, p.3

Using the Point Class

Constructors and Destructors Dynamic memory allocation

Constructors and Destructors, p.2

Constructors and Destructors, p.3 No memory management in main program orfunctions, as a goal in C++ –In C, memory was managed for each variable You had to remember to allocate it and free it when done, no matter where these events occurred. Dynamic memory errors are over 50% of C program errors –In C++, we build memory management into classes New only in constructors; delete only in destructors Application developer sees nearly automatic garbagecollection. She never uses new; creates new objects just by defining them: Student Joe, same as int i Class developer has control of garbage collection when needed –C++ garbage collection is tough on lists, trees, etc.

Memory Management Errors Deleting same memory twice is a bug –Hard to find! –Trick: Comment out all deletes if you have a weird bug in a program with dynamic memo If bug goes away, youre deleting some memory twice Not deleting memory when done is a leak –Leaking 100 bytes per call on a Web server with a million calls a day means buying GBs of memory and crashing regularly anyway

Stack Template Class

Main Program, Stack Template

Inheritance: Access Specifiers

Research project revisited

Undergrad

Research project class

Research project class, p.2

Research project class C++ will dynamically set Student pointers to Undergrad, Grad, SpecGrad to get the pay for each If we stored Student objects in StaffList instead of pointers, we would only have the base Student class data!

Main program

Other Items Exceptions essentially same as Java® –Try, throw, catch Standard template library (STL) with Vectors, Lists, Stacks, similar to Java® –Cannot inherit from STL classes Multiple inheritance supported C has two string types (with conversions) –Null terminated arrays (old) –String class Arrays are just a memory location (reference) –Array size is passed as separate argument –Very problematic (buffer overruns) Recursion supported as in Java®