0% found this document useful (0 votes)
89 views

Copy Constructor and Recursion

Here is a program that swaps two integer values using pointers in a function: #include <iostream> using namespace std; void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a = 10, b = 20; cout << "Before swap: " << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(&a, &b); cout << "After swap: " << endl; cout << "a = " <

Uploaded by

Shehroze Talat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Copy Constructor and Recursion

Here is a program that swaps two integer values using pointers in a function: #include <iostream> using namespace std; void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a = 10, b = 20; cout << "Before swap: " << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(&a, &b); cout << "After swap: " << endl; cout << "a = " <

Uploaded by

Shehroze Talat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 4

OOP
Copy Constructor

• a copy of the object is passed or returned


• C++ creates a new object and uses a copy constructor
• copy the original object’s values into the new object
However
• copy constructors can cause serious problems when used with a
class whose data members contain pointers to dynamically
allocated memory
Example with copy constructor

#include <iostream>
using namespace std;
class queue
{
int qval;
public:
void print();
int getval();
queue(int val)
{
setval(val);
}
Example …

void setval(int v)
{
qval = v;
}
};

int queue::getval()
{
return qval;
}
Example …

int main()
{
queue obj1(5);
queue obj2 = obj1;
obj1.setval(4);
cout << obj1.getval() << " " << obj2.getval() << " " << obj1.getval();
}

Output?
Recursion

• A recursive function is a function that calls itself, either directly,


or indirectly
• A recursive function is called to solve a problem
• The function actually knows how to solve only the simplest
case(s), or so-called base case(s).
• If the function is called with a base case, the function simply
returns a result.
• If the function is called with a more complex problem, it typically
divides the problem into two conceptual pieces
Recursion

• A piece that the function knows how to do and a piece that it does
not know how to do
• So it calls itself until it reaches the simplest part of problem.
• The recursion step often includes the keyword return
• The recursion step executes while the original call to the function is
still “open,” i.e., it has not yet finished executing.
• The recursion step can result in many more such recursive calls
• In continues for the recursion to eventually terminate
• Each time the function calls itself with a slightly simpler version of
the original problem
• This sequence of smaller and smaller problems must eventually
converge on the base case.
Factorial

• The factorial of a nonnegative integer n, written n! (and


pronounced “n factorial”), is the product
• n · (n – 1) · (n – 2) · … · 1
• For example, 5! is the product 5 · 4 · 3 · 2 · 1,which is equal to
120.
Example of Reccursion

#include <iostream>
using namespace std;
//Factorial function
int f(int n) {
if (n <= 1)
{
return 1;
}
else
{
return n * f(n - 1);
}
}
Example …

int main() {
int num;
cin >> num;
cout << "Factorial of entered number: " << f(num);
return 0;
}
Fibonacci Series

• 0, 1, 1, 2, 3, 5, 8, 13, 21, …
• begins with 0 and 1 and has the property that each subsequent
Fibonacci number is the sum of the previous two Fibonacci
numbers.
Fibonacci Example

#include <iostream>
using namespace std;
int fab(int n) {
{
if ((n == 0) || (n == 1)) // base cases
return n;
else // recursion step
return fab(n - 1) + fab(n - 2);
}
}
Example

int main() {
int num2;
cin >> num2;
cout << "Fibonacci at entered number is: " << fab(num2);

return 0;
}
Exponential Using Recursion

• Write a program that will take two inputs one no. as a base value
and other as power value and calculate exponential value of the
base no. on given exponent/power value.
Difference between Iteration and Recursion

• Both iteration and recursion involve repetition


• Iteration explicitly uses a repetition structure; recursion achieves
repetition through repeated function calls
• Iteration terminates when the loop-continuation condition fails;
recursion terminates when a base case is recognized
• Recursion is helpful in writing small codes and easy to understand
as compared to iteration.
• Iteration is helpful when time complexity matters
Pointers-Key Concept

• Every byte in the computer’s memory has an address


• Addresses are numbers, just as they are for houses on a street.
• Your program, when it is loaded into memory, occupies a certain
range of these addresses.
• That means that every variable and every function in your program
starts at a particular address.
Pointers

• Pointer variables contain memory addresses as their values


• Normally, a variable directly contains a specific value.
• A pointer contains the memory address of a variable that, in turn,
contains a specific value
• a variable name directly references a value, and a pointer
indirectly references a value
• Referencing a value through a pointer is called indirection
• A pointer with the value 0 or NULL points to nothing and is known
as a null pointer
Pointers

• Pointers, like any other variables, must be declared before they


can be used
• int *countPtr, count;
• *countPtr: a pointer to an int value
• count: int value
• Each variable being declared as a pointer must be preceded by an
asterisk (*).
• Int *ptrx,*ptry;
Pointer type

• The pointers must have a type.


• Without a type, a pointer wouldn’t know how to interpret the
contents it was pointing to when it was dereferenced.
• It’s also why the type of the pointer and the variable address it’s
being assigned to must match.
• If they did not, when the pointer was dereferenced, it would
misinterpret the bits as a different type.
Simple Example

• int i = 3;
• int *iPtr;
Or
int i, *iPtr;
Pointer Operators

• The & is a unary operator that returns the memory address of its
operand
• * is a unary operator that returns the value of the variable at the
address specified by its operand
• The second operator is *, and it is a complement/inverse of &.
Pointing different values

• A pointer variable can point to different values.


• A pointer can be used to represent different variables.
Int a,b,*ptr;
a=5;
b=6;
ptr = &a;
cout << *ptr; //output 5
Ptr = &b;
Cout << *ptr;//output 6
The size of pointers

• The size of a pointer is dependent upon the architecture of


executable
• a 32-bit executable uses 32-bit memory addresses -- consequently,
a pointer on a 32- bit machine is 32 bits (4 bytes).
• With a 64-bit executable, a pointer would be 64 bits (8 bytes).
Note that this is true regardless of what is being pointed to
• However in X86 machine it can vary depending on compilers.
• In my system it is 4.
• cout << sizeof(aPtr); // output 4
Example with & and *

#include <iostream>
using namespace std;
int main() { int a, * aPtr;
a = 5;
aPtr = &a;
cout << "Adress of a " << &a; \\ 006FF9B8
cout << "Adress of a " << aPtr; \\ 006FF9B8
cout << "Adress of aPtr " << &aPtr; \\ 006FF9AC
cout << "value of aPtr" << aPtr; \\ 006FF9B
cout << "value of a" << a; \\ 5
cout << "value of a" << *(&a); \\ 5
cout << "value of a" << *aPtr; \\ 5
}
Pointers in Functions

void cube(int *v)


{
*v = *v * *v * *v; What’s different this
}
int main()
time?
{
int a, * aPtr;
a = 5;
aPtr = &a;
cube(aPtr);
cout << *aPtr;
}
Output?
Pointer in functions

• Write a program that will swap two int values in a functions using
pointers.

You might also like