Object Array inside another class

Hi all ,

The code below is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef FUNCTION_H
#define FUNCTION_H

#include<iostream>
using namespace std;

const int SIZE = 20;
const string filename = "input.txt";



class Person {
private :
    string name;
    int id;
    string faculty;
    string branch;

public :
    void set_name(string);
    void set_id (int);
    void set_faculty(string);
    void set_branch(string);
    string get_name();
    int get_id();
    string get_faculty();
    string get_branch();
    string information_check_faculty(string);
    string information_check_branch(string);
};




class Advisee : public Person {
private :
    string major;
    double CGPA;

public :
    void set_major(string);
    void set_CGPA (double);
    string get_major();
    double get_CGPA();
    int get_information(string,int);
    string information_check_major(string);
};




class Advisor : public Person {
private :
    int contact_num;
    string room_num;
    Advisee student[SIZE];

public :
    Advisor();
    int size_of_advisee;
    void set_contact_num(int);
    void set_room_num(string);
    int get_contact_num();
    string get_room_num();
    void set_advisee(Advisee [],int);
    int get_information(string);
};





void MainMenu_header ();
char MainMenu ();
void table ();
int strWordCount(string line);
string strWord(int index, string line);
void view_information_main (Advisor data1[],Advisee data2[][SIZE], int size_advisors ,int size_advisees[],int sum_of_adviesses);
void strCheck(string line,Advisor data1[],Advisee data2[][SIZE],int size_advisees[],int lines);
void loadAllInfo(Advisor data1[],Advisee data2[][SIZE], int& size_advisors ,int size_advisees[],int& sum_of_adviesses);


#endif


I wish to know how i can make a get_function to get the Advisee student[SIZE] details inside Advisor class.I want from this function to have 2 arguments , 1 for the size of the array and the second one for Advisee[] variable . i am unable to understand as to how it can be done .

My second question how can i use the constructors in my code to improve my code efficiency and performance .

i would appreciate any help in this regard.

thanks to all in advance.
The fact that Person has setters and getters for every data member implies that Person is a value class.
That said, I would make the data members public and rip at least the getters (otherwise they should be
const member functions if you decide to keep them).

I would also make two Person constructors, implemented as follows:

1
2
3
4
Person::Person() : name(), id(), faculty(), branch() {}

Person::Person( const std::string& name, int id, const std::string& faculty, const std::string& branch ) :
    name( name ), id( id ), faculty( faculty ), branch( branch ) {}


Do the same for advisor. (Constructor should take at least contact_num and room_num; whether or
not it takes an array of students depends on how you use the object).

These allow you to write:

 
Person me( "jsmith", 7, "foobar", "blah" );


instead of

1
2
3
4
5
Person me;
me.set_name( "jsmith );
me.set_id( 7 );
me.set_faculty( "foobar" );
me.set_branch( "blah" ); 


The former is easier to get right than the latter. The latter code gets silently broken whenever you add a new data member to Person and forget to update the code. In the former case, you'd add another parameter to the constructor, and then the code will fail to compile until you pass the additional parameter. (The latter code is
also slightly less efficient).

The easiest way to do what you want with the Advisee list is to store the list as an STL container such as a deque and return a copy of the deque (which would be one "thing" instead of two).

If you are not allowed to do that, then you have to return the values via function arguments. For example:

1
2
3
4
5
6
7
8
9
10
void foo( int& x, int& y ) {
    x = 4;
    y = 5;
}

int main() {
    int a, b;
    foo( a, b );
    // a = 4, b = 5 now
}


Or you can also use pointers:

1
2
3
4
5
6
7
8
9
10
void foo( int* x, int* y ) {
    *x = 4;
    *y = 5;
}

int main() {
    int a, b;
    foo( &a, &b );
    // a = 4, b = 5 now
}


The former is preferred to the latter.

Thanks you. i'm really appreciating your help . For constructor part i think i understood how to applied in my code .




The easiest way to do what you want with the Advisee list is to store the list as an STL container such as a deque and return a copy of the deque (which would be one "thing" instead of two).



But here you said i can use STL container in which i have to use pointers or references but the the problem with Advisee is an array which array it's by reference by default , moreover here my problem that i can't call for Advisee variable inside Advisor class from int main() because Advisee[SIZE] is a private variable which needs a function to access the data from int main() ? .
Topic archived. No new replies allowed.