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



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

El Dorado Union High School District Educational Services

The C Programming Language course syllabus associate level

Object Oriented Software Design II

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Curriculum Map. Discipline: Computer Science Course: C++

C++ INTERVIEW QUESTIONS

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

Member Functions of the istream Class

Ch 7-1. Object-Oriented Programming and Classes

C++ Programming Language

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

Comp151. Definitions & Declarations

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

Compiler Construction

CS 103 Lab Linux and Virtual Machines

Brent A. Perdue. July 15, 2009

Basics of I/O Streams and File I/O

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

CSC230 Getting Starting in C. Tyler Bletsch

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

Informatica e Sistemi in Tempo Reale

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

CORBA Programming with TAOX11. The C++11 CORBA Implementation

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

CISC 181 Project 3 Designing Classes for Bank Accounts

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

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

Visual C Tutorial

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

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

J a v a Quiz (Unit 3, Test 0 Practice)

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Tutorial on C Language Programming

C++FA 5.1 PRACTICE MID-TERM EXAM

Facebook Twitter YouTube Google Plus Website

Moving from CS 61A Scheme to CS 61B Java

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

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

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

Lab 2: Swat ATM (Machine (Machine))

OpenCL Static C++ Kernel Language Extension

Note: Syntactically, a ; is needed at the end of a struct definition.

Illustration 1: Diagram of program function and data flow

Answers to Review Questions Chapter 7

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

C++ Language Tutorial

Class 16: Function Parameters and Polymorphism

CS 111 Classes I 1. Software Organization View to this point:

Glossary of Object Oriented Terms

Java Interview Questions and Answers

Java (12 Weeks) Introduction to Java Programming Language

A brief introduction to C++ and Interfacing with Excel

EP241 Computer Programming

Basics of C++ and object orientation in OpenFOAM

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

5 Arrays and Pointers

Java Crash Course Part I

Java Application Developer Certificate Program Competencies

Chapter 6: Programming Languages

Developing an ODBC C++ Client with MySQL Database

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)


Crash Course in Java

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Netscape Internet Service Broker for C++ Programmer's Guide. Contents

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

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

AP Computer Science Java Subset

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

7.7 Case Study: Calculating Depreciation

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

C++ Support for Abstract Data Types

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

csce4313 Programming Languages Scanner (pass/fail)

Software documentation systems

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Chapter 1 Java Program Design and Development

Coding conventions and C++-style

Data Structures using OOP C++ Lecture 1

Surface and Volumetric Data Rendering and Visualisation

Object Oriented Software Design

Appendix M: Introduction to Microsoft Visual C Express Edition

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Object Oriented Software Design II

Charm++, what s that?!

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

13 Classes & Objects with Constructors/Destructors

How To Write Portable Programs In C

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

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

BCS2B02: OOP Concepts and Data Structures Using C++

ECE 341 Coding Standard

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Transcription:

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf

C++ is a legacy programming language, as is other languages such as Language First appeared Fortran mid 1950 s C 1970 Pascal mid 1970 s Modula 2 late 1970 s Java 1990 s C# 2000 s and (many) others. All of these languages require a significant learning curve, are easy to make mistakes in that are hard to find, and are hard for others to decode (in large part because programmers fail to adequately comment codes or use badly named data variables). exist to manipulate data using algorithms. 2

Textbook useful hints (by page) 10-36: basics 87-97: arrays 108-110: switch statements 126-127: compound assignment 128-138: classes 141: cerr MPI (by page) 71-80: basics 651-676: the works 3

hello.cpp A simple first program is the hello, world program: // my first program in C++ #include <iostream> using namespace std; int main (int argc, char** argv) /* command line argument info */ { cout << "Hello World!\n"; // cout is standard character output return 0; } argc is the number of arguments. argv is a pointer to an array of character pointers that hold each of the arguments given to the program when it runs. 4

Compile the file with g++, e.g., g++ hello.cpp o hello Run the program with./hello Some people like to add an extension.exe on executables. Tastes vary. g++ is available on Linux and OS X systems easily. On Windows, you can install it natively or after installing Cygwin (http://www.cygwin.com), a Linuxlike port of many Linux and FSF/GNU programs. 5

Major parts of C++ Data types o Integer: int, short, short int, long, long int o Floating point: float, double, long double o Character: char o User defined Classes o A container for functions (called methods) about complicated data structures with public, protected, and private data. o Classes can be combined through inheritance to be quite complicated. Templates o A mechanism to define a class for a wide variety of data instead of statically defined data, e.g., define a vector class for int, float, double with a single source code. Which data type is actually used is determined when a class is used in a program declaration. Yikes. Functions o Independent programs that implement algorithms. 6

Data types Type Subtype Size Description Integer int 32 or 64 standard sized long or long int 32 or 64 bigger is better short or short int 16 or 32 Legacy Floating float 32 single precision Point double 64 double precision long double 128 quad precision Character char 8 or 16 single character Arrays char name[100]; // 100 characters, indexed 0-99 char me[] = Craig Douglas ; // array size computed by compiler double matrix[10][20]; // 10 rows, 20 columns 7

Functions Output + Function name ( Arguments ) Pointers and References char* who; who = me; cout << who[0] << endl; who[0] = c ; who[6] = d ; cout << who << endl; who = name; cout << who[0] << who[1] << endl; // endl = end of line ( \n ) // me is now in lower case // 1 st 2 characters 8

References are used in function declarations and are similar to pointers: double inner_product( double& x, double& y, int len ) { double ip; // return value for( int i = 0, ip = 0.0; i < len; i++ ) ip += ( x[i] * y[i] ); return ip; } // 0 <= i < len // for loop s one statement // or just ip; Here are lots of new things: a for loop with a local variable definition, an increment operator, and a complicated assignment statement inside the loop. References (double& x) differ from pointers (double* x) only that the data in a reference variable will not change. In the inner_product function, neither x nor y will change, so they can be reference variables. Compilers can do better optimizations on read only variables than on ones that can be changed. 9

Classes Two distinct parts should be defined: 1. A header file with declarations and method (function) headers. 2. An implementation file that is compiled. The header file should have the following sections: private: all hidden variables and members are declared here. protected: only classes declared as a friend are declared here. public: members that can be accessed by anyone. o Constructors, the destructor, and copy members should be defined. o Access members and algorithmic members should be defined. 10

A sample header file for a class hello is hello.h: #ifndef H_class_hello #define H_class_hello class hello { private: char greeting[100]; public: hello(); hello(const char*); virtual ~hello(); hello(hello& the_other_hello); void set_greeting(const char*); void print_greeting(); }; #endif // constructor, no arguments // constructor, 1 argument (greeting) // destructor // copy constructor // set greeting // print greeting 11

The implementation file, hello.cpp, could be as simple as #include "hello.h" #include <iostream> #include <string> using namespace std; hello::hello() { char ini = 0; set_greeting(&ini); } hello::hello(const char* msg) { set_greeting(msg); } hello::~hello() { } hello::hello(hello& the_other_hello) { hello(the_other_hello.greeting); } void hello::set_greeting(const char* msg) { strcpy(greeting, msg); } void hello::print_greeting() { cout << greeting << endl; } You should study one of the classes in the textbook s software. 12

Compound Statements C++ has many compound statements, including if ( clause ) statement else if (clause ) statement else (clause ) statement o else if and else are optional o many times statement is actually { statements } for( initialize ; stopping condition ; updates at end of loop ) statement o initilize can be multiple items separated by commas o stopping condition is anything appropriate inside an if clause o updates are comma separated items o usually there is only one item, not multiple while ( true condition ) statement o true condition is anything appropriate inside an if clause switch ( variable ) { case value: statements break default: statements } o multiple case statements can occur without a statement between them o default is optional o remember the break or the computer will continue into the next case (unless this is desired) 13

One of C++ s strengths and weakness is the ability to overload operators. A very good online source of information about how overload any operator in C++ is given at the URL http://www.java2s.com/tutorial/cpp/0200 Operator- Overloading/Catalog0200 Operator-Overloading.htm C++ has a Template mechanism that allows classes to be automatically defined for different data types. There is even a Standard Template Library (STL) that covers many useful template types. Useful tutorials can be found at http://www.cplusplus.com/doc/tutorial/ http://www.cplusplus.com/files/tutorial.pdf http://www.java2s.com/tutorial/cpp/catalogcpp.htm 14