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
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Chapter 10.
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.
Program Input and the Software Design Process ROBERT REAVES.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
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.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Subprograms subroutines, procedures, functions, methods.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
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
Copyright 2008 Oxford Consulting, Ltd 1 October C to C++ C++ Comments Can use the symbol // To identify single line comments.
More C++ Features True object initialisation
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C LANGUAGE Characteristics of C · Small size
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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++
2Object-Oriented Program Development Using C++ 3 Function Declarations and Definitions Analogize functions to methods –Share same basic features –Unlike.
Object-Oriented Programming (OOP) and C++
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
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"
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
C++ INTERVIEW QUESTIONS
Introduction to C++ Systems Programming.
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
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.
(5 - 1) Object-Oriented Programming (OOP) and C++
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++
Chapter 15 - C++ As A "Better C"
COMS 261 Computer Science I
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’ 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

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;

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.

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.

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)

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