Professional Documents
Culture Documents
Computer Prog6
Computer Prog6
I-INTRODUCTION:
II-OBJECTIVES:
A reference variable is an alias, that is, another name for an already existing variable. Once a reference is
initialized with a variable name or the reference name may be used to refer to the variable.
Datatype& variable_name
Datatype &variable_name
Datatype & variable_name
#include<iostream>
using namespace std;
int main()
{
int num1=3;
int b=num1;
b= 20;
Output is:
Num 1: 3
B is: 20
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
#include<iostream>
using namespace std;
int main()
{
int num1=3;
int &a = num1;
a= 20;
//or we can use num1=3;
Output is:
Num 1: 20
A is: 20
Note that you cannot declare again the same reference variable.
And the reference variable must use to refer a variable.
Example:
Int num1;
Int &a; //this is error
Int &a= num1; //this is correct
Another Example:
Now, we can use either the variable name food or the reference name meal to refer to the food variable:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza";
string &meal = food;
Output:
Pizza
Pizza
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
C++ Memory Address
Reference can also be used to get the memory address of a variable, which is the location of where the
variable is stored on the computer.
In the example from the previous page, the & operator was used to create a reference variable. But it can
also be used to get the memory address of a variable; which is the location of where the variable is stored
on the computer.
When a variable is created in C++, a memory address is assigned to the variable. And when we assign a
value to the variable, it is stored in this memory address.
To access it, use the & operator, and the result will represent where the variable is stored:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza";
Note: The memory address is in hexadecimal form (0x..). Note that you may not get the same result in
your program.
References and Pointers (which you will learn about in the next chapter) are important in C++, because
they give you the ability to manipulate the data in the computer's memory - which can reduce the code
and improve the performance.
These two features are one of the things that make C++ stand out from other programming languages,
like Python and Java.
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
C++ Pointers
A pointer variable points to a data type (like int or string) of the same type, and is created with
the * operator. The address of the variable you're working with is assigned to the pointer:
You learned from the previous chapter, that we can get the memory address of a variable by using
the & operator:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza"; // A string variable
string* ptr = &food; // A pointer variable that stores the address of food
return 0;
}
Output:
Pizza
0x6dfed4 (memory address in computer)
0x6dfed4 (memory address in computer)
Example explained
Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk
sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're working
with.
Use the & operator to store the memory address of the variable called food, and assign it to the pointer.
Another Example:
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
#include<iostream>
using namespace std;
int main()
{
int num1 = 3;
int *n = &num1; // pointer variable is *n and assign pointer to num1 at the same time with
//reference getting address it is required because pointer holds memory address
cout<<"Num 1 is:"<<num1<<endl;
cout<<"PTR is :"<<n<<endl;
return 0;
Output:
Num 1 is: 3
PTR is : (memory address ex. 11223344)
Another Example:
#include<iostream>
using namespace std;
int main()
{
int num1 = 3;
int *n = &num1;
num1=10;
cout<<"Num 1 is:"<<num1<<endl;
cout<<"PTR is:"<<n<<endl;
return 0;
Output:
Num 1 is: 10
PTR is : (memory address ex. 11223344)
Another Example:
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
#include<iostream>
using namespace std;
int main()
{
int num1 = 3;
int num2 = 7;
int num3 = 14;
int *n = &num1;
*n = 10;
n= &num3;
num3 = 17;
cout<<"Num 1 is:"<<num1<<endl;
cout<<"Num 2 is:"<<num2<<endl;
cout<<"Num 3 is:"<<num3<<endl;
cout<<"PTR num1 is :"<<&num1<<endl;
cout<<"PTR num2 is :"<<&num2<<endl;
cout<<"PTR num3 is :"<<&num3<<endl;
return 0;
Output:
Num 1 is: 10
Num 1 is: 15
Num 1 is: 17
Another Example:
#include<iostream>
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
using namespace std;
int main()
{
int num1 = 15;
int *n = &num1;
}
Output: 325
C++ Dereference
You can also use the pointer to get the value of the variable, by using the * operator
(the dereference operator):
Example
#include<iostream>
using namespace std;
int main()
{
int num1 = 3;
int *n = &num1;
num1=10;
cout<<"Num 1 is:"<<num1<<endl;
cout<<"PTR is:"<<*n<<endl; //used asterisk when you don’t want to output the memory //address
return 0;
Output:
Num 1 is: 10
PTR is : 10
Note that the * sign can be confusing here, as it does two different things in our code:
C++ Structure
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
Structures (also called structs) are a way to group several related variables into one place. Each variable
in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, string, bool, etc.).
Create a Structure
To create a structure, use the struct keyword and declare each of its members inside curly braces.
After the declaration, specify the name of the structure variable (myStructure in the example below):
Example
#include <iostream>
using namespace std;
struct person{
string name;
int age;
string address;
};
int main() {
person myProfile;
myProfile.name="Elsa";
myProfile.age=28;
myProfile.address="Appas";
Output:
My name is: Elsa
Age: 28
Address: Appas
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)
SAINT TONIS COLLEGE, INC.
(Formerly: Kalinga Christian Learning Center)
United Church of Christ in the Philippines
Purok 4, Bulanao Centro, Tabuk City, Kalinga
Philippines 3800
Tel. No. (074) 627-5930, Email Address: sainttoniscollege@yahoo.com
Example:
#include <iostream>
using namespace std;
struct person{
string name;
int age;
string address;
};
int main() {
person Profile;
Profile.name="Elsa";
Profile.age=28;
Profile.address="Appas";
person Profile2;
Profile2.name="Alen";
Profile2.age=30;
Profile2.address="Tabuk";
return 0;
}
Output:
______________________________________________________________________________
An institution of the United Church of Christ in the Philippines (UCCP)
Member: Association of Christian Schools, Colleges and Universities (ACSCU)
UCCP Church Related Educational Action Towards Empowerment (UCCP CREATE)
Cordillera Schools Group (CSG)