Download presentation
Presentation is loading. Please wait.
1
CS-341 Dick Steflik Introduction
2
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
3
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
4
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
5
C++ Compilers GNU C++ –Open Source –Multi-platform Windows UNIX (most) LINUX MAC Microsoft Visual C++ –includes everything needed to program the Windows API
6
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
7
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
8
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
9
Java Application Development Java Source File javac Java Class File JVM classpath base classes Compile into bytecodes Run the program
10
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
11
C++ application development C++ Source File compiler C++ Include Files Object File Linker System Object Files.EXE File
12
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
13
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 #include “fully qualified file name ”
14
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
15
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.
16
Java - HelloWorld public class HelloWorld { public static void main(String args[]) { System.out.println(“Hellow World”); }
17
C++ - HelloWorld #include int main() { cout << “Hello World” << “\n”; }
18
Data Types
19
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
20
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 = ‘+’;
21
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
22
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)
23
Derived Types These operators create new types from the basic types –*pointer to –*constconstant pointer –&reference to (addressing operator) –[ ]vector of –()function returning
24
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++) {;}
25
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=7 foo a = 5 b = 5 This gives the effect of x and y being input only as the function can’t change them.
26
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);
27
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);
28
Array passing always pass by reference (default method)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.