28
CS-341 Dick Steflik Introduction

CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

CS-341

Dick Steflik

Introduction

C++ • General purpose programming language

• A superset of C (except for minor details)

• provides new flexible ways for defining new types (classes)

• provides symbolic constants

• provides inline substitution of functions

• allows default function arguments

• allows function name overloading

• allows operator overloading

• operators for managing free storage

• provides a reference type

C++ Usage

• procedural programming - a better version of C– cleaner handling of arrays as function parameters

– stream based I/O model

• object oriented programming– encourages reuse of already tested code rather than inventing a

problem solution over and over

– encourages data abstraction

– encourages data hiding

The C++ Compiler

• Translates C++ source code into the machine language for the hardware platform– output of the compiler are object files (.obj) which are partially ready to

run

– object files must be linked together using the system linker to make an executable file (.exe)

• .exe files are specific for the hardware platform

• C++ is portable only at the source level

C++ Compilers

• GNU C++

– Open Source – Multi-platform

• Windows

• UNIX (most)

• LINUX

• MAC

• Microsoft Visual C++– includes everything needed to program the Windows API

This semester...

• All programs submitted must be able to run on the Sun Blade workstations in Classroom 2 of the POD.– This can be insured by using the GNU gpp Compiler

• you may develop on the Sun Blade Workstations, on LINUX (gpp comes with all standard LINUX installs) or on CYGWIN/XEMACS (a emulated UNIX environment that runs on Windows, a CD will be made available or you may download and install from the internet.)

• Those of you that have Windows machines , I encoutage to install CYGWIN/EMACS

• Those of you with Windows machines that are more daring I encourage you to make the leap to LINUX

• You may also telnet into BINGSUNS and use GPP directly in that environment

Java C++

• Interpreted• optimized for the

internet• Derived from C++ • Good object model• Widely accepted as the

internet programming language

• 4-5 years old

• Compiled• optimized for

workstation usage• derived from C• Poorer object model• Widely accepted as

workstation program- ming language

• 10-12 years old

The Java Compiler

• Translates Java source code into java bytecodes– java bytecodes are the machine language for the

Java Virtual Machine• the JVM is a hypothetical machine that written

mostly in Java but a little bit in C++ and is compiled specifically for various platforms (Windows, Sun, HP, LINUX)

• Bytecodes are portable between platforms

Java Application Development

Java Source

Filejavac

Java Class

FileJVM

classpath

base classes

Compile into bytecodes

Run the program

The C++ Compiler

• Translates C++ source code into the machine language for the hardware platform– output of the compiler are object files (.obj)

which are partially ready to run– object files must be linked together using the

system linker to make an executable file (.exe)• .exe files are specific for the hardware platform

• C++ is portable only at the source level

C++ application development

C++ Source

Filecompiler

C++ Include

Files

ObjectFile Linker

SystemObjectFiles

.EXE File

Java import statements

• imports indicate which parts of which java packages an application will need at run time– also allows the compiler to run to completion

and not indicate that the class isn’t part of the file being compiles

• Remember the compiler only works with the file being compiled

C++ include statements

• indicate to the compiler preprocessor which files (source) to include in the file to be compiled. Included files are actually copied into the source file and replace the

#include statement as part of precompilation.• #include “myclass.h”

• #include <stdio.h>

• #include “fully qualified file name”

Java application

• is defined as a class that is instantiated when the program is run

• main() is the name of the function to be run initially

• constructor is used initialize the class

C++ application

• classes are/can be used by a C++ application; but the application is not itself an instantiated class (like Java)

• main() is the name of the function where the executable file starts execution

• C++ classes do not have main() functions

• C++ objects are initialized via class constructors, C++ applications are initialized in the main() function.

Java - HelloWorld

public class HelloWorld {

public static void main(String args[]) {

System.out.println(“Hellow World”);

}

}

C++ - HelloWorld

#include <iostream.h>

int main()

{

cout << “Hello World” << “\n”;

}

Data Types

Primitive Types

• Integer types– char : 8 bit– short : 16 bit– nit : 32 bit– long : 64 bit

• Real Types– float - 32 bit real numbers– double - 64 bit real numbers

Constants

• In C++ constants are similar to variables but are never allowed to be the target of an assignment (explicit or implicit)

• In C++ constants are defined as:

– const int abc = 100 ; const char plus = ‘+’;

Strings

• C++ has no native built-in String type

• Strings are implemented as null terminated arrays of characters, last character of the string is a null character (‘\0’\)

• char [3] abc = “me”

– array abc consists of “m”, “e”, null

• abc = “me” (shortcut for defining and initializing a string)

• string functions are in <string.h>

Boolean (false/true)

• C++ has no boolean type like Java

– the value 0 represents false

– any other value represents true

• the statement “if (50) cout <<“t”;

– will always print “t” as 50 is not 0 so the predicate is true

• the statement “if (5-5) cout<<“t”; else cout << “f” ;

– will always print “f” as (5-5) is 0 (i.e. False)

Derived Types

• These operators create new types from the basic types– * pointer to– *const constant pointer– & reference to (addressing operator)– [ ] vector of– () function returning

Null statements

• the simplest statement is the null statement

• ;

• useful if the syntax requires a statement

• for instance to introduce a time delay into a program:– for (int j = 0 ; j < 100 ; j++) {;}

Function Parameters

pass by value (default):

upon invoking the function the function creates a local copy of the parameter and copies the value to the copy.

int foo (int a , int b)

{ … }

int x = 5 ; int y = 7;

int b = foo(a , b);

x=5

y=7fooa = 5b = 5

This gives the effect of x and y being input only as the function can’t change them.

Function Parameters

pass by reference:

passes a reference to the parameter to the function, the function works with the reference. The function can both read from and write to the argument. Internal to the function the name in the definition is used as an alias for the argument

use the & operator to indicate “pass by reference

int foo(int &a , int & b) {….}

int x=5 ; int y = 7;int t = foo(x,y);

Arrays

Array definition is the same:

ex. int myarray[5];

when used with a function the syntax is different, to pass an array to a function you no longer need to pass a pointer to the array, just pass the array name.

int sum(int cnt , int [ ] m){

int t = 0;

for (int i=0 ; i<cnt ; i++) t = t + m[i];

return t }

int x = sum(5,myarray);

Array passing

always pass by reference (default method)