13
Getting Started with C++ Yingcai Xiao 09/03/08

Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Embed Size (px)

Citation preview

Page 1: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Getting Started with C++

Yingcai Xiao

09/03/08

Page 2: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Outline

What (is C++) Who (created C++) How (to write a C++ program)

Page 3: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

C++

What is C++?

C++ is an object-oriented programming language.

Who created C++?

Bjarne Stroustrup, while at AT&T.

(now, a professor in Computer Science at Texas A&M University)

http://www.research.att.com/~bs/

Page 4: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

C++C++ by Bjarne Stroustrup : • FAQ:

http://www.research.att.com/~bs/bs_faq.html

http://www.research.att.com/~bs/bs_faq2.html#simple-program

• Books:

The C++ Programming Language

The Design and Evolution of C++

• Recommendations:

http://www.research.att.com/~bs/C++.html

http://www.research.att.com/~bs/glossary.html

"C++ coding standards“ by Sutter and Alexandrescu.

http://www.research.att.com/~bs/JSF-AV-rules.pdf

Page 5: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

C++ and Other Programming Languages

C: a non-object-oriented programming language, a subset of C++.

Java: write-once, run-anywhere Needs JRE (Java Runtime Environment).Started simple and complexity being added later.“Java isn't platform independent; it is a platform.”

.NET: any programming language and any-platform

Needs to confirm to CTS (Common Type System).

Needs to run through CLR (Common Language Runtime).

.Net Code are called managed code.

C++.NET: Managed C++. A subset of C++ confirms to CTS and runs through CLR.

C++/CLI: extensions of C++ to CLI (Common Language Infrastructure). Better than managed C++.

C#: A better Java confirms to CTS and runs through CLR. Runs only in .NET. C(+)n, n=0,2,4,?

Page 6: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

C++ Compilerhttp://www.research.att.com/~bs/compilers.html

GNU C++http://gcc.gnu.org/

Cygwin: is a Linux-like environment for Windows

http://www.cygwin.com/

Microsoft Visual Studio

Page 7: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Coding with an IDE (MS Visual Studio 2008)

Using Start->Program Files-> MS Visual Studio 2008-> MS Visual Studio 2008

Select C++ as default environment if prompted to.

File->New->Project->

Other Languages->Visual C++->Win32->Win32 Console Application

Name: test

Location:

My Documents\Visual Studio 2008\Projects

Note: Put all your projects under the directory. It is actually mapped to C:\Documents and Settings\YourID\My Documents\Visual Studio 2008\Projects. This directory is carried over if you log onto different lab computers and is protected from other users.

Page 8: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Coding with an IDE (MS Visual Studio 2008)

OkWelcome to the Win32 Application Wizard

Next

Console application

Empty project

Project->Add->New Item->Visual C++->Code->C++ File (.cpp)

Name: test

Page 9: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Coding with an IDE (MS Visual Studio 2008)

Enter the following code#include <iostream> using namespace std;int main() { cout << "Hello, World! " << endl; cout << "Pleas enter 'end' to end the program: "<< endl; char a; cin >> a;}

Build->Build testDebug->Start Without DebuggingOr double click:My Documents\Visual Studio 2008\Projects\test\debug\test.exe

Page 10: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Coding without an IDE

• Follow parts 1-3 at the following site to install cygwin and c++ (g++) http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/cygwin/

• Start->Program File->Cygwin->Cygwin Bash Shell

To see the path of your Cygwin home directory:

pwd (ignore /cygdrive/)

To go to your Z drive:

cd Z:

To create a working directory:

mkdir oop

To move to the working directory:

cd oop

Page 11: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Coding without an IDE

To create a program:

notepad test.cpp

Cut-and-paste an example program from

http://www.cs.uakron.edu/~xiao/oop/C++Examples.zip

file->save

file->exit

To compile the program:

g++ test.cpp –o test

To run the program:

./test

Page 12: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Your first C++ Program/* Hello.cpp. This program demonstrates basic io, storage and computation in C++ Author: Dr. Xiao (9/6/07).*/

#include <iostream> // head file for predefined io classes and objects #include <string> // head file for the predefined string class#include <math.h> // math.h: head file of math functions in c

// can't omit .h, but can use <cmath> instead.using namespace std; // all names are in the std namespace

int main() {// primitive data typesint i;float f;char a;// pre-defined classesstring so,si;

Page 13: Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)

Your first C++ Program// basic io and computationcout << “Hello! \n”so = "Please enter ";cout << so;cout << "a string: ";cin >> si;cout << si << endl;cout << "Please enter an integer: ";cin >> i;cout << "You square of " << i << " is " << i*i << endl;cout << "Please enter a floating point number: ";cin >> f;cout << "The square root of " << f << " is " << sqrt(f) << endl;so += "'q' to end: ";cout << so; cin >> a;

}