Module3 ProgrammingConcepts
Module3 ProgrammingConcepts
Module3 ProgrammingConcepts
Programming Concepts
(Using C++)
Slide 1
Reference:
Slide 2
Object Oriented Programming
• Abbreviated OOP
Slide 3
OOP Characteristics
• Encapsulation
– Information hiding
– Objects contain their own data and algorithms
• Inheritance
– Writing reusable code
– Objects can inherit characteristics from other objects
• Polymorphism
– A single name can have multiple meanings depending
on its context
Slide 4
C++ History
• C developed by Dennis Ritchie at AT&T
Bell Labs in the 1970s.
– Used to maintain UNIX systems
– Many commercial applications written in c
• C++ developed by Bjarne Stroustrup at AT&T
Bell Labs in the 1980s.
– Overcame several shortcomings of C
– Incorporated object oriented programming
– C remains a subset of C++
Slide 5
C++ Basics
Variables
• Variables are like small blackboards
– We can write a number on them
– We can change the number
– We can erase the number
• C++ variables are names for memory locations
– We can write a value in them
– We can change the value stored there
– We cannot erase the memory location
• Some value is always there
Slide 7
Identifiers
• Variables names are called identifiers
• Choosing variable names
– Use meaningful names that represent data to
be stored
– First character must be
• a letter
• the underscore character
– Remaining characters must be
• letters
• numbers
• underscore character
Slide 8
Keywords
• Keywords (also called reserved words)
– Are used by the C++ language
– Must be used as they are defined in
the programming language
– Cannot be used as identifiers
Slide 9
Declaring Variables
• Before use, variables must be declared
• Declaration Examples:
– double average, m_score, total_score;
– double moon_distance;
– int age, num_students;
– int cars_waiting;
Slide 12
Assignment Statements
Slide 13
Assignment Statements
number_of_bars = number_of_bars + 3;
Slide 14
Initializing Variables
• Declaring a variable does not give it a value
– Giving a variable its first value is initializing the variable
• Variables are initialized in assignment statements
Slide 16
Include Directives
• Include Directives add library files to our programs
Flow of Control
• Flow of control
– The order in which statements are executed
• Branch
– Lets program choose between two alternatives
Slide 18
Implementing the Branch
Slide 19
Simple Loops
• When an action must be repeated, a loop is used
• C++ includes several ways to create loops
• Consider the while-loop
• Example: while (count_down > 0)
{
cout << "Hello ";
count_down -= 1;
}
• Output: Hello Hello Hello
when count_down starts at 3
Slide 20
3.2
Predefined Functions
C++ comes with libraries of predefined
functions
Slide 21
Function Calls
• sqrt(9.0) is a function call
– It invokes, or sets in action, the sqrt function
– The argument (9), can also be a variable or an
expression
• A function call can be used like any expression
– bonus = sqrt(sales) / 10;
– Cout << “The side of a square with area “ << area
<< “ is “
<< sqrt(area);
Slide 22
Function Call Syntax
• Function_name (Argument_List)
– Argument_List is a comma separated list:
#include <cmath>
• Newer standard libraries, such as cmath, also require
the directive
using namespace std;
Slide 24
Other Predefined Functions
• abs(x) --- int value = abs(-8);
– Returns absolute value of argument x
– Return value is of type int
– Argument is of type x
– Found in the library cstdlib
• fabs(x) --- double value = fabs(-8.0);
– Returns the absolute value of argument x
– Return value is of type double
– Argument is of type double
– Found in the library cmath
Slide 25
Type Casting
• Recall the problem with integer division:
int total_candy = 9, number_of_people = 4;
double candy_per_person;
candy_per_person = total_candy / number_of_people;
– candy_per_person = 2, not 2.25!
• A Type Cast produces a value of one type
from another type
– static_cast<double>(total_candy) produces a double
representing the integer value of total_candy
Slide 26
Type Cast Example
• int total_candy = 9, number_of_people = 4;
double candy_per_person;
candy_per_person = static_cast<double>(total_candy)
/ number_of_people;
– candy_per_person now is 2.25!
– This would also work:
candy_per_person = total_candy /
static_cast<double>( number_of_people);
– This would not!
candy_per_person = static_cast<double>( total_candy /
number_of_people);
Integer division occurs before type cast
Slide 27
3.3
User-Defined Functions
• Two components of a function definition
– Function declaration (or function prototype)
• Shows how the function is called
• Must appear in the code before the function can be called
• Syntax:
Type_returned Function_Name(Parameter_List);
;
//Comment describing what function does
– Function definition
• Describes how the function does its task
• Can appear before or after the function is called
• Syntax:
Type_returned Function_Name(Parameter_List)
{
//code to make the function work
}
Slide 28
Function Declaration
• Tells the return type
• Tells the name of the function
• Tells how many arguments are needed
• Tells the types of the arguments
• Tells the formal parameter names
– Formal parameters are like placeholders for the actual
arguments used when the function is called
– Formal parameter names can be any valid identifier
• Example:
double total_cost(int number_par, double price_par);
// Compute total cost including 5% sales tax on
// number_par items at cost of price_par each
Slide 29
Function Definition
• Provides the same information as the declaration
• Describes how the function does its task
function header
• Example:
double total_cost(int number_par, double price_par)
{
const double TAX_RATE = 0.05; //5% tax
double subtotal;
subtotal = price_par * number_par;
return (subtotal + subtotal * TAX_RATE);
}
function body
Slide 30
The Return Statement
• Ends the function call
• Returns the value calculated by the function
• Syntax:
return expression;
– expression performs the calculation
or
– expression is a variable containing the
calculated value
• Example:
return subtotal + subtotal * TAX_RATE;
Slide 31
3.5
Local Variables
• Variables declared in a function:
– Are local to that function, they cannot be used
from outside the function
– Have the function as their scope
• Variables declared in the main part of a
program:
– Are local to the main part of the program, they
cannot be used from outside the main part
– Have the main part as their scope
Slide 32
Global Constants
• Global Named Constant
– Available to more than one function as well as the
main part of the program
– Declared outside any function body
– Declared outside the main function body
– Declared before any function that uses it
• Example: const double PI = 3.14159;
double volume(double);
int main()
{…}
– PI is available to the main function and to function volume
Slide 33
Global Variables
Slide 34
Formal Parameters
are Local Variables
• Formal Parameters are actually variables that are
local to the function definition
– They are used just as if they were declared in the
function body
– Do NOT re-declare the formal parameters in the
function body, they are declared in the function
declaration
• The call-by-value mechanism
– When a function is called the formal parameters
are initialized to the values of the
arguments in the function call
Slide 35
Namespaces Revisited
• The start of a file is not always the best place
for
using namespace std;