Dr. Bhargavi Dept of CS CHRIST

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Guide To UNIX Using Linux Third Edition
Data Types.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Chapter 15 - C++ As A "Better C"
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: User-Defined Functions II
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
FUNCTIONS In C++.
Functions, Part 2 of 2 Topics Functions That Return a Value
Basic Elements of C++.
C Language VIVA Questions with Answers
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C Short Overview Lembit Jürimägi.
Programming Fundamentals Lecture #7 Functions
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.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
C++ for Engineers and Scientists Second Edition
Basic Elements of C++ Chapter 2.
Chapter 5 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Operator overloading Dr. Bhargavi Goswami
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
The C Language: Intro.
C++ Programming Basics
C – Programming Language
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
SPL – PS1 Introduction to C++.
INTRODUCTION TO C.
Presentation transcript:

Dr. Bhargavi Dept of CS CHRIST 9426669020 Beginning with C++ Dr. Bhargavi Dept of CS CHRIST 9426669020

Header files Comments // or /* comments */ Output Operator, bitwise shift left operator cout<<“print it”<<string; Assert.h = macros and debugging Ctype.h = test properties of function prototype Float.h = float size limit Limits.h = integral size limits Math.h = math library function Stdio.h = standard input output Stdlib.h = convert number to text, etc String.h = c-style string processing Time.h = time land date manipulation Iostream.h = standard input output functions Iomanip.h = formatting streams of data Fstream.h = input to file and output from file functions

New header files Utility = has classes and functions used by other header files Vector, list, deque, queue, set, map, stack, bitset = standard containers Functionals = algorithms of standard library Memory = allocate memory Iterator = manipulates data Algorithms = manipulate data in standard library containers Exception, stdexcept = exception handling String = class string definition Sstream = input from string to memory and output to string in memory Locale = stream processing for different language Limits = define numerical data type limits Typeinfo = run time type identification

Namespace Defines scope of identifiers used in a program. Std is namespace according to ANSI C++ standards It brings all the identifiers defined in “std” to current global scope. ‘using’ and ‘namespace’ are keywords of C++.

Return type of Main() Is Integer Therefore, every program has to end with return 0. Default return type of all the functions in C++ is ‘int’. Consequence? Warning or error. Eg. Average of Two numbers Average of Ten numbers

Variables Can be declared anywhere in the program. Must be declared before used.

Output operator cascading Cout<<“Print variable:”; Cout<<string; Cascading can be done: Cout<<“Print Variable:”<<num1<<endl; C-out Called standard output stream Represents screen We can redirect output to other devices or memory in the form of file. << is called insertion operator << is bitwise left shift operator Operator overloading can be done on it for additional functionality Works such as printf();

Input operator cascading Cin>>number1; C-in Operator >> is known as extraction or get from operator. Similar to scanf() operation >> operator can be overloaded. Eg. cin>>num1; cin>>num2;

Cascading i/0 operators Input and output operators can be cascaded. Eg: Cout<<“Sum=”<<sum<<“\n”; Cout<<“Sum”<<sum<<“\n”<< “ ,Average=”<<average<<“\n”; Output: Sum=14, Average=17;

Class Major features of C++ is a class. Binding together data and functions Is a user defined datatype Eg. 1. use of class 2. class box

Structure of c++ Program INCLUDE FILES CLASS DECLARATION MEMBER FUNCTIONS DEFINITION MAIN FUNCTION PROGRAM

END OF CHAPTER 2 THANK YOU

Tokens, expressions & control structures Dr. Bhargavi Dept of CS CHRIST 9426669020

tokens Definition? The smallest individual units in the program. How many tokens are there in C++? 5. Which are they? Keywords, Identifiers, Constant, Strings Operators.

keywords

Identifiers and constants Identifier definition? It refers to the names of variables, functions, arrays, classes, etc. Rules? Only alphabetic characters, digits and underscores Cannot start with digit Considers upper and lower case Distict Keywords cannot be identifiers Constants definition? Fixed values that do not change during execution. Literal constants do not have memory locations. What is wchar_t? Wide character literal introduced by ANSI C++. Intended for character sets that do not fit a character into single byte. It begins with letter L.

Basic data types

Size and range of data types

Storage classes AUTOMATIC EXTERNAL STATIC REGISTER LIFETIME Function block Entire program Entire Program Function Block VISIBILITY Local Global INITIAL VALUE Garbage STORAGE Stack segment Data segment CPU segment PURPOSE Local variables used by single function Global variables used throughout the program Local variables retaining values throughout the program Variables using CPU registers for storage purpose KEYWORDS auto extern static register

Dynamic initialization of variables C++ allows initialization of variables at run time. Eg. int n = strlen(string); Eg. float area = 3.14 * rad * rad; Eg. float average = sum/I;

Reference variables What is this? It’s the alias (alternative name) for previously defined variable. One memory location, two or more names. Condition? Must be initialized at the time of declaration. Represented by? & but, it is not an address operator. Use? Manipulation of objects by reference. Avoids copying and pasting of object variables to and fro. Eg. total may have an alias as sum and can refer to same variable. Syntax: data-type & reference-name = variable-name Eg. float total = 100; float & sum = total; Eg. 3_ReferenceVariable.cpp

Operators in c++

Scope resolution operator C++ is also block structured language. Use? Variables with same name can have different meaning in different blocks. Even memory location is different even though the name is same in the same program. Limitation of C: global variable cannot be accessed and manipulated within the local blocks. Resolved by C++? Yes. How? Using scope resolution operator. Eg. Check 3_ScopeResolOpr.cpp Detect output of 3_ScopeResolOprEx2.cpp Also check 3_1ScopeResolutionOperator.cpp

Member dereferencing operator Can we access class members using pointers? Yes. How? Using 3 pointer to member operators. Detailed discussion will be done during the chapter of pointers.

Memory management operator C has the concept of malloc(), calloc() and free(). Although C++ supports these function, in addition, also support unary operators for doing that task in easy and better way. How? Using new and delete. Also called free store operators. The life time of the operators are in direct control of the programmers. Syntax: pointer-variable = new data-type(value); Syntax: delete pointer-variable; For array: Syntax: pointer-variable = new data-type[size]; Syntax: delete [size]pointer-variable; Eg. 3_new-delete.cpp Eg. 3_3UseOfBad_AllocException.cpp

manipulators In detail we will study in chapter of streams and i/o manipulators. Just check the output of the program as of now. Eg. 3_manipulators.cpp

Self study 1. explicit type casting 2. control structures of If Switch Do-while While For

END OF CHAPTER 3 THANK YOU

Dr. Bhargavi Dept of CS CHRIST 9426669020 Functions in C++ Dr. Bhargavi Dept of CS CHRIST 9426669020

Function prototype It provides three information to compilers called template. Which 3 information? Name of function, number and type of arguments and type of return values. Any violation in match of these three information results to compilation error. Form: type function-name (argument-list); Eg. float vol(int x, float y, float z) { //function body } Call statements: float cube = vol(b1,w1,h1); Eg: 4_FunctionPrototype.cpp

Function arguments Call by value: passing arguments to a function copies the actual value of an argument into the formal parameter of the function. Eg. 4_CallByValue.cpp Call by pointer: passing arguments to a function copies the address of an argument into the formal parameter. Eg. 4_CallByPointer.cpp Call by reference: passing arguments to a function copies the reference of an argument into the formal parameter. Eg. 4_CallByReference.cpp Return by reference: When a function returns a reference, it returns an implicit pointer to its return value. Eg. 4_ReturnByReference.cpp

Inline function What is the objective of using function? To save memory space. But, memory space is saved at the cost execution time due to execution of long function body, pushing arguments to stack, jumping to and fro function, returning to calling function. Effect is minimum when applications are small, but affects execution time worst when application grows. Problem? Yes. Solution? 1. Macro. But, drawback is being not functions, compile time errors are not checked. 2. Inline Function.

Inline function Definition: The function that is expanded in line when it is invoked. How it works? Compiler replaces function call with corresponding function code. What is the effect? Makes program run faster. Execution time reduces. Similar to macros expansion? Yes. Then what is the advantage. Error checking at compile time is done. If its so good, y can’t we make all functions inline? If functions are large, inline effect diminish.

Inline expansion may not work in situations: Functions returning values Functions with loops, switch or goto stmts. Functions containing static variables Functions that are recursive Eg. 4_1InlineFunctions.cpp

Default arguments Can you call a function with arguments without providing the arguments? Yes or No? C++ provides this facility by Default Arguments. We can call a function without specifying all its arguments. How? Provide default values to arguments. Advantage? No need to specify arguments at each function call. Add new parameters to function without making call statements complex. Also allows to override the default value. Used to combine similar funtions. Remember. Only trailing arguments can have default values. Eg. 4_2DefultArgument.cpp

Const arguments Can arguments be constant? Yes. How. Eg. int strlen(const char *p); Eg. length(const string &s); Compiler is told by keyword ‘const’ that function should not modify the argument. Eg. 4_constant.cpp

Function overloading Define overloading. Use of same thing for different purposes. Another name? Function Polymorphism. We can design a family of function with same name but different argument list. Calculating area depending on shape. Eg. 4_5FunctionOverloading.cpp

END OF CHAPTER 4 THANK YOU