c++++
c++++
Memory allocation for objects in C++ occurs in two primary ways: static and
dynamic.
Static Allocation:
Memory is allocated at compile time.
Typically occurs on the stack.
Objects are created when declared and automatically destroyed when they go out of
scope.
Example: MyClass obj;
Dynamic Allocation:
Memory is allocated at runtime using the new operator.
Occurs on the heap.
Objects persist until explicitly deallocated using the delete operator.
Example: MyClass* obj_ptr = new MyClass();
Requires manual memory management to avoid memory leaks.
STATIC DATA MEMBER
static data member is a class variable that is shared by all objects of the
class. It is associated with the class itself, rather than with individual instances
of the class.
Key characteristics:
Shared among all objects:
All objects of the class share the same static data member. There is only one copy of
the static member for the entire class.
Class-level:
It belongs to the class, not to any specific object.
Initialization:
Static data members must be defined and initialized outside the class definition,
typically in the global scope.
Lifetime:
The lifetime of a static data member is the entire duration of the program. It is
allocated when the program starts and deallocated when the program ends.
Access:
Static members can be accessed using the class name and the scope resolution
operator (::), without needing an object of the class.
STATIC MEMBER FUNCTION
A data member of a class can be qualified as static. The properties of a static member
variable are similar to that of Cs static variable. A static data member has certain
special characteristics. They are:-
It is initialized to zero when the first object of its class is created. No other
initialization is permitted.
Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the entire program.
A static variable is normally used to maintain value common to the entire class. For e.g,
to hold the count of objects created. Note that the type and scope of each static
member variable must be declared outside the class definition. This is necessary
because the static data members are stored separately rather than as a part of an
object.
ARRAYS OF OBJECT
An array of objects in C++ is a collection of objects of the same class type
stored in contiguous memory locations. It allows the creation and
management of multiple objects with similar properties and behaviors within a
single data structure. Each element in the array represents an individual
object, and they can be accessed using their respective indices.
Declaration:
An array of objects is declared similarly to arrays of primitive data types, but
the data type is a class name.
ClassName objectArray[arraySize];
class Rectangle {
public:
int length;
int width;
int area() {
return length * width;
}
};
int main() {
const int size = 3;
Rectangle rects[size] = {Rectangle(2, 3), Rectangle(4, 5), Rectangle(6, 7)};
A Rectangle class is defined with length and width attributes and an area() method.
An array of Rectangle objects named rects is declared with a size of 3.
Each element of the rects array is initialized with a Rectangle object using a constructor.
The area() method is called on each object in the array, and the results are printed.
Key points:
Each object in the array has its own set of member variables, which are independent of
other objects in the array.
Objects in the array can be accessed using the array index and the dot operator.
Arrays of objects can be used to organize and manage multiple instances of a class
effectively.
The array can also be initialized using a loop, prompting the user to input values for
each object.
FRIENDLY FUNCTION
A friend function in C++ is a function that is not a member of a class but has
access to the private and protected members of that class. It is declared
inside the class using the friend keyword but is defined outside the class
scope.
Non-member Function:
It is not a member of the class, meaning it cannot be called using the dot operator on
an object of the class.
Access to Private and Protected Members:
It can access private and protected members of the class, which are normally
inaccessible from outside the class.
Declaration:
It is declared within the class using the friend keyword, but the definition is outside the
class.
Not Inherited:
Friendship is not inherited, meaning derived classes do not automatically have access
to the private and protected members of the base class.
Not Transitive:
Friendship is not transitive, meaning if class A is a friend of class B, and class B is a
friend of class C, it does not imply that class A is a friend of class C.
Usage:
Friend functions are useful in situations where a function needs access to the internal
data of a class but is not logically a member of the class. Examples include:
Overloading operators for classes
Implementing functions that need access to multiple class objects
Creating helper functions that work with a class's internal data