关于VC++6运行程序后出现“出现一个问题,导致程序停止正常工作。请关闭该程序”,望解决,谢谢!
以下是程序代码:#include<iostream.h>#include<string.h>classStudent{public:Student(char*pName,...
以下是程序代码:
#include<iostream.h>
#include<string.h>
class Student
{
public:
Student(char* pName,int xHours,float xgpa)
{
cout<<"constructing student "<<pName<<'\t'<<xHours<<'\t'<<xgpa<<endl;
name=new char[strlen(pName)+1];
if(name!=0)
{
strcpy(name,pName);
}
semesHours=xHours;
gpa=xgpa;
}
~Student()
{
cout<<"destructing "<<name<<'\t'<<semesHours<<'\t'<<gpa<<endl;
name[0]='\0';
delete name;
}
Student(){}
protected:
char * name;
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher.\n";
}
~Teacher()
{
cout<<"destructing teacher.\n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"construting tutorpair.\n";
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair.\n";
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
void main()
{
Student ss("Jenny",22,3.5);
cout<<"\n";
TutorPair fg;
cout<<"back in main.\n";
} 展开
#include<iostream.h>
#include<string.h>
class Student
{
public:
Student(char* pName,int xHours,float xgpa)
{
cout<<"constructing student "<<pName<<'\t'<<xHours<<'\t'<<xgpa<<endl;
name=new char[strlen(pName)+1];
if(name!=0)
{
strcpy(name,pName);
}
semesHours=xHours;
gpa=xgpa;
}
~Student()
{
cout<<"destructing "<<name<<'\t'<<semesHours<<'\t'<<gpa<<endl;
name[0]='\0';
delete name;
}
Student(){}
protected:
char * name;
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher.\n";
}
~Teacher()
{
cout<<"destructing teacher.\n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"construting tutorpair.\n";
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair.\n";
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
void main()
{
Student ss("Jenny",22,3.5);
cout<<"\n";
TutorPair fg;
cout<<"back in main.\n";
} 展开
3个回答
2012-06-01
展开全部
问题在 Student 类:
第一、Student 类的析构函数定义写错。
第二、需要定义 Student 类的默认构造函数。
参考以下代码:
class Student
{
public:
Student( char * pName = "no name", int xHours = 0, float xgpa = 0.0 )
{
cout << "constructing student " << pName << '\t' << xHours << '\t' << xgpa << endl;
name = new char[strlen(pName)+1];
if ( name != 0 )
{
strcpy( name, pName );
}
semesHours = xHours;
gpa = xgpa;
}
~Student()
{
cout << "destructing " << name << '\t' << semesHours << '\t' << gpa << endl;
delete [] name;
}
protected:
char * name;
int semesHours;
float gpa;
};
第一、Student 类的析构函数定义写错。
第二、需要定义 Student 类的默认构造函数。
参考以下代码:
class Student
{
public:
Student( char * pName = "no name", int xHours = 0, float xgpa = 0.0 )
{
cout << "constructing student " << pName << '\t' << xHours << '\t' << xgpa << endl;
name = new char[strlen(pName)+1];
if ( name != 0 )
{
strcpy( name, pName );
}
semesHours = xHours;
gpa = xgpa;
}
~Student()
{
cout << "destructing " << name << '\t' << semesHours << '\t' << gpa << endl;
delete [] name;
}
protected:
char * name;
int semesHours;
float gpa;
};
展开全部
#include<iostream.h>
#include<string.h>
class Student
{
public:
Student(char* pName,int xHours,float xgpa)
{
cout<<"constructing student "<<pName<<'\t'<<xHours<<'\t'<<xgpa<<endl;
name=new char[strlen(pName)+1];
if(name!=0)
{
strcpy(name,pName);
}
semesHours=xHours;
gpa=xgpa;
}
~Student()
{
if(name)
{
cout<<"destructing "<<name<<'\t'<<semesHours<<'\t'<<gpa<<endl;
name[0]='\0';
delete name;
}
}
Student()
{
name = NULL;
semesHours = 0;
gpa = 0;
}
protected:
char * name;
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher.\n";
}
~Teacher()
{
cout<<"destructing teacher.\n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"construting tutorpair.\n";
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair.\n";
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
void main()
{
Student ss("Jenny",22,3.5);
cout<<"\n";
TutorPair fg;
cout<<"back in main.\n";
}
//记得给分哦
#include<string.h>
class Student
{
public:
Student(char* pName,int xHours,float xgpa)
{
cout<<"constructing student "<<pName<<'\t'<<xHours<<'\t'<<xgpa<<endl;
name=new char[strlen(pName)+1];
if(name!=0)
{
strcpy(name,pName);
}
semesHours=xHours;
gpa=xgpa;
}
~Student()
{
if(name)
{
cout<<"destructing "<<name<<'\t'<<semesHours<<'\t'<<gpa<<endl;
name[0]='\0';
delete name;
}
}
Student()
{
name = NULL;
semesHours = 0;
gpa = 0;
}
protected:
char * name;
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher.\n";
}
~Teacher()
{
cout<<"destructing teacher.\n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"construting tutorpair.\n";
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair.\n";
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
void main()
{
Student ss("Jenny",22,3.5);
cout<<"\n";
TutorPair fg;
cout<<"back in main.\n";
}
//记得给分哦
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
无法生成可执行文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询