banner



How To Create An Array In C++ For A Class

After having a good command over classes and objects, you must have understood how useful the concept of classes and objects can be.

In the previous chapter, we printed the area of a rectangle by making an object of the Rectangle class. If we have to print the area of two rectangles having different dimensions, we can create two objects of the Rectangle class, each representing a rectangle. Before moving to the concept of array of objects, let's first see an example of printing the area of two rectangles.

                                #include                <iostream>                                using                namespace                std                ;                class                Rectangle                {                public                :                int                length                ;                int                breadth                ;                Rectangle                (                int                l                ,                int                b                )                {                length                =                l                ;                breadth                =                b                ;                }                int                printArea                ()                {                return                length                *                breadth                ;                }                };                int                main                ()                {                Rectangle                rt1                (                7                ,                4                );                Rectangle                rt2                (                4                ,                5                );                cout                <<                "Area of first rectangle "                <<                rt1                .                printArea                ()                <<                endl                ;                cout                <<                "Area of second rectangle "                <<                rt2                .                printArea                ()                <<                endl                ;                return                0                ;                }              

Output

Area of first rectangle 28
Area of second rectangle 20

We created two objects rt1 and rt2 of class Rectangle representing the two rectangles. Rectangle rt1( 7, 4 ); - created the object 'rt1' and assigned its length and breadth as 7 and 4 respectively. Similarly, Rectangle rt2( 4, 5 ); created the object 'rt2' and assigned its length and breadth as 4 and 5 respectively.

Now, suppose we have 50 students in a class and we have to input the name and marks of all the 50 students. Then creating 50 different objects and then inputting the name and marks of all those 50 students is not a good option. In that case, we will create an array of objects as we do in case of other data-types.

Let's see an example of taking the input of name and marks of 5 students by creating an array of the objects of students.

                                #include                <iostream>                                #include                <string>                                using                namespace                std                ;                class                Student                {                string                name                ;                int                marks                ;                public                :                void                getName                ()                {                getline                (                cin                ,                name                );                }                void                getMarks                ()                {                cin                >>                marks                ;                }                void                displayInfo                ()                {                cout                <<                "Name : "                <<                name                <<                endl                ;                cout                <<                "Marks : "                <<                marks                <<                endl                ;                }                };                int                main                ()                {                Student                st                [                5                ];                for                (                int                i                =                0                ;                i                <                5                ;                i                ++                )                {                cout                <<                "Student "                <<                i                +                1                <<                endl                ;                cout                <<                "Enter name"                <<                endl                ;                st                [                i                ].                getName                ();                cout                <<                "Enter marks"                <<                endl                ;                st                [                i                ].                getMarks                ();                }                for                (                int                i                =                0                ;                i                <                5                ;                i                ++                )                {                cout                <<                "Student "                <<                i                +                1                <<                endl                ;                st                [                i                ].                displayInfo                ();                }                return                0                ;                }              

Output

Student 1
Enter name
Jack
Enter marks
54
Student 2
Enter name
Marx
Enter marks
45
Student 3
Enter name
Julie
Enter marks
47
Student 4
Enter name
Peter
Enter marks
23
Student 5
Enter name
Donald
Enter marks
87
Student 1
Name : Jack
Marks : 54
Student 2
Name : Marx
Marks : 45
Student 3
Name : Julie
Marks : 47
Student 4
Name : Peter
Marks : 23
Student 5
Name : Donald
Marks : 87

Now let's go through this code.

Student st[5]; - We created an array of 5 objects of the Student class where each object represents a student having a name and marks.
The first for loop is for taking the input of name and marks of the students. getName() and getMarks() are the functions to take the input of name and marks respectively.
The second for loop is to print the name and marks of all the 5 students. For that, we called the displayInfo() function for each student.

array of objects in C++

Hopefully, you are now ready to create arrays of objects.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

An ounce of practice is worth more than tons of preaching.
-Mahatma Gandhi

How To Create An Array In C++ For A Class

Source: https://www.codesdope.com/cpp-array-of-objects/

Posted by: gordonhatelve.blogspot.com

0 Response to "How To Create An Array In C++ For A Class"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel