problem in overloading constructors

#include <iostream>
#include<cstring>

using namespace std;

class student
{
protected:
char* pszName;
int nID;
double dGrade;
int nSemesterHours;
public:
student(char* pszNewName,int nNewID,double dXferGrade,int nXferHours)
{
cout<<" Constructing "<<pszNewName<<" as a transfer student. "<<endl;
int nLength=strlen(pszNewName)+1;
pszName=new char[nLength];
strcpy(pszName,pszNewName);
nID=nNewID;
dGrade=dXferGrade;
nSemesterHours=nXferHours;
}
student(const char* pszNewName,int nNewID)
{
cout<<" Constructing "<<pszNewName<<" as a new student. "<<endl;
int nLength=strlen(pszNewName)+1;
pszName=new char[nLength];
strcpy(pszName,pszNewName);
nID=nNewID;
dGrade=0.0;
nSemesterHours=0;
}
student()
{
pszName=0;
nID=0;
dGrade=0;
nSemesterHours=0;
}
~student()
{
cout<<" Destructing "<<pszName<<endl;
delete[] pszName;
pszName=0;
}
const char* getName()
{
return pszName;
}
int getID()
{
return nID;
}
double getGrade()
{
return dGrade;
}
int getHours()
{
return nSemesterHours;
}
double addGrade(double dNewGrade,int nHours)
{
double dWtdHrs=dGrade*nSemesterHours;
dWtdHrs+=dNewGrade*nHours;
nSemesterHours+=nHours;
dGrade=dWtdHrs/nSemesterHours;
return dGrade;
}
};
int main()
{

student student("Rasoul Pourabdollah Vardin",1234);
student xfer("Shayan Pourabdollah Vardin",5678,3.5,12);
student.addGrade(3.0,3);
xfer.addGrade(3.0,3);
cout<< " student "<<student.getName()<<" has a grade of "<<student.getGrade()<<endl;
cout<<" student "<<xfer.getName()<<" has a grade of "<<xfer.getGrade()<<endl;

return 0;
}




||=== Build: Debug in p308 (compiler: GNU GCC Compiler) ===|
C:\Users\rasoul\Desktop\c\Example\p308\main.cpp||In function 'int main()':|
C:\Users\rasoul\Desktop\c\Example\p308\main.cpp|76|error: expected ';' before 'xfer'|
C:\Users\rasoul\Desktop\c\Example\p308\main.cpp|76|warning: statement has no effect [-Wunused-value]|
C:\Users\rasoul\Desktop\c\Example\p308\main.cpp|78|error: 'xfer' was not declared in this scope|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|

Last edited on
The problem is that you named the variable the same as the class.

This works:
1
2
student student("Rasoul Pourabdollah Vardin",1234);
::student xfer("Shayan Pourabdollah Vardin",5678,3.5,12);

But I suggest that you instead pick another name for the first variable.
Last edited on
Topic archived. No new replies allowed.