From C to C++ Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Chapter 18 Vectors and Arrays
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Dynamic memory allocation
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
GPU programming: CUDA Acknowledgement: the lecture materials are based on the materials in NVIDIA teaching center CUDA course materials, including materials.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
C Programming - Lecture 5
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Standard Template Library Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Shallow Copy Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
Computer Organization and Assembly Language C/C++ Pointers and Memory Allocation.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
OOP Languages: Java vs C++
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Singly Linked Lists Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University 1.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
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.
POINTERS. // Chapter 3 - Program 1 - POINTERS.CPP #include int main() { int *pt_int; float *pt_float; int pig = 7, dog = 27; float x = , y = 32.14;
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
From C to C++ Jyh-Shing Roger Jang (張智星)
Dynamic Storage Allocation
Introduction to Computers Computer Generations
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers & Dynamic Memory
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
The Standard Template Library
Dynamic Memory Management
Queues Jyh-Shing Roger Jang (張智星)
The vector Container Type
Dynamic Memory Management
Object and its encapsulation
Data Management First we check system can handle bandwidths
Storing Game Entries in an Array
Presentation transcript:

From C to C++ Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

2 Enhancement of C++ Some features of C++ that are not part of C In-line function substitution Reference types Parametric polymorphism through templates Exception handling Default function arguments Object-oriented programming … C++ shares C’s ability to deal efficiently with hardware at the level of bits, bytes, words, addresses, etc.  C++ has grown to be a complex programming language! It takes years to be a savvy C/C++ programmer!

3 Our First Few C++ Programs First several examples of C++ programs of this course Let’s check out some examples… More examples via Google Google “c++ example vector unique” Choices of compilers Unix/Linux: g++ Windows: Dev C++ (g++.exe), MS Visual Studio (cl.exe) Examples are very important!

4 Pointers and Arrays Do you know pointers in C? Very dangerous! (But convenient!) What could go wrong? Confusing grammar int* x, y, z;  int *x, y, z; Delete memory that has not been allocated Check the availability of memory Memory leak (see next page) Out-of-bound indexing for arrays (example)example For efficiency, C/C++ does not perform boundary checking! Use STL vectors and vec.at() for safe (but inefficient) access.

5 Memory Leak Definition of memory leak A memory leak occurs when you call “new” without calling a corresponding “delete” later. How to avoid it? C++: “new” and “delete” must appear in pairs! C: “malloc” (or “calloc”, or “realloc”) and “free” must appear in pairs! Example Sometimes it’s not so obvious! (See “shallow copy”.) Quiz! More examples!

6 Online Tutorials for C++ There are quite a few good online tutorials on C++ C++ tutorial at C++ tutorial at C++ tutorial at C++ tutorial at Do you think you know C/C++ already? Deep C