Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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
Name:
Grade/section: BSIT-I
Subject: COMPUTER PROGRAMMING1
Module No: 6
TERM MIDTERM

I-INTRODUCTION:

II-OBJECTIVES:

III- LESSON PROPER: C++ References

 Creating C++ References

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.

A reference variable is a "reference" to an existing variable, and it is created with the (Ampersand


(&)) operator:

Format in using Ampersand symbol (&)

Datatype& variable_name
Datatype &variable_name
Datatype & variable_name

Example without using reference variable:

#include<iostream>
using namespace std;

int main()
{
int num1=3;
int b=num1;

b= 20;

cout<<"Num 1:"<<num1 <<endl;


cout<<"B is:"<<b<<endl;
}

Output is:
Num 1: 3
B is: 20

Then Example with Reference:

______________________________________________________________________________
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;

cout<<"Num 1:"<<num1 <<endl;


cout<<"A is:" <<a <<endl;
}

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:

string food = "Pizza";  // food variable


string &meal = food;    // reference to food

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;

cout << food << "\n";


cout << meal << "\n";
return 0;
}

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";

cout << &food;


return 0;
}

Output: 0x6dfed4 (memory address in computer)

Note: The memory address is in hexadecimal form (0x..). Note that you may not get the same result in
your program.

And why is it useful to know the memory address?

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, is a variable that hold the memory address as its value.

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

cout << food << "\n"; // Output the value of food


cout << &food << "\n"; // Output the memory address of food
cout << ptr << "\n"; // Output the memory address of food with the pointer

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.

Now, ptr holds the value of food's memory address.

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= &num2; //can use the pointer n many times


num2= 15;

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

PTR num 1 is: (memory address of num1 ex. 11223344)


PTR num 1 is: (memory address of num2 ex. 11223355)
PTR num 1 is: (memory address of num3 ex. 11223366)

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;

num1 =*n + 10; //15+10= 25


num1 = *n + 100; //25 +100= 125
num1 = *n +200; //125+300 = 325

cout<<num1; //*n same result


return 0;

}
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:

 When used in declaration (string* ptr), it creates a pointer variable.


 When not used in declaration, it act as a dereference operator.

 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):

struct {             // Structure declaration


  int myNum;         // Member (int variable)
  string myString;   // Member (string variable)
} myStructure;       // Structure variable

 Access Structure Members

To access members of a structure, use the dot syntax (.):

Example

Assign data to members of a structure and print it:

#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";

cout <<"My name is: "<< myProfile.name << "\n";


cout << "Age: "<<myProfile.age << "\n";
cout <<"Address: "<< myProfile.address;
return 0;
}

Output:
My name is: Elsa
Age: 28
Address: Appas

 One Structure in Multiple Variables

______________________________________________________________________________
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";

cout <<"My name is: "<< Profile.name << "\n";


cout << "Age: "<<Profile.age << "\n";
cout <<"Address: "<< Profile.address<< "\n"<< "\n"<< "\n";

cout <<"My name is: "<< Profile2.name << "\n";


cout << "Age: "<<Profile2.age << "\n";
cout <<"Address: "<< Profile2.address;

return 0;
}

Output:

My name is: Elsa


Age: 28
Address: Appas

My name is: Alen


Age: 30
Address: Tabuk

______________________________________________________________________________
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)

You might also like