Introduction To OOP & Functions in C++
Introduction To OOP & Functions in C++
Building Blocks of OOP: Objects & Classes Building Blocks of OOP: Objects & Classes
●Object: models a
● Real world object (ex. computer, book, box)
● Concept (ex. meeting, interview)
● Class has
● Process (ex. sorting a stack of papers or comparing two ● Set of attributes or properties that describes every object
computers to measure their performance) ● Set of behavior or actions that every object can perform
Object: FordCar1
Object: Person1
Attributes Behavior
Color: Yellow Start, Accelerate,
Reverse, Stop Class: Person Attributes Behavior
Class: FordCar Type: Coupe
Name: Ann Speak,
Model: Mustang
Attributes Listen, Eat,
Attributes Cylinder: 6 Height: 5’ 4”
Run, Walk
Name, Height, Age Age: 21
Color, Type, Model, Cylinder
Behavior
Behavior
Speak, Listen, Eat, Run, Walk Object: Person2
Start, Accelerate, Reverse, Stop
Object: FordCar2
Attributes Behavior
Attributes Behavior
Name: Adam Speak,
Color: Orange Start, Accelerate, Listen, Eat,
Reverse, Stop Height: 5’ 9”
Type: Coupe Run, Walk
Age: 24
Model: Focus
Cylinder: 4
1
Class Class ShippingBox
sender_name : string
receiver_name : string
cost_per_pound : int int shipping_cost() {
weight : int return cost_per_pound*weight;
}
shipping_cost() : int
Class
ShippingBox
Object BoxB
sender_name = Jim
receiver_name = John
cost_per_pound = 5
weight = 10
shipping_cost()
2
Abstraction Abstraction
Encapsulation
Inheritance
that manipulate the data into one unit ●Create new classes (derived classes) from existing
classes (base classes)
●Objects & Classes enforce encapsulation
●The derived class inherits the variables and functions of
sender_name
receiver_name
cost_per_pound
weight
shipping_cost()
3
20
• Recursive functions
●Initial extra effort needed in accurately modeling the – Are functions that calls themselves
classes and sub-classes for a problem – Can only solve a base case
●Suited for modeling certain real world problems as – If not base case, the function breaks the problem into a
opposed to some others slightly smaller, slightly simpler, problem that resembles the
original problem and
• Launches a new copy of itself to work on the smaller problem,
slowly converging towards the base case
• Makes a call to itself inside the return statement
– Eventually the base case gets solved and then that value
works its way back up to solve the whole problem
21 22
11.1 Recursion
• Example: factorial
n! = n * ( n – 1 ) * ( n – 2 ) * … * 1
– Recursive relationship ( n! = n * ( n – 1 )! )
5! = 5 * 4!
4! = 4 * 3!…
– Base case (1! = 0! = 1)
23 24
1 // Fig. 11.1: fig06_29.cpp Outline
2 // Testing the recursive factorial function.
3 #include <iostream>
4 using std::cout; Only the base cases return values. 1. Function prototype
5 using std::endl;
6 All other cases call the factorial
7 #include <iomanip>
8 using std::setw;
function again.
9
1.1 Initialize variables
10 unsigned long factorial( unsigned long ); // function prototype
11
12 int main()
13 {
2. Input an integer
14 // calculate the factorials of 0 through 10
15 for ( int counter = 0; counter <= 10; counter++ )
16 cout << setw( 2 ) << counter << "! = " << factorial( counter ) 2.1 Call function
17 << endl;
1819 return 0; // indicates successful termination factorial
20 } // end main
21
22 // recursive definition of function factorial
23 unsigned long factorial( unsigned long number ) 2.2 Output results.
24 {
25 if ( number <= 1 ) // test for base case
26
27
return 1; // base cases: 0! = 1 and 1! = 1
else // recursion step
11. Define factorial
28 return number * factorial( number - 1 ); recursively
29 } // end function factorial
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
4
25 26
29 30
Outline
The default box volume is: 1
11.5 Unary Scope Resolution Operator
The volume of a box with length 10,
width 1 and height 1 is: 10
Program Output
The volume of a box with length 10, • Unary scope resolution operator (::)
width 5 and height 1 is: 50
5
1 // Fig. 11.5: fig11_24.cpp 31 32
2 // Using the unary scope resolution operator Outline
3
4
#include <iostream>
11.6 Function Overloading
5 using std::cout; 1. Define variables
6 using std::endl;
7
2. Print variables
• Function overloading
8 #include <iomanip>
9 – Having functions with same name and different parameters
10 using std::setprecision;
11 – Should perform similar tasks ( i.e., a function to square
12 const double PI = 11.14159265358979; ints, and function to square floats).
13
14 int main() int square( int x) {return x * x;}
15 {
16 const float PI = static_cast< float >( ::PI );
float square(float x) { return x * x; }
Notice the use of ::
17
18 cout << setprecision( 20 )
– Program chooses function by signature
19 << " Local float value of PI = " << PI • signature determined by function name and parameter types
20 << "\nGlobal double value of PI = " << ::PI << endl;
21 – Can have the same return types
22 return 0;
23 }
Program Output
Local float value of PI = 11.141592741012573242
Global double value of PI = 11.141592653589790007