继承和组合
对于类,在下还是不明白。。。我编了一个类的程序,又提示同样的错误。麻烦各位看一下(红色):提示错误如下:C:\Documents and Settings\wys\桌面\c++\继承和组合.cpp(32) : fatal error C1083: Cannot open include file: 'coursetype.h': No such file or directory
Error executing cl.exe.
//header file coursetype.h
#ifndef H_coursetype
#define H_coursetype
#include <fstream>
#include <string>
using namespace std;
class coursetype
{
private:
string coursename;
string courseno;
int coursecredits;
public:
void setcourseinfo(string cname, string cno, int credits);
void print(ofstream& outf) const;
int getcredits() const;
string getcoursenumber() const;
string getcoursename() const;
coursetype(string cname=" ", string cno=" ", int credits=0);
};
#endif
//header file coursetype.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "coursetype.h"
using namespace std;
void coursetype::setcourseinfo(string cname, string cno, int credits)
{
coursename=cname;
courseno=cno;
coursecredits=credits;
}
void coursetype::print(ofstream& outf) const
{
outf<<courseno<<'\n';
outf<<coursename<<'\n';
outf<<coursecredits<<'\n';
}
int coursetype::getcredits() const
{
return coursecredits;
}
string coursetype::getcoursenumber() const
{
return courseno;
}
string coursetype::getcoursename() const
{
return coursename;
}
coursetype::coursetype(string cname, string cno, int credits)
{
coursename=cname;
courseno=cno;
coursecredits=credits;
}
//header file persontype.h
#ifndef H_persontype
#define H_persontype
#include <string>
using namespace std;
class persontype
{
private:
string firstname;
string lastname;
public:
void setname(string first, string last);
void print() const;
string getfirstname() const;
string getlastname() const;
persontype(string first=" ", string last=" ");
};
#endif
//header file persontype.cpp
#include <string>
#include <iostream>
#include "persontype.h"
using namespace std;
void persontype::setname(string first, string last)
{
firstname=first;
lastname=last;
}
void persontype::print() const
{
cout<<firstname<<" "<<lastname;
}
string persontype::getfirstname() const
{
return firstname;
}
string persontype::getlastname() const
{
return lastname;
}
persontype::persontype(string first, string last)
{
firstname=first;
lastname=last;
}
//header file studenttype.h
#ifndef H_studenttype
#define H_studenttype
#include <fstream>
#include <string>
#include "persontype.h"
#include "coursetype.h"
using namespace std;
class studenttype:public persontype
{
private:
coursetype coursesenrolled[6];
int sid;
bool istuitionpaid;
char coursesgrade[6];
int numberofcourses;
void sortcourses();
public:
void setinfo(string fname, string lname, int id, int nofcoureses, bool istpaid, coursetype courses[], char cgrades[]);
void print(ofstream& outf, double tuitionrate);
int gethoursenrolled();
double getgpa();
double billingamout(double tuitionrate);
studenttype();
};
#endif
//header file studenttype.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "persontype.h"
#include "coursetype.h"
#include "studenttype.h"
using namespace std;
void studenttype::setinfo(string fname, string lname, int id, int nofcoureses, bool istpaid, coursetype courses[], char cgrades[])
{
int i;
setname(fname,lname);
sid=id;
istuitionpaid=istpaid;
numberofcourses=nofcourses;
for(i=0;i<numberofcourses;i++)
{
coursesenrolled[i]=courses[i];
coursesgrade[i]=cgrades[i];
}
sortcourses();
}
studenttype::studenttype()
{
numberofcourses=0;
sid=0;
istuitionpaid=false;
for(int i=0; i<6;i++)
coursesgrade[i]='*';
}
void studenttype::print(ofstream& outf, double tuitionrate)
{
int i;
outf<<"Student name: "<<getfirstname()<<" "<<getlastname()<<endl;
outf<<"Student id: "<<sid<<endl;
outf<<"Number of courses enrolled: "<<numberofcourses<<endl;
outf<<endl;
outf<<left;
outf<<"course no"<<'\n';
outf<<"Credits"<<'\n';
outf<<"grade"<<'\n';
for(i=0;i<numberofcourses;i++)
{
coursesenrolled[i].print(outf);
if(istuitionpaid)
outf<<coursesgrade[i]<<endl;
else
outf<<"***"<<endl;
}
outf<<endl;
outf<<"Total number of credit hours:"<<gethoursenrolled()<<endl;
outf<<fixed<<showpoint<<setprecision(2);
if(istuitionpaid)
outf<<"Mid-semester GPA: "<<getgpa()<<endl;
else
{
outf<<"***grades are being held for not paying the tuition.***"<<endl;
outf<<"Amount due: $"<<billingamount(tuitionrate)<<endl;
}
outf<<"_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"<<endl<<endl;
}
int studenttype::gethoursenrolled()
{
int totalcredits=0;
int i;
for(i=0;i<numberofcourses;i++)
totalcredits+=coursesenrolled[i].getcredits();
return totalcredits;
}
double studenttype::billingamount(double tuitionrate)
{
return tuitionrate*gethourseenrolled();
}
double studenttype::getgpa()
{
int i;
double sum=0;
for(i=0;i<numberofcourses;i++)
{
switch(coursesenrolled[i].getgrade())
{
case 'A': sum+=coursesenrolled[i].getcredits()*4;
break;
case 'B': sum+=coursesenrolled[i].getcredits()*3;
break;
case 'C': sum+=coursesenrolled[i].getcredits()*2;
break;
case 'D': sum+=coursesenrolled[i].getcredits()*1;
break;
case 'F': sum+=coursesenrolled[i].getcredits()*0;
break;
default: cout<<"Invalid course grade"<<endl;
}
}
return sum/gethourseenrolled();
}
void studenttype::sortcourses()
{
int i,j;
int minindex;
coursetype temp;
char tempgrade;
string course1;
string course2;
for(i=0;i<numberofcourses-1;i++)
{
minindex=i;
for(j=i+1;j<numberofcourses;j++)
{
course1=coursesenrolled[minindex].getcoursenumber();
course2=coursesenrolled[j].getcoursenumber();
if(course1>course2)
minindex=j;
}
temp=coursesenrolled[minindex];
coursesenrolled[minindex]=coursesenrolled[i];
coursesenrolled[i]=temp;
tempgrade=coursesgrade[minindex];
coursesgrade[minindex]=coursesgrade[i];
coursesgrade[i]=tempgrade;
}
}
//main()
#include <iostream>
#include <fstream>
#include <string>
#include "studenttype.h"
using namespace std;
const int maxnumberofstudents=10;
void getstudentdata(ifstream& infile, studenttype studentlist[], int numberofstudents);
void printgradereports(ofstream& outfile, studenttype studentlist[], int numberofstudents, double tuitionrate);
int main()
{
studenttype studentlist[maxnumberofstudents];
int noofstudents;
double tuitionrate;
ifstream infile;
ofstream outfile;
infile.open("E:\\c++学习\\继承和组合.txt");
if(!infile)
{
cout<<"The input file does not exist."<<"program terminates."<<endl;
return 1;
}
outfile.open("E:\\c++学习\\继承和组合1.txt",ios::app);
infile>>noofstudents;
infile>>tuitionrate;
getstudentdata(infile,studentlist,noofstudents);
printgradereports(outfile,studentlist,noofstudents,tuitionrate);
return 0;
}
void getstudentdata(ifstream& infile, studenttype studentlist[], int numberofstudents)
{
string fname;
string lname;
int id;
int noofcourses;
char ispaid;
bool istuitionpaid;
string cname;
string cno;
int credits;
int count;
int i;
coursetype courses[6];
char cgreades[6];
for(count=0;count<numberofstudents;count++)
{
iflile>>fname>>lname>>id>>ispaid;
if(ispaid=='y')
istuitionpaid=true;
else
istuitionpaid=false;
infile>>noofcourses;
for(i=0;i<noofcourses;i++)
{
infile>>cname>>cno>>credits>>cgrades[i];
courses[i].setcourseinfo(cname,cno,credits);
}
studentlist[count].setinfo(fname,lname,id,noofcourses,istuitionpaid,courses,cgrades);
}
}
void printgradereports(ofstream& outfile, studenttype studentlist[],int numberofstudents, double tuitionrate)
{
int count;
for(count=0;count<numberofstudents;count++)
{
studentlist[count].print(outfile,tuitionrate);
}
}