C++ Tutorial. Let s get to coding. W. Deconinck 1 V. Gray 2. Department of Physics College of William & Mary

Similar documents
An Incomplete C++ Primer. University of Wyoming MA 5310

Tutorial Guide to the IS Unix Service

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Lab 1 Beginning C Program

Linux Overview. Local facilities. Linux commands. The vi (gvim) editor

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Appendix K Introduction to Microsoft Visual C++ 6.0

University of Toronto

CPSC 226 Lab Nine Fall 2015

Running your first Linux Program

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Command Line - Part 1

C++ INTERVIEW QUESTIONS

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

13 Classes & Objects with Constructors/Destructors

Crash Course in Java

C++FA 5.1 PRACTICE MID-TERM EXAM

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS 103 Lab Linux and Virtual Machines

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

Introduction to Java

Introduction to Running Hadoop on the High Performance Clusters at the Center for Computational Research

Sources: On the Web: Slides will be available on:

CISC 181 Project 3 Designing Classes for Bank Accounts

Introduction to Linux and Cluster Basics for the CCR General Computing Cluster

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Object Oriented Software Design II

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

FEEG Applied Programming 6 - Working Remotely on Linux Server

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

C++ Programming Language

CP Lab 2: Writing programs for simple arithmetic problems

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

CISE Research Infrastructure: Mid-Scale Infrastructure - NSFCloud (CRI: NSFCloud)

The C Programming Language course syllabus associate level

Introduction to R and UNIX Working with microarray data in a multi-user environment

Java Crash Course Part I

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

PHP Tutorial From beginner to master

Visual Studio 2008 Express Editions

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Moving from CS 61A Scheme to CS 61B Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Linux Connection Guide. by Tristan Findley

C++ Language Tutorial

NLP Programming Tutorial 0 - Programming Basics

- Hour 1 - Introducing Visual C++ 5

Chapter One Introduction to Programming

Unix Sampler. PEOPLE whoami id who

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Java (12 Weeks) Introduction to Java Programming Language

Tutorial 0A Programming on the command line

Appendix M: Introduction to Microsoft Visual C Express Edition

Compiler Construction

Agenda. Using HPC Wales 2

Command-Line Operations : The Shell. Don't fear the command line...

Personal Portfolios on Blackboard

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Introduction to Mac OS X

Unix Guide. Logo Reproduction. School of Computing & Information Systems. Colours red and black on white backgroun

Creating a Simple Visual C++ Program

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Sequential Program Execution

Using Microsoft SQL Server A Brief Help Sheet for CMPT 354

Overview. Lecture 1: an introduction to CUDA. Hardware view. Hardware view. hardware view software view CUDA programming

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

1. The First Visual C++ Program

How to Install Multicraft on a VPS or Dedicated Server (Ubuntu bit)

Answers to Review Questions Chapter 7

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

CSCE 665: Lab Basics. Virtual Machine. Guofei Gu

CS170 Lab 11 Abstract Data Types & Objects


Moving from C++ to VBA

Comp151. Definitions & Declarations

UltraLite C and C++ Programming

Instructions for Accessing the Advanced Computing Facility Supercomputing Cluster at the University of Kansas

Introduction to Programming System Design. CSCI 455x (4 Units)

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Introduction to Running Computations on the High Performance Clusters at the Center for Computational Research

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

C++ Overloading, Constructors, Assignment operator

Using Dedicated Servers from the game

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

Parallels. for your Linux or Windows Server. Small Business Panel. Getting Started Guide. Parallels Small Business Panel // Linux & Windows Server

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Computational Mathematics with Python

A Crash Course on UNIX

Programming with the Dev C++ IDE

Tutorial: Using WestGrid. Drew Leske Compute Canada/WestGrid Site Lead University of Victoria

Adafruit's Raspberry Pi Lesson 6. Using SSH

Example of a Java program

Data Tool Platform SQL Development Tools

Transcription:

C++ Tutorial Let s get to coding W. Deconinck 1 V. Gray 2 1 Assistant Professor Department of Physics College of William & Mary 2 Graduate Student Department of Physics College of William & Mary C++ Tutorial, May 29, 2014

Outline C++ An Overview Data Types, Variables, etc Development Environments C++ 101 Set Up

Data Types bool int char float double std:: string Declaration & Initialization int a; a = 1; or int a = 1; int b(a); Type Casting unsigned int b = 1; int a = dynamic_cast <int >(b); double a = static_cast <double >(b); Scope Variable can only be used in their scope: the smallest block of braces ({ /*...*/ }) that surrounds them

Arrays // array of 3 integers with initialization int a[3] = {0, 1, 2}; // without explicit size (as it is redundant) int b[] = {0, 2}; // 2 x 2 matrix without initialization int M[2][2]; cout << a[0] << endl; // first element in a cout << M[1][1] << endl; // second diagonal element in M Indices Array indices in C++ start counting at 0: a[0], a[1],...

Vectors # include <vector > vector <int > a(3); // vector of integers with size 3 // Unfortunately, the initializer lists {0,1,2} // does not work with these vectors. cout << a. size() << endl; // print size of a vector <vector <int > > M; // vector of vectors of integers M. resize (2); // resize M to have two rows M[0]. resize (2); // two elements in first row M[1]. resize (3); // three elements in second row // print size of first row of M cout << M[0]. size() << endl; // print size of second row of M cout << M[1]. size() << endl;

Control structure if ( condition) { /* do something sometimes*/ }else { /* do something the other times */ }

Control structure for ( int i = 0; i < 10; i++) { /* do something 10 times */ }

Control structure switch (a) { case 1: /* do something */ break; case 2: /* do something different */ break; } default: /* do something in all other cases */ break;

Functions // functions are defined like type name ( parameter1, parameter2,...) { /* statements */ } // they are called like type name ( parameter1, parameter2,...); Signature Function can (but dont have to) have a signature before they are defined. The signature for a general function (above) is type name ( parameter1, parameter2,...); This signature will typically be placed in the header file, as it provides the interface to using the functions that are defined in the source file.

Call by Value When a function such as int addition (int a, int b) is called, the two arguments are copied and the function operates on the copy only. The original values of the arguments, a and b are not changed. Call by Reference It is also possible to call the function as int addition (int & a, int & b). The additional & characters indicate that the values for a and b are not copies but are the original values. The variables a and b are now references to the original variables, and any changes to a and b will affect the original values.

Pointers // functions are defined like type functionname ( parameter1, parameter2,...) { /* statements */ return something; } // they are called like functionname ( parameter1, parameter2,...);

Classes They are extensions of the different basic data types // class of a rectangle with sides x and y class CRectangle { int x, y; public: void set_values (int, int); int area (void); }; This is just the abstract object that represents a rectangle.

Classes They are extensions of the different basic data types // class of a rectangle with sides x and y class CRectangle { int x, y; public: void set_values (int, int); int area (void); }; There are private and public blocks of class data members (or fields) and functions (or methods). The private members and functions can only be accessed by the objects of the class itself. The public members and functions can be accessed by everyone.

Classes To create a variable of the type CRectangle you can use the same syntax as for the declaration of basic data types. CRectangle rect; // the variable rect of type CRectangle On the object rect you can now call all public functions and access public data members: rect. set_values (3,4); // set the values on object rect myarea = rect. area(); // get the area

Classes Notice that we have not yet specified the implementation for the functions void set values(int,int) and int area (void). For example void CRectangle:: set_values ( int a, int b) { x = a; y = b; } int CRectangle:: area ( void) { return (x*y); } The notation :: indicates that the functions set values and area are inside the class CRectangle.

Constructor & Destructor We have to tell C++ how to initialize objects of classes that we define, and how to free the memory again. For example: CRectangle:: CRectangle () { a = 0; b = 0; } CRectangle:: CRectangle () { std:: cout << "he s dead, Jim" << std:: endl; } Destructors are often used to release explicitly created pointer objects (with delete).

Integrated development environments An integrated development environment (IDE) the compiler, editor and debugger are integrated in the same software program. This typically makes for a more productive environment when you can spend the time in setting it up to work according to your needs. Eclipse (with C/C++ Development Tooling), http://www.eclipse.org/cdt/ This is an integrated development environment originally developed for Java programming but excellent support for C/C++ is available as well. It has syntax highlighting, auto-completion and even auto-generation of code, and it includes a debugger.

Integrated development environments An integrated development environment (IDE) the compiler, editor and debugger are integrated in the same software program. This typically makes for a more productive environment when you can spend the time in setting it up to work according to your needs. Kdevelop, http://www.kdevelop.org/ This integrated development environment focuses on C/C++ development, in particular for open source projects (e.g. KDE). It includes a similar set of features as Eclipse above.

Integrated development environments An integrated development environment (IDE) the compiler, editor and debugger are integrated in the same software program. This typically makes for a more productive environment when you can spend the time in setting it up to work according to your needs. Visual C++ Express (part of Visual Studio Express), http://www.microsoft.com/visualstudio/eng/ team-foundation-service This is an integrated development environment for Microsoft Windows.

C++ 101 Set Up Open a Terminal Window Windows users: this will be through a program like Putty or SSH Secure Shell Mac and Linux users: find the Terminal program Login $ ssh -X RootTutorial@sporades.physics.wm.edu Password Rootisfun2013

C++ 101 Set Up See what is there List the contents the home directory, via ls command [ RootTutorial@sporades ]$ ls cplusplus_tutorial_files cplusplus_tutorial_link. txt cplusplus_tutorial_files. tar root_tutorial_files root_tutorial_files. tar users

C++ 101 Set Up Create your directory Create the your own directory in the users directory [ RootTutorial@sporades ]$ cd users [ RootTutorial@sporades users]$ ls adorasmith dasalmon Henderson... ansmith eahenderson hnguyen... cmkaramitsos Eahenderson itgordon... crhaufe eemikh janderson... [ RootTutorial@sporades users]$ mkdir <yourwmusername> [ RootTutorial@sporades users] This directory is only for you. Please don t delete anything in someone elses directory

C++ 101 Set Up Copy the C++ tutorial files [ RootTutorial@sporades users]$ cd <yourwmusername> [ RootTutorial@sporades <yourwmusername>]$ mkdir cplusplus_tutorial_files [ RootTutorial@sporades <yourwmusername>]$ cp / cplusplus_tutorial_files/* / users/<yourwmusername>/ cplusplus_tutorial_files/

C++ 101 Set Up Double Check Make sure the files are there [ RootTutorial@sporades <yourwmusername>]$ cd cplusplus_tutorial_files/ [ RootTutorial@sporades <yourwmusername>]$ ls addition. cpp addition.h arrays arrays. cpp CRectangle. cpp datatypes. cpp helloworld. cpp helloworld.h pointers. cpp subtraction. cpp subtraction.h switch switch. cpp

Ready, Set, Go! text editors nano: a bare-bones text-based editor with basic syntax highlighting (after some effort). Exit by typing Ctrl-x vi: a text-based editor with a fairly steep learning curve. Exit by typing :q emacs: a graphical or text-based editor with a fairly steep learning curve. Exit by typing Ctrl-x,Ctrl-c nedit: a basic graphical editor with syntax highlighting (after some effort). Nedit runs smoothly over ssh connections. kate: a graphical editor with syntax highlighting. Kate supports opening files over ssh connections using the fish://server/path/to/file syntax.

Help? Need help? Linux command, terminal navigation: http://files.fosswire.com/2007/08/fwunixref.pdf, http: //www.thegeekstuff.com/2010/11/50-linux-commands/ C++: www.cplusplus.com Google: www.google.com