0% found this document useful (0 votes)
3 views8 pages

c++++

The document explains key concepts related to arrays of classes and memory allocation in C++. It covers static and dynamic memory allocation, static data members and functions, passing objects as function arguments, friend functions, and constant member functions. Each section provides definitions, characteristics, and examples to illustrate the concepts in C++ programming.

Uploaded by

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

c++++

The document explains key concepts related to arrays of classes and memory allocation in C++. It covers static and dynamic memory allocation, static data members and functions, passing objects as function arguments, friend functions, and constant member functions. Each section provides definitions, characteristics, and examples to illustrate the concepts in C++ programming.

Uploaded by

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

Array with class

An array of classes is a collection of objects of the same class type stored


contiguously in memory. This allows managing multiple instances of a class
using a single variable name.
 ClassName: The name of the class.
 arrayName: The name of the array.
 arraySize: The number of objects the array can hold.

Memory allocation for object

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;

Rectangle(int l = 0, int w = 0) : length(l), width(w) {}

int area() {
return length * width;
}
};

int main() {
const int size = 3;
Rectangle rects[size] = {Rectangle(2, 3), Rectangle(4, 5), Rectangle(6, 7)};

for (int i = 0; i < size; i++) {


std::cout << "Area of rectangle " << i + 1 << ": " << rects[i].area() << std::endl;
}
return 0;
}

 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.

OBJECT AS FUNCTION ARGUMENT


Objects can be passed as arguments to functions, similar to how primitive
data types are passed. There are two primary methods for passing objects: by
value and by reference.
Pass by Value: When an object is passed by value, a copy of the object is
created within the function. Any modifications made to the object inside the
function do not affect the original object outside the function. This method is
suitable when you don't want the function to alter the original object.
Pass by Reference: When an object is passed by reference, the function
receives a reference to the original object. This means that any changes
made to the object within the function directly affect the original object outside
the function. Passing by reference is more efficient than passing by value,
especially for large objects, as it avoids the overhead of copying.

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

CONSTANT MEMBER FUNCTION


Constant member functions are those functions that are denied permission
to change the values of the data members of their class. To make a member
function constant, the keyword const is appended to the function prototype
and also to the function definition header.
Like member functions and member function arguments, the objects of a
class can also be declared as const. An object declared as const cannot be
modified and hence, can invoke only const member functions as these
functions ensure not to modify the object. A const object can be created by
prefixing the const keyword to the object declaration. Any attempt to change
the data member of const objects results in a compile-time error.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy