The length of an array in C++ is significant because it determines ____.
1. the number of elements that can be stored in the array
2. the memory allocated for the array
3. the maximum size of the array
4. All of the above
Explanation:
Please open Telegram to view this post
VIEW IN TELEGRAM
👍15❤5
To access elements in a structure in C++, you use the structure variable name followed by ___.
1. A dot operator (.)
2. An arrow operator (->)
3. A hash sign (#)
4. A dollar sign ($)
Explanation:
Please open Telegram to view this post
VIEW IN TELEGRAM
👍23❤4
Top 50 C++ Keywords & Functions
#CPP #Basics #IO
#1.
A preprocessor directive that includes the input/output stream library.
#2.
The main function where program execution begins.
#3.
Used to output data (print to the console).
#4.
Used to get input from the user.
#5.
Tells the compiler to use the
---
#CPP #DataTypes #Variables
#6.
Declares an integer variable.
#7.
Declares a floating-point number variable (can hold decimals).
#8.
Declares a character variable.
#9.
Declares a boolean variable, which can only have the value
#10.
Declares a variable that can hold a sequence of characters. Requires
---
#CPP #Keywords #Operators
#11.
Declares a variable as a constant, meaning its value cannot be changed.
#12.
An operator that returns the size (in bytes) of a data type or variable.
#CPP #Basics #IO
#1.
#include <iostream>A preprocessor directive that includes the input/output stream library.
#include <iostream>
int main() {
std::cout << "This requires iostream!";
return 0;
}
This requires iostream!
#2.
int main()The main function where program execution begins.
#include <iostream>
int main() {
std::cout << "Program starts here.";
return 0;
}
Program starts here.
#3.
std::coutUsed to output data (print to the console).
#include <iostream>
int main() {
std::cout << "Hello, C++!";
return 0;
}
Hello, C++!
#4.
std::cinUsed to get input from the user.
#include <iostream>
#include <string>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";
return 0;
}
Enter your age: 25
You are 25 years old.
#5.
using namespace std;Tells the compiler to use the
std (standard) namespace. Avoids prefixing std:: to every standard function.#include <iostream>
using namespace std;
int main() {
cout << "No std:: prefix needed.";
return 0;
}
No std:: prefix needed.
---
#CPP #DataTypes #Variables
#6.
intDeclares an integer variable.
#include <iostream>
int main() {
int number = 100;
std::cout << "The number is: " << number;
return 0;
}
The number is: 100
#7.
doubleDeclares a floating-point number variable (can hold decimals).
#include <iostream>
int main() {
double price = 19.99;
std::cout << "The price is: " << price;
return 0;
}
The price is: 19.99
#8.
charDeclares a character variable.
#include <iostream>
int main() {
char grade = 'A';
std::cout << "Your grade is: " << grade;
return 0;
}
Your grade is: A
#9.
boolDeclares a boolean variable, which can only have the value
true or false.#include <iostream>
int main() {
bool isRaining = false;
std::cout << "Is it raining? " << isRaining; // Outputs 0 for false
return 0;
}
Is it raining? 0
#10.
std::stringDeclares a variable that can hold a sequence of characters. Requires
#include <string>.#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello, World!";
std::cout << greeting;
return 0;
}
Hello, World!
---
#CPP #Keywords #Operators
#11.
constDeclares a variable as a constant, meaning its value cannot be changed.
#include <iostream>
int main() {
const double PI = 3.14159;
std::cout << "The value of PI is: " << PI;
// PI = 4; // This would cause a compile error
return 0;
}
The value of PI is: 3.14159
#12.
sizeof()An operator that returns the size (in bytes) of a data type or variable.
#include <iostream>
int main() {
int myInt;
std::cout << "Size of int is: " << sizeof(myInt) << " bytes.";
return 0;
}
Size of int is: 4 bytes.
#13.
A keyword that lets the compiler automatically deduce the data type of a variable at compile-time.
#14.
Returns the memory address of a variable.
#15.
Accesses the value stored at a memory address held by a pointer.
---
#CPP #ControlFlow #Conditional
#16.
Executes a block of code if a specified condition is true.
#17.
Executes a block of code if the condition in the
#18.
Specifies a new condition to test, if the first
#19.
Selects one of many code blocks to be executed.
#20.
Used to exit a
---
#CPP #Loops
#21.
Executes a block of code a specified number of times.
#22.
Loops through a block of code as long as a specified condition is true.
autoA keyword that lets the compiler automatically deduce the data type of a variable at compile-time.
#include <iostream>
int main() {
auto number = 10; // Compiler deduces int
auto pi = 3.14; // Compiler deduces double
std::cout << "Type of 'number' is deduced.";
return 0;
}
Type of 'number' is deduced.
#14.
& (Address-of Operator)Returns the memory address of a variable.
#include <iostream>
int main() {
int var = 20;
std::cout << "Memory address of var: " << &var;
return 0;
}
Memory address of var: 0x61ff08
(Note: Address will vary)
#15.
* (Dereference Operator)Accesses the value stored at a memory address held by a pointer.
#include <iostream>
int main() {
int var = 50;
int* ptr = &var; // ptr holds the address of var
std::cout << "Value at address " << ptr << " is " << *ptr;
return 0;
}
Value at address 0x61ff04 is 50
(Note: Address will vary)
---
#CPP #ControlFlow #Conditional
#16.
ifExecutes a block of code if a specified condition is true.
#include <iostream>
int main() {
int age = 18;
if (age >= 18) {
std::cout << "You are an adult.";
}
return 0;
}
You are an adult.
#17.
elseExecutes a block of code if the condition in the
if statement is false.#include <iostream>
int main() {
int age = 16;
if (age >= 18) {
std::cout << "You are an adult.";
} else {
std::cout << "You are not an adult.";
}
return 0;
}
You are not an adult.
#18.
else ifSpecifies a new condition to test, if the first
if condition is false.#include <iostream>
int main() {
int score = 85;
if (score >= 90) {
std::cout << "Grade: A";
} else if (score >= 80) {
std::cout << "Grade: B";
} else {
std::cout << "Grade: C";
}
return 0;
}
Grade: B
#19.
switch / caseSelects one of many code blocks to be executed.
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
}
return 0;
}
Wednesday
#20.
breakUsed to exit a
switch statement or a loop.#include <iostream>
int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) {
break; // Exit the loop when i is 5
}
std::cout << i << " ";
}
return 0;
}
0 1 2 3 4
---
#CPP #Loops
#21.
forExecutes a block of code a specified number of times.
#include <iostream>
int main() {
for (int i = 0; i < 5; ++i) {
std::cout << i << " ";
}
return 0;
}
0 1 2 3 4
#22.
whileLoops through a block of code as long as a specified condition is true.
#include <iostream>
int main() {
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
return 0;
}
0 1 2 3 4
#23.
Similar to a
#24. Range-based
Iterates over all elements in a range, such as an array or vector, without using an index.
#25.
Skips the current iteration of a loop and continues with the next iteration.
---
#CPP #Functions
#26. Function Declaration (Prototype)
Declares a function's name, return type, and parameters, allowing it to be used before it's defined.
#27.
A keyword specifying that a function does not return any value.
#28.
Terminates a function and can return a value to the caller.
#29.
Includes the C++ math library for complex mathematical operations.
#30.
A function from
---
#CPP #STL #Vector
#31.
Includes the library for
#32.
A sequence container that encapsulates dynamic size arrays.
#33.
Member function of
do-whileSimilar to a
while loop, but the code block is executed at least once before the condition is tested.#include <iostream>
int main() {
int i = 5;
do {
std::cout << "This will run once.";
i++;
} while (i < 5);
return 0;
}
This will run once.
#24. Range-based
for loopIterates over all elements in a range, such as an array or vector, without using an index.
#include <iostream>
#include <vector>
int main() {
int numbers[] = {10, 20, 30};
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}
10 20 30
#25.
continueSkips the current iteration of a loop and continues with the next iteration.
#include <iostream>
int main() {
for (int i = 0; i < 5; ++i) {
if (i == 2) {
continue; // Skip printing 2
}
std::cout << i << " ";
}
return 0;
}
0 1 3 4
---
#CPP #Functions
#26. Function Declaration (Prototype)
Declares a function's name, return type, and parameters, allowing it to be used before it's defined.
#include <iostream>
void sayHello(); // Function declaration
int main() {
sayHello(); // Call the function
return 0;
}
void sayHello() { // Function definition
std::cout << "Hello from function!";
}
Hello from function!
#27.
voidA keyword specifying that a function does not return any value.
#include <iostream>
void printMessage(std::string message) {
std::cout << message;
}
int main() {
printMessage("This function returns nothing.");
return 0;
}
This function returns nothing.
#28.
returnTerminates a function and can return a value to the caller.
#include <iostream>
int add(int a, int b) {
return a + b; // Return the sum
}
int main() {
int result = add(5, 3);
std::cout << "Result: " << result;
return 0;
}
Result: 8
#29.
#include <cmath>Includes the C++ math library for complex mathematical operations.
#include <iostream>
#include <cmath> // Include for sqrt()
int main() {
double number = 25.0;
std::cout << "Square root is: " << sqrt(number);
return 0;
}
Square root is: 5
#30.
pow()A function from
<cmath> that returns the base raised to the power of the exponent.#include <iostream>
#include <cmath>
int main() {
double result = pow(2, 3); // 2 to the power of 3
std::cout << "2^3 is: " << result;
return 0;
}
2^3 is: 8
---
#CPP #STL #Vector
#31.
#include <vector>Includes the library for
std::vector, a dynamic array.#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector;
myVector.push_back(1);
std::cout << "Vector is ready.";
return 0;
}
Vector is ready.
#32.
std::vectorA sequence container that encapsulates dynamic size arrays.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30};
std::cout << "First element: " << numbers[0];
return 0;
}
First element: 10
#33.
.push_back()Member function of
std::vector that adds an element to the end.#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
std::cout << "Vector size: " << numbers.size();
return 0;
}
Vector size: 2
#34.
.size()Member function that returns the number of elements in a container like
std::vector or std::string.#include <iostream>
#include <vector>
int main() {
std::vector<std::string> fruits = {"Apple", "Banana"};
std::cout << "There are " << fruits.size() << " fruits.";
return 0;
}
There are 2 fruits.
#35.
.length()A member function of
std::string that returns its length. It's synonymous with .size().#include <iostream>
#include <string>
int main() {
std::string text = "C++";
std::cout << "The length of the string is: " << text.length();
return 0;
}
The length of the string is: 3
---
#CPP #STL #Algorithms
#36.
#include <algorithm>Includes the standard library algorithms, like sort, find, copy, etc.
#include <iostream>
#include <vector>
#include <algorithm> // Required for std::sort
int main() {
std::vector<int> nums = {3, 1, 4};
std::sort(nums.begin(), nums.end());
std::cout << "Sorting is possible with <algorithm>.";
return 0;
}
Sorting is possible with <algorithm>.
#37.
std::sort()Sorts the elements in a range (e.g., a vector).
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {50, 20, 40, 10, 30};
std::sort(nums.begin(), nums.end());
for(int n : nums) {
std::cout << n << " ";
}
return 0;
}
10 20 30 40 50
#38.
.begin()Returns an iterator pointing to the first element in a container.
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {100, 200, 300};
auto it = nums.begin();
std::cout << "First element: " << *it;
return 0;
}
First element: 100
#39.
.end()Returns an iterator referring to the past-the-end element in the container.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {1, 2, 3};
// .end() points after the last element, used as a boundary
std::cout << "Vector has elements until the end.";
return 0;
}
Vector has elements until the end.
#40.
#defineA preprocessor directive used to create macros or symbolic constants.
#include <iostream>
#define PI 3.14159
int main() {
std::cout << "The value of PI is " << PI;
return 0;
}
The value of PI is 3.14159
---
#CPP #OOP #Classes
#41.
classA keyword used to declare a class, which is a blueprint for creating objects.
#include <iostream>
class Dog {
public:
void bark() {
std::cout << "Woof!";
}
};
int main() {
Dog myDog;
myDog.bark();
return 0;
}
Woof!
#42.
structSimilar to a class, but its members are public by default.
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p;
p.x = 10;
p.y = 20;
std::cout << "Point: (" << p.x << ", " << p.y << ")";
return 0;
}
Point: (10, 20)
#43.
publicAn access specifier that makes class members accessible from outside the class.
#include <iostream>
class MyClass {
public: // Accessible from anywhere
int myNum = 10;
};
int main() {
MyClass obj;
std::cout << obj.myNum;
return 0;
}
10
#44.
privateAn access specifier that makes class members accessible only from within the class itself.
#include <iostream>
class MyClass {
private:
int secret = 42;
public:
void printSecret() {
std::cout << secret; // Accessible from within the class
}
};
int main() {
MyClass obj;
// std::cout << obj.secret; // This would cause a compile error
obj.printSecret();
return 0;
}
42
#45. Constructor
A special member function of a class that is executed whenever a new object of that class is created.
#include <iostream>
class Car {
public:
// Constructor
Car() {
std::cout << "Car object created.";
}
};
int main() {
Car myCar; // Constructor is called here
return 0;
}
Car object created.
---
#CPP #OOP #MemoryManagement
#46. Destructor
A special member function that is executed automatically when an object is destroyed.
#include <iostream>
class MyClass {
public:
// Destructor
~MyClass() {
std::cout << "Object destroyed.";
}
};
int main() {
MyClass obj;
// Destructor is called when main() ends
return 0;
}
Object destroyed.
#47.
thisA keyword that refers to the current instance of the class.
#include <iostream>
class Box {
private:
int length;
public:
Box(int length) {
this->length = length; // Use 'this' to distinguish member from parameter
}
void printLength() {
std::cout << "Length: " << this->length;
}
};
int main() {
Box b(10);
b.printLength();
return 0;
}
Length: 10
#48.
newAn operator that allocates memory on the heap and returns a pointer to it.
#include <iostream>
int main() {
int* ptr = new int; // Allocate an integer on the heap
*ptr = 100;
std::cout << "Value from heap: " << *ptr;
delete ptr; // Must deallocate memory
return 0;
}
Value from heap: 100
#49.
deleteAn operator that deallocates memory previously allocated with
new.#include <iostream>
int main() {
int* ptr = new int(55);
std::cout << *ptr << " allocated. ";
delete ptr; // Deallocate the memory
std::cout << "Memory freed.";
// Accessing ptr now is undefined behavior
return 0;
}
55 allocated. Memory freed.
#50.
nullptrRepresents a null pointer literal. It indicates that a pointer does not point to any valid memory location.
#include <iostream>
int main() {
int* ptr = nullptr;
if (ptr == nullptr) {
std::cout << "The pointer is null.";
}
return 0;
}
The pointer is null.
━━━━━━━━━━━━━━━
By: @DataScienceQ ✨