Description: D:\website\syllabus\study_matreials\c++\logo_pic\logo.jpg

Array of Objects



/*  create student class  with following data members

        int rno;

        char nm[20];

        float avg;

create an Array of Objects ( size :5 )

decide procedural abstraction ( All Possible member Function ) for String Class.

Sort Array of Objects according descending order of avg marks.

Display Result in tabular Format before and after sorting.

*/

#include<iostream>

using namespace std;

#include <string.h>

class Student

{

    int rno;

    char nm[20];

    float avg;

public:

    Student():rno(0),avg(0)

    {

        nm[0]=0;

    }

    Student(int no, char *name, float av):rno(no),avg(av)

    {

        strcpy(nm, name);

    }

    int Get_rno()

    {

        return rno;

    }

    void GetName(char* name)

    {

        strcpy(name, nm);

    }

    float Get_avg()

    {

        return avg;

    }

    void acceptStudentData()

    {

        cout<<"Enter Student roll: ";

        cin>>this->rno;

        cout<<"Enter Student name: ";

        cin>>this->nm;

        cout<<"Enter Student avg: ";

        cin>>this->avg;

    }

    void Sort(Student* arr)

    {

    }

    void Display()

    {

        cout<<"roll "<<this->rno<<"  name: "<<this->nm<<"  avg:"<<avg<<endl;

    }

};

void bubble_sort(Student arr[])

{

    int n= 5;

    for(int i = 0; i<n; ++i)

    {

        for (int j= 0;j <n-1; ++j)

        {

            if (arr[j].Get_avg()  >arr[j+1].Get_avg())

            {

                Student  temp = arr[j];

                arr[j] = arr[j+1];

                arr[j+1]= temp;

            }

        }

    }

}

int main()

{

    //bubble_sort();

    int i=0;

    Student arr[5];

    for (i=0; i<5; ++i)

    {

        arr[i].acceptStudentData();

    }

    cout<<"\nbefore sort:\n";

    for (i=0; i<5; ++i)

    {

        arr[i].Display();

    }

    bubble_sort(arr);

    cout<<"after sort: \n";

    for (i=0; i<5; ++i)

    {

        arr[i].Display();

    }

}  

 


Run in MyWhiteBoard


Copyright © Open Sky Technology