. Advertisement .
..3..
. Advertisement .
..4..
The main purpose of C++ is to add object-orientedness to the C Programming Language and Classes are the central feature of C++ that supports object-oriented programming and are often referred to as types. user-defined. However, in the process, there will be cases with many different parameters. If declared according to the normal method, we will have to repeat it many times. So in C++ we have an extra part that is “Arrays within a Class“. Here’s some basic information for you.
What is an Array?
An array is a group of identically data-typed elements that are kept in consecutive memory regions.
Therefore, by simply supplementing an offset to a base value, or the memory place where the first element of the array (which is generally showed by the name of the array) is stored, it is simpler to calculate each element’s position. The offset is the difference between the two indices, and index 0 acts as the base value.
Operations on an array include the following:
1. at(): This function is used to access an array’s elements.
2. get(): You can also get an array’s elements using this function. This function is an overload of a tuple class function rather than a member of the array class.
3. operator[]: This is comparable to arrays written in C. Accessing items of an array is also done using this method.
4. front(): This function retrieves the array’s first entry.
5. back() function returns the array’s final element.
6. size(): This function returns the array’s element count. C-style arrays do not have this characteristic.
7. max_size(): It gives the array’s maximum element count, or the size specified when the array was declared. The values returned by size() and max size() are identical.
8. swap(): swaps all of the elements in one array with those in another.
9. empty(): If the array size is zero, this function returns true; otherwise, it returns false.
10. fill() function: fills the entire array with a certain value.
Definition of Arrays within a Class
C++ introduces the concept of an array definition declared as a member of a class.
Declaring an array in a class can be done in one of three modes: public, private, or protected. The public keyword determines the access properties of the class members that follow it. A public member can be accessed from outside the class anywhere within the scope of that class object. You can also define class members as private or protected, which will be discussed in a subchapter.
If we declare an array in a class as private, it can only be accessed inside the class because you cannot use functions to access it. It is better to declare an array in a class in public so that it can be accessed directly with the help of object(s).
Here is an example to help you better understand “Arrays within a Class”:
Input:
#include<iostream>
const int size=5;
class student
{
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
} ;
void student :: getdata ()
{
cout<<"\nEnter roll no: ";
Cin>>roll_no;
for(int i=0; i<size; i++)
{
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i<size; i++)
total+ = marks[i];
cout<<"\n\nTotal marks "<<total;
}
void main()
student stu;
stu.getdata() ;
stu.tot_marks() ;
getch();
}
Output:
Enter roll no: 101
Enter marks in subject 1: 67
Enter marks in subject 2 : 54
Enter marks in subject 3 : 68
Enter marks in subject 4 : 72
Enter marks in subject 5 : 82
Total marks = 343
Conclusion
Through this article, we have introduced you to the necessary knowledge when learning about “Arrays within a Class“.These are the simplest definition and also the most important definition in object-oriented programming, you need to keep in mind because we will use it very often in an object-oriented programming program. If you have any questions, please contact us immediately for answers.
Read more
Leave a comment