Introduction to Programming

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 7: User-Defined Functions II
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Learners Support Publications Functions in C++
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 6 Methods Chapter 6 - Methods.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Learners Support Publications Introduction to C++
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Chapter 7: User-Defined Functions II
IS Program Design and Software Tools Introduction to C++ Programming
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.
Functions and an Introduction to Recursion
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.
Anatomy of a Function Part 2
Lecture Notes – Week 3 Lecture-2
Introduction to Programming
Local Variables, Global Variables and Variable Scope
CS2011 Introduction to Programming I Methods (II)
Functions and an Introduction to Recursion
Introduction to Programming
Chapter 15 - C++ As A "Better C"
CS150 Introduction to Computer Science 1
Fundamental Programming
Introduction to Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Programming
Unit-1 Introduction to Java
CS150 Introduction to Computer Science 1
NQC Program Structure 3 types of code blocks with their own features and limitations 1. Tasks 2. Subroutines 3. Inline Functions.
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Introduction to Programming Lecture 25

Introduction to C++ language

In Coming Lectures C++ Features Classes Object

Today’s Lecture Default Function Arguments Inline Functions Classes

Rules for Structured Programming Do Modular programming Write small function Every function should perform a specific well defined task Function should obey single entry single exit rule

Liberally Comment the code

Object Oriented Language

Early 1980's Bjarne Stroustrup at Bell Labs

C with Classes C++ Java

Default Function Arguments

power ( x , n ) ;

void f ( int i , double x ) ;

void f ( int i =1 , double x = 10.5 ) { -- }

Example 1 void f ( int i = 1 , double x = 10.5 ) { cout<<“i = ”<<i; cout<<“ x = “<< x<<endl; }

Example 1 main ( ) { f ( ) ; f ( 2 ) ; f ( 2 , 12 ) ; } Output i = 1 x = 10.5 i = 2 x = 10.5 i = 2 x = 12

void f ( double x=10.5 , int i ) ;

The declaration of variables inside the code

while ( condition ) { // body of the while loop }

{ int i ; --- }

Scope of Variable When you declare a variable inside the block then the variable exists in that block.

Example 2 for ( int i = 0 ; i < 3 ; i++ ) { int temp = 22 ; cout << "\n i = " << i << "temp = " << temp ; } cout << "i = " << i ;

Inline functions

inline

Example 2 #define SQUARE ( X ) X * X main ( ) { int i = 5 , j = 10 , k ; : k = } i + j * i + j ; SQUARE ( i + j ) ;

Example 3 #define SQUARE ( X ) ( X ) * ( X ) main ( ) { int i = 5 , j = 10 , k ; k = } SQUARE ( i + j ) ; ( i + j ) * ( i + j ) ;

Example 3 #define MAX ( A , B ) ( ( A ) > ( B ) ? ( A ) : ( B ) ) inline f ( int a, int b ) { if ( a > b ) return a ; return b ; } void main ( ) int i , x , y ; x = 23 ; y = 45 ; i = MAX ( x++ , y++ ) ; // Side - effect : larger value incremented twice cout << "x = " << x << " y = " << y << '\n' ; i = f ( x++ , y++ ) ; // Works as expected

Function Overloading

Operator Overloading

Overloading Using the same name to perform multiple tasks or different task depending on the situation

double Intsquareroot ( int i ) ; double Doublesquareroot ( double x ) ;

Example 4 int squareroot ( int i ) { // body of the function } double squareroot ( double x )

Example 4 main ( ) { double i , x ; int j = 5 , k ; i = squareroot ( 10.5 ) ; cout << i << endl ; k = squareroot ( j ) ; cout << k << endl ; }

Example F ( int a , int b ) ; F ( int a , int b , int c ) ; main ( ) { ----------- F ( 1 , 2 ) ; // F ( int a , int b ) ; is called F ( 1 , 2 , 3 ) ; // F ( int a , int b , int c ) ; is called }

int f ( int a ) ; double f ( int a ) ;

Name Mangling