Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.

Similar presentations


Presentation on theme: "C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in."— Presentation transcript:

1 C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in 1998 Standard class libraries Standard Template Library (STL)

2 C++ Overview C++ is a mostly upwardly compatible extension of C that provides: – Stronger type checking – Support for data abstraction – Support for object-oriented programming – Support for generic programming

3 C++ Design Goals As with C, run-time efficiency is important. Unlike other languages (e.g. Ada) complicated run-time libraries have not traditionally been required for C++ Note, that there is no language-specific support for concurrency, persistence, or distribution in C++ Compatibility with C libraries and traditional development tools is emphasized e.g. Object code reuse

4 C++ Design Goals “As close to C as possible, but no closer" – i.e. C++ is not a proper superset of C! Backwards compatibility is not entirely maintained. Typically not a problem in practice. Note, certain C++ design goals conflict with modern techniques for: – 1. Compiler optimization e.g., pointers to arbitrary memory locations complicate register allocation and garbage collection – 2. Software engineering e.g., separate compilation complicates in-lining due to difficulty of inter-procedural analysis

5 Major C++ Enhancements C++ supports – object-oriented programming features abstract classes, inheritance, and virtual methods – Data abstraction and encapsulation the class mechanism and name spaces – generic programming parameterized types – sophisticated error handling exception handling – identifying an object's type at runtime Run-Time Type Identification (RTTI)

6 Important Minor Enhancements C++ enforces type checking via function prototypes Provides type-safe linkage Provides inline function expansion Declare constants that can be used to define static array bounds with the const type qualifier Built-in dynamic memory management via new and delete operators Namespace control

7 C++ Based on C Comments in C++ Simple input and output using stream I/O Reference variables and parameter passing Use of const keyword

8 Comments Can use /* …… */ of ‘C’ for multi-line comments. // provides a single line comment. The end of the line marks the end of the comment // This is a comment cout << x; // another comment Using pre-processor commands to comment out a block of code containing other comments :- #if 0 code to be commented out #endif

9 Simple Stream Input and output Requires inclusion of iostream header file #include The header file creates three stream objects – cin, cout and cerr. The insertion operator << is used with cout and cerr The extraction operator >> is used with cin General format – cout << variable; cin >> variable;

10 Stream Input and output cout and cerr are instances of the ostream class cin is an instance of the istream class These streams can correctly handle all the standard built in data-types Stream I/O is an advanced topic and we may return to it later in the module.

11 Stream output and input Examples: cout << “The value is “ << v << endl; cout << “The answer is “ << a << “ohms\n”; cin >> x >> y ; // input two values x and y To prompt for input:- cout << “Enter your age “; cin >> age; endl - flushes the output buffer and outputs a newline character.

12 Passing arguments to functions Calling a function :- – func(arg1, arg2, etc.); // arg1 etc. are actual arguments The function – void func(farg1, farg2, etc.); //farg1 etc are the formal arguments Two ways to pass data – Pass by value (A copy of the actual argument is made. The function uses the copy) – Pass by reference ( The address of the actual argument is passed to the function, The function has access to the actual argument)

13 Passing arguments to functions By default – the basic primitive data types (int, char, float etc.) are passed by value. – Structures are passed by value. – Arrays are passed by reference. This is for efficiency reasons. We can override pass by value and force pass by reference. But not the other way round.

14 Passing arguments to functions Why might we want to override pass by value and use pass by reference? – To allow the function being called to modify the passed argument(s). – For efficiency. E.g. We may be passing a large structure.

15 Passing arguments to functions In ‘C’ we use pointers to achieve pass by reference. In C++ we can use pointers as before or reference variables. A reference variable can be thought of as an alias i.e. another name for the same thing.

16 Passing arguments to functions Consider a function which swaps the value of two variables int a = 123, b = 456; swap(a,b); cout << “a is “ << a << “, b is “ << b << endl; This should output a is 456, b is 123

17 Passing arguments to functions void swap(int x, int y) { int temp = x; x = y; y = temp; } Will this work?

18 Using pointers to pass by reference In ‘C’ we would force pass by reference and use pointers :- void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } This would also require us to modify the call to the swap function:- swap( &a, & b); This would now work correctly.

19 Using reference variables void swap(int& x, int& y) // x and y are references to int’s { int temp = x; x = y; y = temp; } The call to the function swap would remain as swap(a,b) x is reference variable a (i.e. an alias for a) The compiler actually creates pointers behind the scenes

20 Reference variables We now have the efficiency benefit of pass by reference and the simplicity of coding of pass by value. We can now use pass by reference to achieve efficiency even if the function doesn’t need to modify the passed arguments. In order to restrict the function to have read only access to the passed argument we use the const keyword. Example: void print_name( const PERSON & p) { cout << “name is “ << p.name << endl; } Where PERSON could be a very large structure. It is efficient because it is pass by reference and the function only has read access to the structure. The complier will give an error if the function attempts to modify the structure.

21 Reference variables One disadvantage of reference variables is that just looking at the function call it impossible to know whether pass by value or pass by reference is being used. One common guideline when using pass by reference is to use pointers for the basic data-types and reference variables for user defined types (data structures and objects)

22 Miscellaneous All functions must be declared before being used. A structure template name becomes a type name. Variable declarations can be inter-mingled with executable statements Global variables can be initialised with an expression, provided the expression can be computed. C++ has enumerated types similar to PASCAL TYPE Constants should be used in preference to #define statements. Example- – #define MAXVAL 400 should be replaced with const int maxval = 400; and declared at the appropriate scope level rather than globally as is the case for #define.


Download ppt "C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in."

Similar presentations


Ads by Google