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

OOP Lab 03

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OOP Lab 03

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIVERSITY OF CHAKWAL

DEPARTMENT OF COMPUTER SCIENCE


(Object Oriented Programming)

Lab 03

C++ Arrays, Structures


and Pointers

Lab Instructor: Engr. Samina Bilquees


UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)

Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value. To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:

string cars[4];

We have now declared a variable that holds an array of four strings. To insert values to it, we can
use an array literal - place the values in a comma-separated list, inside curly braces:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of three integers, you could write:

int myNum[3] = {10, 20, 30};

How to access elements of array?

You access an array element by referring to the index number inside square brackets [].

This statement accesses the value of the first element in cars:

Example

#include <iostream>

#include <string>

using namespace std;

int main() {

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
cout << cars[0];

return 0;

Example

#include <iostream>

#include <string>

using namespace std;

int main() {

string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};

for (int i = 0; i < 5; i++) {

cout << cars[i] << "\n";

return 0;

Change the Array Element


To change the value of a specific element, refer to the index number.
Example:
#include <iostream>
#include <string>
using namespace std;
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}

Multi-Dimensional Arrays

A multi-dimensional array is an array of arrays.

To declare a multi-dimensional array, define the variable type, specify the name of the array followed
by square brackets which specify how many elements the main array has, followed by another set of
square brackets which indicates how many elements the sub-arrays have:

string letters[2][4];

As with ordinary arrays, you can insert values with an array literal - a comma-separated list inside
curly braces. In a multi-dimensional array, each element in an array literal is another array literal.

string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};

Each set of square brackets in an array declaration adds another dimension to an array. An array like
the one above is said to have two dimensions.

Access an element of multi-dimensional array


UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
To access an element of a multi-dimensional array, specify an index number in each of the array's
dimensions.

This statement accesses the value of the element in the first row (0) and third column (2) of
the letters array.

Example

#include <iostream>

using namespace std;

int main() {

string letters[2][4] = {

{ "A", "B", "C", "D" },

{ "E", "F", "G", "H" }

};

cout << letters[0][2];

return 0;

Change Elements in a Multi-Dimensional Array


To change the value of an element, refer to the index number of the element in each of the
dimensions:

#include <iostream>

using namespace std;


UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
int main() {

string letters[2][4] = {

{ "A", "B", "C", "D" },

{ "E", "F", "G", "H" }

};

letters[0][0] = "Z";

cout << letters[0][0];

return 0;

Loop through a Multi-Dimensional Array


To loop through a multi-dimensional array, you need one loop for each of the array's dimensions.

The following example outputs all elements in the letters array:

#include <iostream>

using namespace std;

int main() {

string letters[2][4] = {

{ "A", "B", "C", "D" },

{ "E", "F", "G", "H" }

};

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 4; j++) {


UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
cout << letters[i][j] << "\n";

return 0;

C++ Structures

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
#include <iostream>
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
#include <string>
using namespace std;

int main() {
struct {
int myNum;
string myString;
} myStructure;
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
cout << myStructure.myNum << "\n";
cout << myStructure.myString << "\n";
return 0;
}

One Structure in Multiple Variables


You can use a comma (,) to use one structure in many variables:

#include <iostream>
#include <string>
using namespace std;

int main() {
struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here

// Put data into the first structure


myCar1.brand = "BMW";
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
myCar1.model = "X5";
myCar1.year = 1999;

// Put data into the second structure


myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;

// Print the structure members


cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";
return 0;
}

Named Structures
By giving a name to the structure, you can treat it as a data type. This means that you can create
variables with this structure anywhere in the program at any time.

To create a named structure, put the name of the structure right after the struct keyword:

struct myDataType { // This structure is named "myDataType"


int myNum;
string myString;
};

To declare a variable that uses the structure, use the name of the structure as the data type of the
variable:

myDataType myVar;
#include <iostream>
#include <string>
using namespace std;

// Declare a structure named "car"


struct car {
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
string brand;
string model;
int year;
};

int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;

// Create another car structure and store it in myCar2;


car myCar2;
myCar2.brand = "Ford";
myCar2.model = "Mustang";
myCar2.year = 1969;

// Print the structure members


cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year << "\n";
cout << myCar2.brand << " " << myCar2.model << " " << myCar2.year << "\n";
return 0;
}

Creating Pointers

You learned from the previous chapter, that we can get the memory address of a variable by using
the & operator.

Example
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
#include <iostream>

#include <string>

using namespace std;

int main() {

string food = "Pizza";

cout << food << "\n";

cout << &food << "\n";

return 0;

A pointer however, is a variable that stores 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:

Example

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.

#include <iostream>
#include <string>
using namespace std;
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
(Object Oriented Programming)
int main() {
string food = "Pizza"; // A string variable
string* ptr = &food; // A pointer variable that stores the address of food
// Output the value of food
cout << food << "\n";
// Output the memory address of food
cout << &food << "\n";
// Output the memory address of food with the pointer
cout << ptr << "\n";
return 0;
}

Lab Tasks
1. Write all C++ code for arrays, structure and pointers and check the output.
2. Write C++ code for array that take 5 numbers randomly from the user also store these
numbers in array.
3. Write the C++ code to generate the following table.

4. Write a program to store and print the roll no., name, age and marks of a student using
structures.
5. Write a program to print the address of a variable whose value is input from user.

You might also like