Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining."— Presentation transcript:

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++ application development C++ Source File compiler C++ Include Files Object File Linker System Object Files.EXE File

6 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” –the quotes indicate that the file is in the same directory as the file being compiled #include –the indicate the file is in the compiler system directory #include “fully qualified file name ”

7 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.

8 Data Types

9 Primitive Types Integer types –char : 8 bit –short : 16 bit –int : 32 bit –long : 64 bit Real Types –float - 32 bit real numbers –double - 64 bit real numbers

10 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 = ‘+’;

11 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

12 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)

13 Derived Types These operators create new types from the basic types –*pointer to –*constconstant pointer –&reference to (addressing operator) –[ ]vector of –()function returning

14 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++) {;}

15 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.

16 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);

17 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);

18 Array passing always pass by reference (default method)


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

Similar presentations


Ads by Google