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

Using functions create a program, In rPeANUt implement the "char getchar()"...

In rPeANUt implement the "char getchar()" and "void printstring(char *str)" functions. Using these functions implement the following: void main() {    while (1) {       ch

Error handling and constructor, Provide me the answer, Can a constructor th...

Provide me the answer, Can a constructor throws an exception? How to handle the error when the constructor fails?

How many ways are there to initialize an int with a constant, There are two...

There are two ways for initializes in C++ as shown in the example that follows. The first way uses the traditional C notation. The second way uses constructor notation. int foo

Explain pros and cons of cpp as a programming language, Write a 3-4 page pa...

Write a 3-4 page paper (350 words per page) in APA format detailing the development and use of C++ in academia and in industry. Explain the pros and cons of C++ as a programming la

Enumeration types, Write a simple program in C++ to investigate the safety ...

Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on enumeration types that are incorrect/unsafe things to

Constants, explain about symbolic constants with examples

explain about symbolic constants with examples

Flow chart, obtain two numbers from thekey board,and determain and display(...

obtain two numbers from thekey board,and determain and display(if either)is the larger of two numbers.

Lcm, lcm program.

lcm program.

multithreaded server, Implement a multithreaded server that can be used as...

Implement a multithreaded server that can be used as a proxy server to access some designated file (say it reflects the proxy copy of two file1.txt, file2.txt). Now use a client to

Student, Ask4. Write a query to display the columns listed below. The query...

Ask4. Write a query to display the columns listed below. The query should list each customer in which the video rental is overdue. The Days_Overdue column should calculate the numb

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