C++ Basics
What are the reasons for problems with procedural languages? • First, functions have unrestricted access to global data • Second, unrelated functions and data.
3 Characteristics of OOPL • Encapsulation: Combining data structure with actions – Data structure: represents the properties, the state, or characteristics of objects – Actions: permissible behaviors that are controlled through the member functions Data hiding: Process of making certain data inaccessible • Inheritance: Ability to derive new objects from old ones – permits objects of a more specific class to inherit the properties (data) and behaviors (functions) of a more general/base class – ability to define a hierarchical relationship between objects • Polymorphism: Ability for different objects to interpret functions differently
Reading and Writing Data • Cout is a predefined object and represents the standard output stream and this output stream represents the screen. • E.g. cout<<“ I love india”; cout will display this string as such on screen.
• << is called insertion or put to operator. • It is also called bit-wise left -shift operator • if string is variable then cout can be used to display the contents of string. E.g. cout<< string; • cin is used to read the data. • cin>>a; • >> is called extraction operator.
6 Data Types C++ Data types (built-in data types) •Integers •Floating-point numbers •Characters •And more •The data type determines how data is represented in the computer and the kind of processing that the computer can perform on it •The number of bytes that a data type occupies in memory is system dependent
7 Arithmetic Operators1/3 Operators Unary operator: One operand Binary operator: two operands + Unary Plus + Addition - Unary minus - Subtraction * Multiplication / floating-point division or integer division (no fractional part) % modulus operator (remainder of integer division) Examples Area_triangle = 0.5 *base * height; Y = -x;
OPERATORS in C++ 1. Arithmetic Operators + (Addition), - (subtraction), * (Multiplication), / (Division), % (Modulus) 2. Assignment Operators =, +=, -=, *=, /=, !=,/=
Operators contd… 3.Relational and Logical Operators < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to) b) Equality Operator:- == (equal to), != (not equal to) c) Logical operators:- && (logical AND), || (logical OR), ! (NOT)
Operators(contd…) • 4. Bitwise Logical Operators Eg:- bitwise compliment Left shift Right shift Bitwise AND (&) Bitwise OR (|) Bitwise Exclusive OR (^)
Ternary Operator (?: ) • exp1 ? Exp2 : exp3 • Eg:- x=10; Y=x>9?100:200
Sizeof Operator • This operator tells about the size in byte of whatever type specified. e.g. sizeof (int). • In case of pc size of int 2 bytes. • X=sizeof (int) saves the value in integer type.
Storage Class • Each variable has a storage class. – Determines the period during which the variable exists in memory. – Some variables are created only once (memory is set aside to hold the variable value) • Global variables are created only once. – Some variables are re-created many times • Local variables are re-created each time a function is called. 13
Storage Classes • auto – created each time the block in which they exist is entered. • register – same as auto, but tells the compiler to make as fast as possible. • static – created only once, even if it is a local variable. • extern – global variable declared elsewhere. 14
Specifying Storage Class auto int j; register int i_need_to_be_fast; static char remember_me; extern double a_global; 15
Introduction to Functions • A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. • This is called structured programming. • These parts are sometimes made into functions in C++. • main() then uses these functions to solve the original problem.
Advantages of Functions • Functions separate the concept (what is done) from the implementation (how it is done). • Functions make programs easier to understand. • Functions can be called several times in the same program, allowing the code to be reused.
Sample function int add2ints(int a, int b) { return(a+b); } Returntype Function name parameters Function body
Call-by-value vs. Call-by-reference • So far we looked at functions that get a copy of what the caller passed in. – This is call-by-value, as the value is what gets passed in (the value of a variable). • We can also define functions that are passed a reference to a variable. – This is call-by-reference, the function can change a callers variables directly. 19
Overloading in C++ What is overloading – Overloading means assigning multiple meanings to a function name or operator symbol – It allows multiple definitions of a function with the same name, but different signatures. C++ supports – Function overloading – Operator overloading
Function Overloading • Two or more functions can have the same name but different parameters • Example: int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; }
Scope Rules • The scope of a variable is the portion of a program where the variable has meaning (where it exists). • A global variable has global (unlimited) scope. • A local variable’s scope is restricted to the function that declares the variable. • A block variable’s scope is restricted to the block in which the variable is declared.
Understanding Scope • Some variables can be accessed throughout an entire program, while others can be accessed only in a limited part of the program • The scope of a variable defines where it can be accessed in a program • To adequately understand scope, you must be able to distinguish between local and global variables
Local variables • Parameters and variables declared inside the definition of a function are local. • They only exist inside the function body. • Once the function returns, the variables no longer exist! – That’s fine! We don’t need them anymore!
Block Variables • You can also declare variables that exist only within the body of a compound statement (a block): { int f; … … }
Global variables • You can declare variables outside of any function definition – these variables are global variables. • Any function can access/change global variables. • Example: flag that indicates whether debugging information should be printed.
Distinguishing Between Local and Global Variables • Celebrity names are global because they are known to people everywhere and always refer to those same celebrities • Global variables are those that are known to all functions in a program • Some named objects in your life are local • You might have a local co-worker whose name takes precedence over, or overrides, a global one
Distinguishing Between Local and Global Variables • Variables that are declared in a block are local to that block and have the following characteristics: – Local variables are created when they are declared within a block – Local variables are known only to that block – Local variables cease to exist when their block ends
Distinguishing Between Local and Global Variables • Variables declared within a function remain local to that function • In contrast, variables declared within curly braces within any function are local to that block
Recursion • Functions can call themselves! This is called recursion. • Recursion is very useful – it’s often very simple to express a complicated computation recursively. 32
A Better Example - Computing Factorials int factorial( int x ) { if (x == 1) return(1); else return(x * factorial(x-1)); } 33
Inline Functions • Each time you call a function in a C++ program, the computer must do the following: – Remember where to return when the function eventually ends – Provide memory for the function’s variables – Provide memory for any value returned by the function – Pass control to the function – Pass control back to the calling program • This extra activity constitutes the overhead, or cost of doing business, involved in calling a function
Using an Inline Function
Inline Functions • An inline function is a small function with no calling overhead • Overhead is avoided because program control never transfers to the function • A copy of the function statements is placed directly into the compiled calling program • The inline function appears prior to the main(), which calls it • Any inline function must precede any function that calls it, which eliminates the need for prototyping in the calling function
Inline Functions • When you compile a program, the code for the inline function is placed directly within the main() function • You should use an inline function only in the following situations: – When you want to group statements together so that you can use a function name – When the number of statements is small (one or two lines in the body of the function) – When the function is called on few occasions
Basics of cpp

Basics of cpp

  • 1.
  • 2.
    What are thereasons for problems with procedural languages? • First, functions have unrestricted access to global data • Second, unrelated functions and data.
  • 3.
    3 Characteristics of OOPL •Encapsulation: Combining data structure with actions – Data structure: represents the properties, the state, or characteristics of objects – Actions: permissible behaviors that are controlled through the member functions Data hiding: Process of making certain data inaccessible • Inheritance: Ability to derive new objects from old ones – permits objects of a more specific class to inherit the properties (data) and behaviors (functions) of a more general/base class – ability to define a hierarchical relationship between objects • Polymorphism: Ability for different objects to interpret functions differently
  • 4.
    Reading and WritingData • Cout is a predefined object and represents the standard output stream and this output stream represents the screen. • E.g. cout<<“ I love india”; cout will display this string as such on screen.
  • 5.
    • << iscalled insertion or put to operator. • It is also called bit-wise left -shift operator • if string is variable then cout can be used to display the contents of string. E.g. cout<< string; • cin is used to read the data. • cin>>a; • >> is called extraction operator.
  • 6.
    6 Data Types C++ Datatypes (built-in data types) •Integers •Floating-point numbers •Characters •And more •The data type determines how data is represented in the computer and the kind of processing that the computer can perform on it •The number of bytes that a data type occupies in memory is system dependent
  • 7.
    7 Arithmetic Operators1/3 Operators Unary operator:One operand Binary operator: two operands + Unary Plus + Addition - Unary minus - Subtraction * Multiplication / floating-point division or integer division (no fractional part) % modulus operator (remainder of integer division) Examples Area_triangle = 0.5 *base * height; Y = -x;
  • 8.
    OPERATORS in C++ 1.Arithmetic Operators + (Addition), - (subtraction), * (Multiplication), / (Division), % (Modulus) 2. Assignment Operators =, +=, -=, *=, /=, !=,/=
  • 9.
    Operators contd… 3.Relational andLogical Operators < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to) b) Equality Operator:- == (equal to), != (not equal to) c) Logical operators:- && (logical AND), || (logical OR), ! (NOT)
  • 10.
    Operators(contd…) • 4. BitwiseLogical Operators Eg:- bitwise compliment Left shift Right shift Bitwise AND (&) Bitwise OR (|) Bitwise Exclusive OR (^)
  • 11.
    Ternary Operator (?:) • exp1 ? Exp2 : exp3 • Eg:- x=10; Y=x>9?100:200
  • 12.
    Sizeof Operator • Thisoperator tells about the size in byte of whatever type specified. e.g. sizeof (int). • In case of pc size of int 2 bytes. • X=sizeof (int) saves the value in integer type.
  • 13.
    Storage Class • Eachvariable has a storage class. – Determines the period during which the variable exists in memory. – Some variables are created only once (memory is set aside to hold the variable value) • Global variables are created only once. – Some variables are re-created many times • Local variables are re-created each time a function is called. 13
  • 14.
    Storage Classes • auto– created each time the block in which they exist is entered. • register – same as auto, but tells the compiler to make as fast as possible. • static – created only once, even if it is a local variable. • extern – global variable declared elsewhere. 14
  • 15.
    Specifying Storage Class autoint j; register int i_need_to_be_fast; static char remember_me; extern double a_global; 15
  • 16.
    Introduction to Functions •A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. • This is called structured programming. • These parts are sometimes made into functions in C++. • main() then uses these functions to solve the original problem.
  • 17.
    Advantages of Functions •Functions separate the concept (what is done) from the implementation (how it is done). • Functions make programs easier to understand. • Functions can be called several times in the same program, allowing the code to be reused.
  • 18.
    Sample function int add2ints(inta, int b) { return(a+b); } Returntype Function name parameters Function body
  • 19.
    Call-by-value vs. Call-by-reference • Sofar we looked at functions that get a copy of what the caller passed in. – This is call-by-value, as the value is what gets passed in (the value of a variable). • We can also define functions that are passed a reference to a variable. – This is call-by-reference, the function can change a callers variables directly. 19
  • 20.
    Overloading in C++ Whatis overloading – Overloading means assigning multiple meanings to a function name or operator symbol – It allows multiple definitions of a function with the same name, but different signatures. C++ supports – Function overloading – Operator overloading
  • 22.
    Function Overloading • Twoor more functions can have the same name but different parameters • Example: int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; }
  • 24.
    Scope Rules • Thescope of a variable is the portion of a program where the variable has meaning (where it exists). • A global variable has global (unlimited) scope. • A local variable’s scope is restricted to the function that declares the variable. • A block variable’s scope is restricted to the block in which the variable is declared.
  • 25.
    Understanding Scope • Somevariables can be accessed throughout an entire program, while others can be accessed only in a limited part of the program • The scope of a variable defines where it can be accessed in a program • To adequately understand scope, you must be able to distinguish between local and global variables
  • 26.
    Local variables • Parametersand variables declared inside the definition of a function are local. • They only exist inside the function body. • Once the function returns, the variables no longer exist! – That’s fine! We don’t need them anymore!
  • 27.
    Block Variables • Youcan also declare variables that exist only within the body of a compound statement (a block): { int f; … … }
  • 28.
    Global variables • Youcan declare variables outside of any function definition – these variables are global variables. • Any function can access/change global variables. • Example: flag that indicates whether debugging information should be printed.
  • 29.
    Distinguishing Between Local andGlobal Variables • Celebrity names are global because they are known to people everywhere and always refer to those same celebrities • Global variables are those that are known to all functions in a program • Some named objects in your life are local • You might have a local co-worker whose name takes precedence over, or overrides, a global one
  • 30.
    Distinguishing Between Local andGlobal Variables • Variables that are declared in a block are local to that block and have the following characteristics: – Local variables are created when they are declared within a block – Local variables are known only to that block – Local variables cease to exist when their block ends
  • 31.
    Distinguishing Between Local andGlobal Variables • Variables declared within a function remain local to that function • In contrast, variables declared within curly braces within any function are local to that block
  • 32.
    Recursion • Functions cancall themselves! This is called recursion. • Recursion is very useful – it’s often very simple to express a complicated computation recursively. 32
  • 33.
    A Better Example- Computing Factorials int factorial( int x ) { if (x == 1) return(1); else return(x * factorial(x-1)); } 33
  • 34.
    Inline Functions • Eachtime you call a function in a C++ program, the computer must do the following: – Remember where to return when the function eventually ends – Provide memory for the function’s variables – Provide memory for any value returned by the function – Pass control to the function – Pass control back to the calling program • This extra activity constitutes the overhead, or cost of doing business, involved in calling a function
  • 35.
  • 36.
    Inline Functions • Aninline function is a small function with no calling overhead • Overhead is avoided because program control never transfers to the function • A copy of the function statements is placed directly into the compiled calling program • The inline function appears prior to the main(), which calls it • Any inline function must precede any function that calls it, which eliminates the need for prototyping in the calling function
  • 37.
    Inline Functions • Whenyou compile a program, the code for the inline function is placed directly within the main() function • You should use an inline function only in the following situations: – When you want to group statements together so that you can use a function name – When the number of statements is small (one or two lines in the body of the function) – When the function is called on few occasions

Editor's Notes

  • #34 Available on the course home page in code/functions/factorias.cpp