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

Sums a sequence of integers, assume that the first integer read with cin sp...

assume that the first integer read with cin specifies the number of values remaining to be entered. that program should read only one value each time cin is executed .a typical inp

Advanced input - output routines, Although we can handle most I/O routines ...

Although we can handle most I/O routines with getc and putc , there are file versions of fgets, fputs, fprintf and fscanf . The syntax is       fgets(stringname,n,filename);   Th

Write a program to calculate the total resistance, Write a program to calcu...

Write a program to calculate the total resistance of a series or parallel circuit. The maximum number of resistors is two.   We need to decide whether the user wants the to

What are source files and bytecode files, Problem : (a) What do you u...

Problem : (a) What do you understand by the term ‘constructor' in Java? Explain with an appropriate example. (b) Describe the differences between an object and a class usi

Subrotine assembly language, You have to write a subroutine (assembly langu...

You have to write a subroutine (assembly language code using NASM) for the following equation.

Program that print the number of hops, You are working for a computer gamin...

You are working for a computer gaming company and have been asked to look at a survival game.  You will be simulating a rabbit stranded on an island, collecting a large number of s

Algorithm, What is an algorithm and write an algorithm to calculate the sim...

What is an algorithm and write an algorithm to calculate the simple interest

Random question, Ask question #write statement that assign random integer t...

Ask question #write statement that assign random integer to the varaible n in the (100

Restart, how to create program in c that will system restart

how to create program in c that will system restart

Decode the code, By pressing ‘6’ (i.e. key’6’ one time) on the keypad, the ...

By pressing ‘6’ (i.e. key’6’ one time) on the keypad, the mobile shows ‘m’ on display screen in write text message area

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