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

OOP Lecture 2

The document discusses object-oriented paradigm and concepts like functions, pointers, call by value vs call by reference. It provides examples of declaring and defining functions, using pointers by assigning addresses to pointer variables and accessing values using pointers. It also shows an example of passing arguments by reference using pointers.

Uploaded by

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

OOP Lecture 2

The document discusses object-oriented paradigm and concepts like functions, pointers, call by value vs call by reference. It provides examples of declaring and defining functions, using pointers by assigning addresses to pointer variables and accessing values using pointers. It also shows an example of passing arguments by reference using pointers.

Uploaded by

ayesha sabir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OBJECT-ORIENTED Lecture 1

PARADIGM Instructor : Syeda Hira Fatima


REVIEW OF BASICS
Functions in c++:
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are
important for reusing code: Define the code once, and use it many
times.
void myFunction() {
// code to be executed
}

Example Explained
•myFunction() is the name of the function
•void means that the function does not have a return value. You will learn more about return values later in the next chapter
•inside the function (the body), add code that defines what the function should do
Declared functions are not executed immediately. They are "saved
for later use", and will be executed later, when they are called.
To call a function, write the function's name followed by two
parentheses and a semicolon.

Task 1:
Write a code for calculator that can preform some basic arithmetic’s
i.e. +,-,*,/
A C++ function consist of two parts:
•Declaration: the return type, the name of the function, and
parameters (if any)
•Definition: the body of the function (code to be executed)

If a user-defined function, such as myFunction() is declared after the main() function, an error will occur:
What is the difference between call by value and call by reference?
POINTERS
Pointers are symbolic representations of addresses. They enable programs to
simulate call-by-reference as well as to create and manipulate dynamic data
structures. Iterating over elements in arrays or other data structures is one of the
main use of pointers.
The address of the variable you’re working with is assigned to the pointer variable
that points to the same data type (such as an int or string).

Syntax:
datatype *var_name; int *ptr; /
How to use a pointer?
•Define a pointer variable
•Assigning the address of a variable to a pointer using the unary operator (&) which
returns the address of that variable.
•Accessing the value stored in the address using unary operator (*) which returns the
value of the variable located at the address specified by its operand.
The reason we associate data type with a pointer is that it knows how many bytes
the data is stored in. When we increment a pointer, we increase the pointer by the
size of the data type to which it points.
#include <bits/stdc++.h>
using namespace std;
void Fun()
{
int var = 20;

// declare pointer variable


int* ptr;

// note that data type of ptr and var must be same


ptr = &var;

// assign the address of a variable to a pointer


cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
}
// Driver program
int main()
{
Fun();
return 0;
}
PASS BY REFERENCE BY
POINTER ARGUMENTS
void square2(int* n)
{
// Address of n in
square2() is the same as
n2 in main() int n2 = 8;
cout << "address of cout << "address of n2 in main(): " << &n2 << "\n";
n2 in square2(): " << n << square2(&n2);
"\n"; cout << "Square of n2: " << n2 << "\n";
cout << "Change reflected in n2: " << n2 << "\n";
// Explicit de-
referencing to get the
value pointed-to
*n *= *n;
}

You might also like