Jump to content
joli

Array Of Objects Boostc++

Recommended Posts

joli    0

This is just an example for who needs something alike also to c++ experts comments that are very welcome.

Purpose: .create a list of objects and do something with them

Device: pic18Fxxx

// file: students.h

#ifndef _STUDENTS_H_
#define _STUDENTS_H_

typedef unsigned char uchar;

#define STUDENTS   64                           // max number of students

class Student                                   // 
{
    private:
        uchar s_id;                             // data member
        uchar s_age;                            // data member
 
    public:
        void AddStudent( uchar *pustudent );    // function member
};

extern Student StuList[ STUDENTS ];             // studets list

#endif
// file: student.cpp

#include <system.h>
#include "students.h"


class Student StuList[STUDENTS];                  // list of students

void Student::AddStudent( uchar *pstudent ) // AddStudent
{
    s_id  = *pstudent++;                    // update id
    s_age = *pstudent;                      // update age
}

void main()
{
    uchar adata[2];                         // buffer from where data comes from
    
    for( uchar i=0; i<STUDENTS; i++ )       // Lets fill all students [0..63]
    {
        adata[0] = i+1;                     // data to be stored
        adata[1] = i+1;
        
        StuList[i].AddStudent( adata );     // add student id&age
    }
    
    while(1);
}

i did AddStudent member this way because i needed to pass a buffer with data received from the serial port.

It works perfect, but suggestions are welcome.

 

I also did it with an array of structures that results shorter in ram resources.

Using classes an additional byte is consumed for each element.

 

br

Edited by joli

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×