SlideShare a Scribd company logo
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
Basics of cpp
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;
}
Basics of cpp
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

More Related Content

PPTX
Modular programming
PDF
Python algorithm
PPTX
User defined functions
PPTX
PDF
Data Structure with C
PPTX
User defined functions in C
PDF
Lecture21 categoriesof userdefinedfunctions.ppt
Modular programming
Python algorithm
User defined functions
Data Structure with C
User defined functions in C
Lecture21 categoriesof userdefinedfunctions.ppt

What's hot (20)

PPT
user defined function
PPTX
Modular programming
PPTX
FUNCTION CPU
PDF
Programming in c by pkv
PPTX
PPTX
structured programming
PPTX
Plc part 3
PDF
Functions
PPTX
Qbesic programming class 9
PDF
Python recursion
PPTX
predefined and user defined functions
PPTX
Functions
PPTX
Functions in C
PPTX
Modular programming
PPTX
Functions in C
PDF
Pointers and call by value, reference, address in C
PDF
9 subprograms
PDF
Functional programming in python
PPTX
Functional programming in python
DOC
c.p function
user defined function
Modular programming
FUNCTION CPU
Programming in c by pkv
structured programming
Plc part 3
Functions
Qbesic programming class 9
Python recursion
predefined and user defined functions
Functions
Functions in C
Modular programming
Functions in C
Pointers and call by value, reference, address in C
9 subprograms
Functional programming in python
Functional programming in python
c.p function
Ad

Viewers also liked (6)

PPTX
classes & objects in cpp
PPTX
Classes And Objects
PPTX
class and objects
PPTX
Classes, objects in JAVA
PPT
Oops ppt
PPT
Object Oriented Programming Concepts
classes & objects in cpp
Classes And Objects
class and objects
Classes, objects in JAVA
Oops ppt
Object Oriented Programming Concepts
Ad

Similar to Basics of cpp (20)

PPTX
CPP07 - Scope
PPTX
Chapter One Function.pptx
PDF
Python functions
PDF
3-Python Functions.pdf in simple.........
PPTX
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPTX
Polymorphism
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
Functions and structure in programming c
PPTX
CPP06 - Functions
PPTX
Functions_new.pptx
PDF
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
PPTX
Functions.pptx
PPTX
Function in C Programming
PPTX
Funtions of c programming. the functions of c helps to clarify all the tops
PDF
PPTX
CH.4FUNCTIONS IN C (1).pptx
CPP07 - Scope
Chapter One Function.pptx
Python functions
3-Python Functions.pdf in simple.........
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
Polymorphism
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Functions and structure in programming c
CPP06 - Functions
Functions_new.pptx
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
Functions.pptx
Function in C Programming
Funtions of c programming. the functions of c helps to clarify all the tops
CH.4FUNCTIONS IN C (1).pptx

Recently uploaded (20)

PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Open Quiz Monsoon Mind Game Prelims.pptx
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
NOI Hackathon - Summer Edition - GreenThumber.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Anesthesia in Laparoscopic Surgery in India
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O5-L3 Freight Transport Ops (International) V1.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
Open folder Downloads.pdf yes yes ges yes

Basics of cpp

  • 2. What are the reasons 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 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.
  • 5. • << 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. 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. 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 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)
  • 10. Operators(contd…) • 4. Bitwise Logical 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 • 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.
  • 13. 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
  • 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 auto int 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(int a, int b) { return(a+b); } Returntype Function name parameters Function body
  • 19. 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
  • 20. 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
  • 22. 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; }
  • 24. 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.
  • 25. 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
  • 26. 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!
  • 27. Block Variables • You can also declare variables that exist only within the body of a compound statement (a block): { int f; … … }
  • 28. 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.
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 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 • 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
  • 35. Using an Inline Function
  • 36. 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
  • 37. 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

Editor's Notes

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