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...
You're creating an array of pointers here:
Totscore *st[MAX];
But you're passing the first element:
PrintArray(*st);
Changing your function to take a pointer to the entire array should work:
void PrintArray(Totscore **stu){
for(int i=0; i< MAX; i++){
stu[i]->Display();
}
}
PrintArray(st);
Few major problems leading to undefined behavior:
Student does not initialize its char array.Well, there is also a memory leak, many unnecessary operations just to initialize data. You should use constructor initialization lists instead. There is no strcpy_s function in C/C++, it is a terrible Microsoft invention. Why not to use portable strncpy instead?
Here is a bit improved code:
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 5 //size of array
//Class 'Student'
class Student
{
public:
Student (int num = 0) : num (num)
{
name[0] = '\0';
}
Student (const char *name, int num = 0) : num (num)
{
strncpy (this->name, name, sizeof (this->name));
}
int num;
char name[10];
};
//Class 'Lscore' extends Student (virtual)
class Lscore : virtual public Student
{
public:
Lscore (int eng = 0) : eng (eng) {}
int eng;
};
//Class 'Nscore' extends Student (virtual)
class Nscore : virtual public Student
{
public:
Nscore (int math = 0) : math (math) {}
int math;
};
//Class 'Totscore' extends Lscore, Nscore
class Totscore : public Lscore, public Nscore
{
public:
Totscore() {} //Constructor1
Totscore(const char *name, int num, int eng, int math); //Constructor2
void Display(); //Print Myself
};
//Constructor2
Totscore::Totscore(const char *name, int num, int eng, int math)
: Student (name, num), Lscore (eng), Nscore (math)
{
}
//Print Myself
void Totscore::Display(){
cout<<this->num<<" "<<this->name<<" "
<<this->eng<<" "<<this->math<<" \n";
}
//Print Array (--- Problem Part !! ---)
void PrintArray(Totscore **stu){
for(int i=0; i< MAX; ++i){
stu[i]->Display();
}
}
void DeleteArray(Totscore **stu){
for(int i=0; i< MAX; ++i){
delete stu[i];
}
}
//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);
DeleteArray (st);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With