OOP Lab 03
OOP Lab 03
Lab 03
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:
You access an array element by referring to the index number inside square brackets [].
Example
#include <iostream>
#include <string>
int main() {
return 0;
Example
#include <iostream>
#include <string>
int main() {
return 0;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}
Multi-Dimensional 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.
This statement accesses the value of the element in the first row (0) and third column (2) of
the letters array.
Example
#include <iostream>
int main() {
string letters[2][4] = {
};
return 0;
#include <iostream>
string letters[2][4] = {
};
letters[0][0] = "Z";
return 0;
#include <iostream>
int main() {
string letters[2][4] = {
};
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):
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;
}
#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
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:
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;
int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
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>
int main() {
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.