0% found this document useful (0 votes)
13 views6 pages

Reviewer

The document explains arrays in C++, highlighting their purpose, benefits, and examples of declaration, initialization, and modification. It also covers Object-Oriented Programming (OOP) concepts, including classes, objects, access specifiers, constructors, and destructors, emphasizing their roles in structuring code for reusability and maintainability. Overall, it provides foundational knowledge for managing data and implementing OOP principles in C++.

Uploaded by

Louie Lagasca
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)
13 views6 pages

Reviewer

The document explains arrays in C++, highlighting their purpose, benefits, and examples of declaration, initialization, and modification. It also covers Object-Oriented Programming (OOP) concepts, including classes, objects, access specifiers, constructors, and destructors, emphasizing their roles in structuring code for reusability and maintainability. Overall, it provides foundational knowledge for managing data and implementing OOP principles in C++.

Uploaded by

Louie Lagasca
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/ 6

Array in C++

An array is a collection of elements of the same data type stored in contiguous memory locations. Each
element in the array can be accessed using an index (starting from 0).
Purpose of Arrays in C++
Arrays provide an efficient way to store and manage multiple values using a single variable name. Instead of
declaring multiple separate variables, arrays allow storing and processing multiple related data items.
Key Benefits of Using Arrays:
1. Efficient Data Storage: Store multiple values under a single name.
2. Easy Access: Retrieve or modify elements using their index.
3. Reduces Code Complexity: Instead of declaring individual variables, use a loop to process multiple values.
4. Useful in Data Processing: Arrays help in sorting, searching, and storing large datasets like matrices.
Example of an Array in C++:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Array declaration and initialization
cout << "First element: " << numbers[0] << endl; // Accessing an element
return 0;
}
Output:
First element: 10
Arrays are commonly used in loops, searching algorithms, sorting, and data storage in various applications.
Declaring and Initializing Arrays in C++
1. Declaring an Array in C++
An array is declared using the following syntax:
data_type array_name[array_size];
 data_type → The type of data stored (e.g., int, float, char).
 array_name → The name of the array.
 array_size → The number of elements in the array.
Example:
int numbers[5]; // Declares an array of 5 integers

2. Initializing an Array During Declaration


An array can be initialized at the time of declaration.
Method 1: Specifying all values
int numbers[5] = {10, 20, 30, 40, 50}; // Declares and initializes
Method 2: Letting the Compiler Determine Size
int numbers[] = {10, 20, 30, 40, 50}; // Size is automatically set to 5

3. Initializing an Array Using a Loop


Instead of manually assigning values, a loop can be used:
#include <iostream>
using namespace std;

int main() {
int numbers[5];

// Assign values using a loop


for(int i = 0; i < 5; i++) {
numbers[i] = i * 10; // Assigns values (0, 10, 20, 30, 40)
}

// Display the array values


for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}

return 0;
}
Output:
0 10 20 30 40
2. Modifying Elements in an Array
To modify an element, assign a new value using its index.
Example:
#include <iostream>
using namespace std;

int main() {
int numbers[5] = {10, 20, 30, 40, 50};

// Modifying elements
numbers[2] = 99; // Changing the third element

// Display modified array


for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}

return 0;
}
Output:
10 20 99 40 50

Summary
Declaration: int arr[5];
Initialization: int arr[5] = {1, 2, 3, 4, 5};
Using a loop for initialization: Assign values dynamically.
Character arrays can store strings.
Accessing Elements: Use array_name[index] (e.g., arr[2])
Modifying Elements: Assign a new value using array_name[index] = new_value;
Using Loops: Iterate through the array for bulk modification
Arrays are fundamental in storing and processing multiple values efficiently in C++.
Arrays allow easy data management in C++ programs!
Classes and Objects in C++ (Object-Oriented Programming - OOP)
Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure programs. It helps in
code reusability, modularity, and maintainability.
Key OOP Concepts
1. Encapsulation – Hiding data and exposing only necessary parts.
2. Abstraction – Simplifying complex systems by exposing only necessary details.
3. Inheritance – Allowing one class to derive properties from another.
4. Polymorphism – Using a single interface for different data types or methods.

What is a Class?
A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that objects will have.
Syntax of a Class in C++
class Car { // Class definition
public: // Access specifier
string brand; // Attribute (data member)
int year; // Attribute

void display() { // Method (function)


cout << "Car Brand: " << brand << ", Year: " << year << endl;
}
};
 Attributes: brand and year are variables that store object data.
 Methods: display() is a function that prints car details.
 Access Specifier: public allows access to variables and functions outside the class.

What is an Object?
An object is an instance of a class. It holds real values for the class attributes and can call class functions.
Creating Objects in C++
int main() {
Car myCar; // Object creation
myCar.brand = "Toyota"; // Assign values
myCar.year = 2022;

myCar.display(); // Call method


return 0;
}
Output
Car Brand: Toyota, Year: 2022
Each object has its own copy of attributes and can perform functions defined in the class.

Access Specifiers in C++


Access specifiers control access to class members.
Access Specifier Description
public Members are accessible from outside the class.
private Members are only accessible within the class.
protected Members are accessible in derived classes (inheritance).
Example: Public and Private Members
class Student {
private:
int age; // Private variable

public:
void setAge(int a) { // Setter function
age = a;
}
int getAge() { // Getter function
return age;
}
};

int main() {
Student s1;
s1.setAge(20); // Assign value using setter
cout << "Student age: " << s1.getAge(); // Output: Student age: 20
return 0;
}
 Private members (age) cannot be accessed directly.
 Getter and setter functions control access.

Constructors and Destructors


A constructor initializes an object automatically when it is created.
A destructor is called when an object is deleted.
Example of a Constructor
class Person {
public:
Person() { // Constructor
cout << "Constructor is called!" << endl;
}
};

int main() {
Person p1; // Object creation
return 0;
}
Output
Constructor is called!
 Constructors do not need to be called manually.
 They initialize objects automatically.
Destructor Example
class Person {
public:
~Person() { // Destructor
cout << "Destructor is called!" << endl;
}
};

int main() {
Person p1; // Object creation
return 0;
}
Output
Destructor is called!
 Destructors clean up memory when an object is deleted.
Summary
✅ Classes define blueprints for objects.
✅ Objects are instances of classes with real values.
✅ Access specifiers (public, private, protected) control access.
✅ Constructors and destructors initialize and clean up objects.
Would you like some exercises or a quiz on this topic? 😊

You might also like