[go: up one dir, main page]

0% found this document useful (0 votes)
9 views4 pages

65e19d10d9463OOP LAB 1

Uploaded by

sabailyas2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

65e19d10d9463OOP LAB 1

Uploaded by

sabailyas2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Namal University Mianwali

Department of Computer Science

LAB MANUAL # 1

Subject: OOP Instructor Name: Ammar Ahmad Khan

Code: Lab Timings: 02:20 PM to 05:20 PM (FRIDAY)

Structures & Pointers

Definition:
A structure is a user-defined data type that allows you to group together variables
of different data types under a single name. Structures provide a way to represent a
composite object that can contain members with various data types.

Declaring and Defining a Structure


Declare a structure called Person with the following members:

firstName (string)
lastName (string)
age (integer)
address (string)
Example:
struct Person {
string firstName;
string lastName;
int age;
string address;
};

Initializing and Accessing Structure Members


Declare and define a variable of type Person and initialize its members.

Example:
Person person1 = {"John", "Doe", 30, "123 Main St."};
Print the values of the structure members to the console.
Example:
cout << "First Name: " << person1.firstName << endl;
cout << "Last Name: " << person1.lastName << endl;
cout << "Age: " << person1.age << endl;
cout << "Address: " << person1.address << endl;

Declare an array of Person structures.


Example:
Person people[3];
Initialize the members of each structure in the array.
Example:
people[0] = {"Jane", "Doe", 25, "456 Main St."};
people[1] = {"Bob", "Smith", 40, "789 Main St."};
people[2] = {"Mary", "Johnson", 50, "987 Main St."};

Example 1: Student Record


Write a program to declare a student struct with data members name, rollNo, and
marks and initialize its members with user input.
Requirements:
The program should initialize and accessing structure members.
Then print those values.
Solution:
#include <iostream> cout << "Roll No: " << s1.rollNo
<< endl;
#include <string>
cout << "Marks: " << s1.marks <<
using namespace std;
endl;

struct Student {
return 0;
string name;
}
int rollNo;
float marks;
};

int main() {
Student s1;
s1.name = "John Doe";
s1.rollNo = 123;
s1.marks = 85.5;

cout << "Student Details" << endl;


cout << "Name: " << s1.name <<
endl;
Pointers:
A pointer in C++ is a variable that stores the memory address of another variable.
It allows indirect access to the memory location it points to, enabling manipulation
of data indirectly. Pointers are extensively used in dynamic memory allocation,
array manipulation, and accessing data structures like linked lists and trees.

Example:
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr; // Pointer declaration

ptr = &num; // Pointer initialization with address of 'num'

// Print value of 'num' using pointer and variable


cout << "Value of num using variable: " << num <<endl;
cout << "Value of num using pointer: " << *ptr <<endl;

return 0;
}

You might also like