Created by Hwansoo Han Edited by Ikjun Yeom

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
How Create a C++ Program. #include using namespace std; void main() { cout
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Intro to C/C++ #include using namespace std; int main() { cout
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
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. 
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
1 Writing a Good Program 8. Elementary Data Structure.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Dynamic memory allocation and Pointers Lecture 4.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
1 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Pointers by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
A Sample Program #include using namespace std; int main(void) { cout
Lecture 3: Getting Started & Input / Output (I/O)
Dynamic Allocation in C
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 13 Introduction to C++ Language
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Intro to ETEC Java.
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Basic Elements of C++.
An Introduction to C Programming
C++ in 90 minutes.
Introduction to C++ October 2, 2017.
C Basics.
Pointers and Dynamic Variables
Data Structures with C++
Basic Elements of C++ Chapter 2.
Pointers, Dynamic Data, and Reference Types
Given the code to the left:
Introduction to C++ Programming
Data Structures with C++
Arrays an array of 5 ints is filled with 3,2,4,1,7
Programs written in C and C++ can run on many different computers
Pointers, Dynamic Data, and Reference Types
SPL – PS1 Introduction to C++.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Created by Hwansoo Han Edited by Ikjun Yeom C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom

Why New Languages? Already know C++ C, Java, Python, Matlab, … Basically extension to C All C code compiled with C++ compilers But many libraries and “OO” concepts added

Many Use C++ Most recent open source projects Natural extension to C Written in C++ or Java Easier to understand – “OO” design Natural extension to C C and C++ can be easily mixed (with a little efforts)

Hello, World! /* * First program in C++ : hello world */ #include <iostream> using namespace std; // main function int main() { cout << “hello, world!” << endl; return 0; } console output Comment, header include, main, cout

C++ IDE Download Cygwin Download JRE 6.0 or above www.cygwin.org Set PATH to include g++ or gcc (제어판>시스템및보안>시스템>고급시스템설정>고급tab>환경변수) Download JRE 6.0 or above Google “JRE 64bit windows 7” E.g. Downloads.cnet.com Run the downloaded file to install JRE Download Eclipse CDT (C/C++ development tooling) www.eclipse.org/downloads Download “Eclipse IDE for C/C++ Developers” Unzip the downloaded file into a directory E.g. D:\bin\

Genealogy of Programming Languages

C++ vs. C vs. Java: Minor Differences Boolean type C: int myBooleanValue = 1; C++: bool myBooleanValue = true; Java: boolean myBooleanValue = true; Printing C: printf(“Hello!\n”); C++: cout << “Hello!” << endl; Java: println(“Hello!”);

C++ vs. C vs. Java: Minor Differences Calling other functions C/C++ : define before use Java : no such worries void PrintHello(); void PrintTime() { cout << “It’s time to go!” << endl; } int main() { PrintHello(); // OK – Prototype defined earlier PrintTime(); // OK – Function defined earlier SayHi(); // ERROR – no prototype earlier } void PrintHello() { cout << “Hello!” << endl; } void SayHi() { cout << “Hi!” << endl; }

C++ vs. C vs. Java: Major Differences C/C++ has no garbage collector Java: Integer myInt = new Integer(); myInt = NULL; // GC will delete C++ : programmers need to delete C : programmers need to free Objects Java : memory for all objects is put on the heap C++ : Objects can be placed either on the stack or the heap C : no objects, malloc gets the space from the heap

Program Images, Variables, Objects Address space Local variables : stack Global variables : data Dynamically allocated objects : heap code data heap stack kernel 0xC0000000 0xFFFFFFFF 0x080482d0 int a = 1; // ? int main() { int b = 1; // ? int *c; // ? c = (int*)malloc(sizeof(int)); // ? } a: data, b,c: stack, *c(malloc): heap

C++ vs. C vs. Java: Major Differences Stack vs. Heap allocation in C/C++ int main() { int a = 1; // STACK int b(2); // STACK int *c = new int(3); // HEAP cout << “a=“ << a << “ addr=“ << &a << endl; cout << “b=“ << b << “ addr=“ << &b << endl; cout << “c=“ << *c << “ addr=“ << c << endl; delete c; } int fn() { int *parray = new int [10]; // STACK // … delete [] parray; a: stack, b: stack, c: heap

C++ vs. C vs. Java: Major Differences C++ can choose to pass by value, pointer, and reference C can choose to pass by value or pointer Java cannot choose Primitives only pass by value Objects only pass by reference /* C */ void AddOne(int *x) { (*x)++; } int main() { int x = 7; AddOne(&x); printf(“%d\n”, x); // output 8 } /* C++ */ void AddOne(int &x) { x++; } int main() { int x = 7; AddOne(x); cout << x << endl; // output 8 } /* Java */ void AddOne(int x) { x++; } void run() { int x = 7; AddOne(x); println(x); // output 7 }

Coding Style – Managing Large Code Instead putting all into a single file, separate them for easier management Files with extension .cpp/.cc contains implementations of functions Files with extension .h contains function prototype .h files are used to overall shape of source code Other programmers can roughly recognize your programs through header files (.h files) If you don’t care the details on how a function works, you don’t have to look into .cpp files.

C++ vs. C vs. Java: Similarities Short circuit evaluation if (x < 4 || y == 3) // test the first (x<4), if true skip the second Integer division 9/4 ⇒ 2 Primitives char, short, int, long, float, double Type conversion 3/2.0 ⇒ 1.5 10 + 8.2 ⇒ 18.2 Functions, objects (C++/Java), arithmetic operators, …

First Assignment – Guess Number Print out “Welcome to guess game!” Declare a variable secret (0..100) Print out the range of secret value Set secret value with random() Loop until exit Get a guess from the user If the guess is equal to secret, exit Print if the guess is high or low Print out “Congratulations!” /* C++ random value */ #include <cstdlib> #include <ctime> int main() { srandom(time(NULL)); int x = random(); } /* C++ cin, cout */ int main() { int x = 0, inum; cin >> inum; /* IN */ x += inum; cout << x << endl; }

First Assignment – Guess Number DUE: midnight (23:59:59), Sep. 19 (Wed) Submit to ckjun1987@gmail.com Submit: student_id.tar includes guess.cpp, io.cpp, guess.h, Makefile, Readme.txt guess.cpp: #include “guess.h”, main(), check() io.cpp: getNum(), printCongrats() guess.h: declare types of getNum(), printCongrats();

Makefile - Example Comment Variable definition target: dependency_list List of targets # Makefile for hello, mem, and random CXXFLAGS = -O3 –Wall CXX = g++ PROGRAMS = hello mem random all: $(PROGRAMS) hello: hello.cpp $(CXX) $(CXXFLAGS) –o $@ $^ mem: mem.cpp random: random.cpp clean: rm –f $(PROGRAMS) @echo “--binaries are removed” target: dependency_list [TAB]command_list $@ : name of target $^ : dependecy_list First target is a default target > make > make all > make random > make clean