Sie sind auf Seite 1von 22

Welcome to CMSC 389C

C++ Programming

Please put laptops away…

Welcome to CMSC 389C C++ Programming Please put laptops away… © 2011 Fawzi Emad, Computer Science

© 2011 Fawzi Emad, Computer Science Department, UMCP

1

Let’s go over class webpage…

<Play Elevator Music>

© 2011 Fawzi Emad, Computer Science Department, UMCP

2

Before I forget….

HW #1 will be posted TODAY.

© 2011 Fawzi Emad, Computer Science Department, UMCP

3

Brief History

C – Invented in early 70’s when computers were SLOW so it was designed to be EFFICIENT

C++ Extends C and replaces many features. Still emphasizes EFFICIENCY

Java Emphasizes simplicity and bug-free code production

© 2011 Fawzi Emad, Computer Science Department, UMCP

4

Compiling: C++ vs. Java

Java Portable bytecode

Each platform needs its own JVM to run the bytecode

C++ Native executables (not portable) Slight advantage in runtime performance

© 2011 Fawzi Emad, Computer Science Department, UMCP

5

API: Java vs. C++

Java HUGE standardized libraries Excellent collections framework Generics

C++

Fewer standard librariesvendors supply endless variations

Standard Template Library (STL) Templates

© 2011 Fawzi Emad, Computer Science Department, UMCP

6

What’s Hot?

What’s Hot? Ratings are calculated by counting hits on the most popular search engines © 2011

Ratings are calculated by counting hits on the most popular search engines

© 2011 Fawzi Emad, Computer Science Department, UMCP

7

C++ Advantages

Efficient runtime performance Space efficiency Operator overloading Destructor that runs predictably Enforcing immutability (const) Multiple Inheritance

© 2011 Fawzi Emad, Computer Science Department, UMCP

8

C++ Hazards

C++ Hazards • Multiple Inheritance • Pointers to garbage • Inconsistent type casts at runtime •

Multiple Inheritance Pointers to garbage Inconsistent type casts at runtime Uninitialized variables Memory leaks Few dynamic “safety checks” Implicit copying must be handled carefully Etc…

© 2011 Fawzi Emad, Computer Science Department, UMCP

9

CMSC 389C Development Environment

Class accounts (grace) Important: Hostname is linux.grace.umd.edu

/afs/glue.umd.edu/class/winter2011/cmsc/389c/0101 g++ compiler for official testing

Use ssh client to move files back and forth (See Resources

Page)

IDE (Recommended)

Many helpful features

Easier for debugging

Visual C++ Express

Others (See Resources Page)

© 2011 Fawzi Emad, Computer Science Department, UMCP

10

Grace Account Demo

Demo Grace Hostname: linux.grace.umd.edu Create file with emacs or other editor Compile with g++

#include<iostream> using namespace std;

int main() {

cout << "Hello World" << endl; return 0;

}

© 2011 Fawzi Emad, Computer Science Department, UMCP

11

Visual C++ Demo

Demo Visual C++ New, Project, CLR Empty Project Right-click “Source Files”, Add, New Item, C++ file

#include<iostream> using namespace std;

int main() {

cout << "Hello World" << endl; cin.ignore(); return 0;

}

© 2011 Fawzi Emad, Computer Science Department, UMCP

12

Example: firstProgram.cpp

#include

“using namespace” Global scope Functions

Basic I/O main

© 2011 Fawzi Emad, Computer Science Department, UMCP

13

Primitives

Similar to Java

Integers long, int, short

No “byte” signed or unsigned

Floating point double, float bool (not “boolean”) char

Important: Sizes are platform dependent!

© 2011 Fawzi Emad, Computer Science Department, UMCP

14

Example: sizeCheck.cpp

sizeof

© 2011 Fawzi Emad, Computer Science Department, UMCP

15

Example: rounding.cpp

Arithmetic (similar to Java) Rounding varies with different compilers!

© 2011 Fawzi Emad, Computer Science Department, UMCP

16

Casting Primitives

Both work:

(double) 3/5 static_cast<double> 3/5

Favor static_cast!

© 2011 Fawzi Emad, Computer Science Department, UMCP

17

Conditionals

Similar to Java:

if

if/else ?: ternary operator

Condition can be:

int (like C)

 

0 for false

non-0 for true

bool (like Java)

Favor bool!

© 2011 Fawzi Emad, Computer Science Department, UMCP

18

Loops

Very similar to Java:

while do-while for break continue

© 2011 Fawzi Emad, Computer Science Department, UMCP

19

Example: functions.cpp

Global functions passing primitives (by value) No enforced type checking!

© 2011 Fawzi Emad, Computer Science Department, UMCP

20

Example: upsideDown.cpp

Top-down compilation

Function definition Function declaration (prototype)

© 2011 Fawzi Emad, Computer Science Department, UMCP

21

Example: defaultArguments.cpp

If one argument has a default, then all subsequent ones must also

Default values can go either in function definition or declaration, not both!

Favor default arguments in declaration.

© 2011 Fawzi Emad, Computer Science Department, UMCP

22