1 / 38

Beginning with C++

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

williama
Download Presentation

Beginning with C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

  7. 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();

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

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

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

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

  12. END OF CHAPTER 2 THANK YOU

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

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

  15. keywords

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

  17. Basic data types

  18. Size and range of data types

  19. Storage classes

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

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

  22. Operators in c++

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

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

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

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

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

  28. END OF CHAPTER 3 THANK YOU

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

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

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

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

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

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

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

  36. Const arguments • Can arguments be constant? • Yes. • How. • Eg. intstrlen(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

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

  38. END OF CHAPTER 4 THANK YOU

More Related