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

Board Coloring , In this problem you are given a board in which some of the...

In this problem you are given a board in which some of the elements are placed as shown in diagram below. Each element represents a color. Fill the other elements in the board, suc

Introduction of computer and programming concept, Classify computer system ...

Classify computer system according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

Minimum shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Program of swapping in c++, Program of swapping two varibales: void sw...

Program of swapping two varibales: void swap(int *, int *);   // This is swap's prototype int main() {                 int x = 5, y = 7;                 swap(&x, &

Oops, write a c++ program to find the prime numbers

write a c++ program to find the prime numbers

Wap to print the largest number from any 10 numbers, WAP TO PRINT THE LARGE...

WAP TO PRINT THE LARGEST NUMBER FROM ANY 10 NUMBERS #include stdio.h> #include conio.h>   void main()   {                    int a[10],i,max;

Please help to solve a c program, Padovan String Problem Description A P...

Padovan String Problem Description A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes s

Explain the terms- substitutability and extensibility, Explain the terms- S...

Explain the terms- Substitutability and Extensibility Substitutability - The objects of a properly derived class can be safely and easily substituted for an object of its

C program to find vowels , C Program to FIND VOWELS   void main() ...

C Program to FIND VOWELS   void main() {           char str[50];           int ac=0, ec=0, ic=0, oc=0, uc=0;           int i,stln,vc,word=1;           clrscr

Flowchart, how to define a contanst in a flowchart like we do in c language...

how to define a contanst in a flowchart like we do in c language

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