Define array of objects, C/C++ Programming

Assignment Help:

Array of Objects

A class is a template, which can contain data items as well as member functions to operate on the data items. Various objects of the class can also be declared and used. Also, an array of objects can be declared and used just like an array of any other data type. An example will show the use of array of objects.

e.g.

                class student

                 {

                  public :

                                                void getdetails();

                                                void printdetails();

                  private :

                                                int rollno;

                                                char name[25];

                                                int marks[6];

                                                float percent;

                 };

                void student :: getdetails()

                 {

                  int ctr,total; 

                                                cout << "enter rollno";

                                cin >> rollno ;

                                cout << "enter name";

                                cin >> name;

                                cout << " enter 6 marks " ;

                                for( ctr = 1 ;ctr <= 6 ; ctr++ )

                                 {

                                  cin >> marks[ctr];

                                  total = total + marks[ctr];

                      }

 

                                percent = total / 6;

                 }

 

                void student :: printdetails ()

                 {

                                cout << rollno << name << percent ;

                 }

 

                void main()

                 {

                  student records[50];

                  int x=0;

                                cout << " How many students ";

                                cin >> x;

                                for ( int i =1; i<= x; i++)

                                 {

                                                records[i].getdeatils(); 

                                 }

                                for ( int i =1; i<= x; i++)

                                 {

                                                records[i].printdeatils();              

                                 }

 

                }

As can be seen above, an array of objects is declared just like any other array. Members of the class are accessed, using the array name qualified by a subscript.

 


Related Discussions:- Define array of objects

Blanche has a fashion design company called BLB_Best_Clothin, Blanche has a...

Blanche has a fashion design company called BLB_Best_Clothing Pty. That she has just opened recently

Function declarations and function definitions in cpp, FUNCTIONS It refe...

FUNCTIONS It refers to a subprogram that is meant to do a certain task. It is basically used to execute a set of operations and return information to the main or calling functio

#AREA UNDER CURVE, #question.Write a program to find the area under the cur...

#question.Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two po

What is the role of && operator in a program code, What is the role of && o...

What is the role of && operator in a program code? - && is also referred to as AND operator. - When this operator is used, all conditions specified should be TRUE before nex

I need computer application/program, I need Computer application/program. ...

I need Computer application/program. Project Description:                                                                I want a project done. It is a computer desktop appli

Explain the different types of errors in php, Explain the different types o...

Explain the different types of errors in PHP. Warnings, Notices and Fatal errors are the types of errors in PHP Notices: Notices signifies non-critical errors, i.e. ac

Define some features of automatic variables in c program, Define Some Featu...

Define Some Features of Automatic Variables in C program? The features of automatic variables are like as Storage - memory Default initial value - an unpredictable value,

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd