I made an Array of Class st[5]. And tried to display st[5]'s data by using a function. But it doesn't work.
Only the 1st class (st[0]) is displayed, and the 'debug error Message'. I don't know what the problem is.
A main function is at the bottom.
#include <iostream>
using namespace std;
#define MAX 5 //size of array
//Class 'Student'
class Student
{
public:
int num;
char name[10];
};
//Class 'Lscore' extends Student (virtual)
class Lscore : virtual public Student
{
public:
int eng;
};
//Class 'Nscore' extends Student (virtual)
class Nscore : virtual public Student
{
public:
int math;
};
//Class 'Totscore' extends Lscore, Nscore
class Totscore : public Lscore, public Nscore
{
public:
Totscore(); //Constructor1
Totscore(char name[], int num, int eng, int math); //Constructor2
void Display(); //Print Myself
};
//Constructor1
Totscore::Totscore( )
{
}
//Constructor2
Totscore::Totscore(char name[10], int num, int eng, int math)
{
strcpy_s(this->name, 10, name);
this->num = num;
this->eng = eng;
this->math = math;
}
//Print Myself
void Totscore::Display(){
cout<<this->num<<" "<<this->name<<" ";
cout<<this->eng<<" "<<this->math<<" "<<endl;
}
//Print Array (--- Problem Part !! ---)
void PrintArray(Totscore *stu){
for(int i=0; i< MAX; i++){
stu[i].Display();
}
}
//Main Function
int main(){
Totscore *st[MAX]; //Class Array 'st'
st[0] = new Totscore("A",101,85,77);
st[1] = new Totscore("B",102,90,89);
st[2] = new Totscore("C",103,80,55);
st[3] = new Totscore("D",104,75,85);
st[4] = new Totscore("E",105,85,85);
PrintArray(*st);
}
And run screen is following. (I can't upload an image because my reputation is low.)
101 A 85 77
And 'Debug error message' displayed...
g++under MacOS X. I switched to usingstrncpy(this->name, name, 10);, and the code printed five lines quite nicely.