C++ Articles - Page 675 of 719

How to “return an object” in C++?

Ravi Ranjan
Updated on 20-May-2025 19:38:05

5K+ Views

An object is an instance of a class. The memory is allocated only when an object is created and not when a class is defined. How to Return an Object in C++? An object can be returned by a function using the return keyword. There is no specific technique to return an object, you can simply return it just like any other data type returning from the function. Consider the following syntax. Syntax The syntax for returning an object from the function is as follows: class_name function_name(class_name obj1) // Passing object { class_name obj2; ... Read More

What is the lifetime of a static variable in a C++ function?

Ravi Ranjan
Updated on 15-May-2025 19:33:43

17K+ Views

The lifetime of a static variable in a C++ function exists till the program executes. We can say the lifetime of a static variable is the lifetime of the program. The static variable is a variable that is declared using the static keyword. The space for the static variable is allocated only one time, and this is used for the entirety of the program. In this article, we will understand the lifetime of the static variable and the reason behind its lifetime. Why do Static Variable Exists until program execution? A static variable is initialized ... Read More

When should you use a class vs a struct in C++?

Chandu yadav
Updated on 26-Jun-2020 13:36:53

452 Views

Structures and classes are very similar in C++ except for some differences. So details about these differences are given below that help to decide when to use a class or structure in C++.Differences between Class and StructureAll the members of a class are private by default. This is different compared to structures as all the members of a structure are public by default.A program that demonstrates a class in C++ is given as follows −Example#include using namespace std; class Example {    int val; }; int main() {    Example obj;    obj.val = 20;    return 0; }This ... Read More

How do I find the length of an array in C/C++?

George John
Updated on 09-Apr-2025 19:14:40

21K+ Views

To find the length of an array in C++, we can use various functions and approaches that we are going to discuss in this article. Finding the length of an array is a very common task and is used in looping through an array, sorting the array, finding maximum and minimum, and in many more scenarios. In this article, we have an array and our task is to find the length of the array in C++. Approaches to Find Length of Array Here is a list of approaches to find the length of an array in C++ which ... Read More

When to use references vs. pointers in C/C++

karthikeya Boyini
Updated on 26-Jun-2020 09:35:51

545 Views

Reference variableReference variable is an alternate name of already existed variable. It cannot be changed to refer another variable and should be initialized at the time of declaration. It cannot be NULL. The operator ‘&’ is used to declare a reference variable.The following is the syntax of reference variable.datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variableHere, datatype − The datatype of variable like int, char, float etc.variable_name − This is the name of variable given by user.refer_var − The name of reference variable.The following is an example of reference variable.Example Live Demo#include using namespace std; int ... Read More

What is the size of an object of an empty class in C++?

Samual Sam
Updated on 11-Apr-2025 17:17:28

380 Views

The size of an object of an empty class in C++ is 1 byte as it allocates one unique address to the object in the memory. The size can not be 0, as the two objects can not have same memory allocation. In this article, we will see an example of checking the size of an object of an empty class in C++. Demonstrating Size of an Empty Class In this example, we have two C++ classes. One class is an empty class while other is not an empty class. We have printed the size of objects of both the ... Read More

How to call a parent class function from derived class function in C++?

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:31:11

11K+ Views

In the OOPs concept of C++, the parent class represents the root of the hierarchy while the derived class is inherited from the parent class. The derived class is presented using the scope resolution operator (::). The derived class is also known as the child class or subclass. What is Parent Class? The parent class is also called a base class used to design the structure of variable and function initialization based on access specifiers (private, public, and protected). Syntax Following is the syntax of parent class in C++: class ParentClass { // Access specifiers: ... Read More

When to use extern in C/C++

Tapas Kumar Ghosh
Updated on 22-Apr-2025 15:15:40

11K+ Views

In C/C++, the extern keyword is used to declare a variable that is defined in another file or scope. It allows the program to access a variable or function that is defined outside the file. There are two main reason of using extern in C/C++ programs which is listed below: The extern is used when the compiler needs to be informed about the existence of a variable that is declared in another file. ... Read More

What is a reference variable in C++?

Revathi Satya Kondra
Updated on 21-Apr-2025 19:22:04

23K+ Views

In C++, the reference variable is an alternate name of already existing variable. It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL. The operator '&' is used to declare reference variable. Reference variables are commonly used for parameter passing in functions, returning values, and aliasing existing variables. Syntax Following is the syntax for the reference variable in c++: datatype &refer_var = variable_name; Here, datatype − The datatype of variable like int, char, float etc. variable_name − This is ... Read More

What is the difference between an int and a long in C++?

Samual Sam
Updated on 26-Jun-2020 09:20:19

2K+ Views

intThe datatype int is used to store the integer values. It could be signed or unsigned. The datatype int is of 32-bit or 4 bytes. It requires less memory area than long to store a value. The keyword “int” is used to declare an integer variable.The following is the syntax of int datatype.int variable_name;Here,variable_name − The name of variable given by user.The following is an example of int datatype.Example Live Demo#include using namespace std; int main() {    int a = 8;    int b = 10;    int c = a+b;    cout

Advertisements