Class constructors and class array members

Hello everyone,

Lets say I have:
1
2
3
4
5
6
7
8
9
10
class student
{
public:
student() {midterm = 60; final = 60}
student(int a, int b) {midterm = a; final = b}

private:
int midterm;
int final;
};


and an array
1
2
const int SIZE = 2;
student array[SIZE];


I have 2 questions:

1) How can I make use of the first constructor to modify only "midterm" and keep "final" as default (ie 60)

2) How can I use the second constructor to initialize array[0] and array[1], i tried:
 
array[0](90,91)

but didn't work, knowing that
 
student anyname(90,91)

does work


Thanks!!
Last edited on
You can't. But, you can unify both constructors so they will fulfill that task, by writing just 1 constructor like this:
student(int a=60, int b=60) { midterm=a; final=b}
If a default value is given to a parameter, that value can be omitted. So Student1(60,60) Student1(60) and Student1() would all be legal calls to the parameter, and all would produce the same result.
Thanks!!
What's an alternative to inputing values of class student into the array?

Another thing, Im trying to bubble sort the array according to the highest final grade;

i declared the function as:
 
BubbleSort(student[]);


and then I defined it after main as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void BubbleSort(student a[])
{
    int i;
    int j;
    student t;
    for (i = 0; i < SIZE - 1; i++)
    for (j = 1; j <= SIZE; j++)
    {
        if (a[j].final > a[j-1].final);
        t = a[j-1];
        a[j-1] = a[j];
        a[j] = t;
    }
}


the compiler doesnt seem to have a problem with the definition at the end, but keeps reporting
C:\test\A72\main.cpp|36|error: expected constructor, destructor, or type conversion before ';' token|
C:\test\A72\main.cpp||In function `int main()':|
C:\test\A72\main.cpp|49|error: `BubbleSort' was not declared in this scope|
C:\test\A72\main.cpp|49|warning: unused variable 'BubbleSort'|
||=== Build finished: 2 errors, 1 warnings ===|
Last edited on
The problem is in this case, that if you define an array, the default constructor ( student()) will have already been called, so you are actually trying to instantiate an already existing instance. Instead, declare pointers to your instances and instanciate them together with the new operator:
1
2
student* array[size];
new array[0](90,91);


Ok, for your code to work:
Declare the parameter of your function as a reference instead of a value:
void BubbleSort(EECE230Student &a[])

Otherwise, a copy of your array will be edited, not the array itself.

Also, BubbleSort must be declared before the main function, this error message reads as if you accidentally declared it inside of it.

Do these changes, and tell me what comes out of it.

Oh and I think array is a reserved word.
Last edited on
You can make use of default parameters:
1
2
3
4
5
6
7
8
9
10
class student
{
    public:
    //student() {midterm = 60; final = 60}
    student(int mid=30, int fin=60): midterm(mid), final(fin)  {}

    private:
    int midterm;
    int final;
};


Using this method, you can get 3 for the price of 1.

1. You get a default constructor student() where if you dont provide any parameter, midterm will be set to 30 and final to the default 60.

2. A constructor taking 1 parameter student(some_value) - will make a student where midterm will be some_value and final will be the default 60.

3. A constructor taking to parameters student(some_value1, some_value2) where midterm will have some_value1 and final will have some_value2

**Notice you have no option of providing just one value to the constructor which will set final but leave midterm at the default value using the method described above.
You will need to read up on the use of defalt parameter to find out why.


Example program:
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
#include <iostream>

using namespace std;


class student
{
	public:
	student(int mid=30, int fin=60): midterm(mid), final(fin)  {}

	private:
	int midterm;
	int final;
};



const int SIZE =3;
int main()
{
    
	//Create an array of 3 students
	//the first student will have his/her own midtem value and the default final value
	//The second student will have both midterm and final values set different from the default values. 
	//Third student will have both alues set to default values
	
	student array[SIZE]={ student(20), student(15,70), student() };
	

    return 0;
}


Oh sorry, made a mistake. Where I said
new array[0](90,91);

It should actually have been array[0]= new student(90,01);
hanst99:

I tried using a value by reference for a[] but it erred, so i just moved the definition from below main to above main and removed the declaration... It worked, concerning the pointer approach, well, i deleted the former array declaration and tried the two lines you gave me. It gave the following error: 'ast' is not a type...
guestgulkan:: THANKS!!

hanst99:: NOW IT WORKED with the new code, but it give the error when Im using BubbleSort

C:\test\A72\main.cpp||In function `int main()':|
C:\test\A72\main.cpp|65|error: cannot convert `EECE230Student**' to `EECE230Student*' for argument `1' to `void BubbleSort(EECE230Student*)'|
||=== Build finished: 1 errors, 0 warnings ===|
Ahh... I didn't consider that. I try to fix it, I keep confusing myself with array pointers all the time.

Ahhh, now I get it.

1
2
3
4
5
6
7
//Signature like this:
void BubbleSort(EECE230Student *a[])
//variable definition like this:
student *array[size]; //The position of the asterisk is irrelevant, as long as it's between student      
                                  //and array
//call like this:
BubbleSort(array);


Remember though that instead of dots (student.final) you will have to use arrows (student->final) now cause you're using pointers instead of actual objects. This SHOULD work.
Last edited on
Thanks alot! Finally worked with min code written :D
Topic archived. No new replies allowed.