| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 707 人关注过本帖
标题:继承和组合
只看楼主 加入收藏
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:7 
继承和组合
对于类,在下还是不明白。。。我编了一个类的程序,又提示同样的错误。麻烦各位看一下(红色):
提示错误如下: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);
    }
}


搜索更多相关主题的帖子: include private Documents 继承 
2012-11-01 16:39
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3452
专家分:19340
注 册:2012-3-31
收藏
得分:15 
先看看
收到的鲜花
  • 寒风中的细雨2012-11-01 18:07 送鲜花  10朵   附言:这娃 到处都有你的‘脚印’ 下次换句台词 ...

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-11-01 17:50
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3452
专家分:19340
注 册:2012-3-31
收藏
得分:0 
回复 2楼 青春无限
难道不行吗????/???羡慕嫉妒恨吗???

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-11-01 18:09
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
难道是我c++的问题吗。。。我运行的时候,就提示上面红色的语句??
2012-11-01 18:30
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
为了检验,我又编了一个关于类的小程序,结果也是提示同样的错误:
c:\documents and settings\wys\桌面\c++\指针.cpp(15) : fatal error C1083: Cannot open include file: 'wu.h': No such file or directory
Error executing cl.exe.

#ifndef H_wu
#define H_wu
class wu
{
private:
    int x;
public:
    void setx(int a);
    void print() const;
};
#endif

#include <iostream>
#include "wu.h"

using namespace std;

void wu::setx(int a)
{
    x=a;
}

void wu::print() const
{
    cout<<"x="<<x<<endl;
}

#include <iostream>
#include "wu.h"

using namespace std;

int main()
{
    wu a;
    wu *b;
    b=&a;

    b->setx(5);
    b->print();
   
    return 0;
}
请各位同仁看一下,在下不胜感激。
2012-11-01 19:07
senpujituan
Rank: 4
等 级:业余侠客
帖 子:91
专家分:203
注 册:2012-6-29
收藏
得分:15 
回复 5楼 wuyushuai521
我运行了没错误,原来是你机器的问题,哈哈!!
2012-11-01 19:18
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
你能把你的c++送给我吗,我安装一下。谢谢了
2012-11-01 19:35
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
我把所有的自定义函数头文件删除以后,就可以运行了。。。。

//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<<'\t'<<'\t';
    outf<<coursename<<'\t'<<'\t'<<'\t';

    outf<<coursecredits<<'\t'<<'\t'<<'\t';
}

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 nofcourses, 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"<<'\t'<<'\t';
    outf<<"course name"<<'\t'<<'\t';
    outf<<"Credits"<<'\t'<<'\t';
    outf<<"                 grade"<<'\t';
    outf<<endl;

    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: $"<<billingamout(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::billingamout(double tuitionrate)
{
    return tuitionrate*gethoursenrolled();
}

double studenttype::getgpa()
{
    int i;
    double sum=0;

    for(i=0;i<numberofcourses;i++)
    {
        switch(coursesgrade[i])
        {
        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/gethoursenrolled();
}

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 cgrades[6];

    for(count=0;count<numberofstudents;count++)
    {
        infile>>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);
    }
}


2012-11-01 20:17
快速回复:继承和组合
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029326 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved