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.

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

C++ Programming Languages
Chapter 7: User-Defined Functions II
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ fundamentals.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
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.
Subprograms subroutines, procedures, functions, methods.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Object-Oriented Programming (OOP) and C++
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Chapter 15 - C++ As A "Better C"
Examples (D. Schmidt et al)
Eine By: Avinash Reddy 09/29/2016.
Object Lifetime and Pointers
C ++ MULTIPLE CHOICE QUESTION
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 6 CS 3370 – C++ Functions.
Chapter 1.2 Introduction to C++ Programming
C++ Templates.
C++ INTERVIEW QUESTIONS
Introduction to C++ Systems Programming.
CS3340 – OOP and C++ L. Grewe.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
A Lecture for the c++ Course
Introduction to C++.
Pointers and Pointer-Based Strings
Object-Orientated Programming
C Basics.
Templates.
(5 - 1) Object-Oriented Programming (OOP) and C++
Object-Oriented Programming Part 1
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Brought to you by C++ Tutorial Brought to you by
More About Data Types & Functions
Chapter 4 Implementing Free Functions
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Classes and Objects.
(5 - 1) Object-Oriented Programming (OOP) and C++
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
7 Arrays.
Chapter 15 - C++ As A "Better C"
Pointers and Pointer-Based Strings
COMS 261 Computer Science I
Recitation Course 0603 Speaker: Liu Yu-Jiun.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Presentation transcript:

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)

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

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

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

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)

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

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

Comments Can use /* …… */ of ‘C’ Double / provides 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

Simple Stream Input and output Requires inclusion of iostream header file 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;

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 return to it as we do through the unit.

Stream Input and output 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 output buffer and outputs newline character.

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

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.

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.

Passing arguments to functions In ‘C’ we had to 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.

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

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

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.

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

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.

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)

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.