| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:类的继承
只看楼主 加入收藏
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
类的继承
我编了一个类的程序,但是编译时提示以下错误(红颜色标记的),麻烦各位看看。非常感谢:
C:\Documents and Settings\wys\桌面\c++\继承.cpp(18) :fatal error C1083: Cannot open include file: 'rectangle.h': No such file or directory
class rectangle
{
private:
    double length;
    double width;
public:
    void setdimension(double l, double w);
    double getlength() const;
    double getwidth() const;
    double area() const;
    double perimeter() const;
    void print()  const;
    rectangle();
    rectangle(double l, double w);
};

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

using namespace std;

void rectangle::setdimension(double l, double w)
{
    if(l>0)
        length=l;
    else
        length=0;
    if(w>0)
        length=w;
    else
        length=0;
}

double rectangle::getlength() const
{
    return length;
}

double  rectangle::getwidth() const
{
    return  width;
}

double  rectangle::area() const
{
    return length*width;
}
double  rectangle::perimeter() const
{
    return  2*(length+width);
}

void rectangle::print()  const
{
    cout<<"length="<<length;
    cout<<"width="<<width;
}

rectangle::rectangle(double l, double w)
{
    setdimension(l,w);
}

rectangle::rectangle()

{
    length=0;
    width=0;
}



class boxtype:public rectangle
{
private:
    double height;
public:
    void setdimension(double l,double w,double h);
    double getheight() const;
    double area() const;
    double volume() const;
    void print() const;
    boxtype();
    boxtype(double l, double w, double h);
};


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

using namespace std;

void boxtype::setdimension(double l, double w, double h)
{
    rectangle::setdimension(l,w);
    if(h>0)
        height=h;
    else
        height=0;
}

double boxtype::getheight() const
{
    return height;
}

double boxtype::area() const
{
    return 2*(getlength()*getwidth()+getlength()*height+getwidth()*height);
}

double boxtype::volume() const
{
    return rectangle::area()*height;
}

void boxtype::print() const
{
    rectangle::print();
    cout<<"; height="<<height;
}

boxtype::boxtype()
{
    height=0;
}

boxtype::boxtype(double l, double w, double h)
        :rectangle(l,w)
{
    if(h>0)
        height=h;
    else
        height=0;
}


#include <iostream>
#include <iomanip>
#include "rectangle.h"
#include "boxtype.h"

using namespace std;

int main()
{
    rectangle myrectangle1;
    rectangle myrectangle2(8,6);
    boxtype mybox1;
    boxtype mybox2(10,7,3);
    cout<<fixed<<showpoint<<setprecision(2);
    cout<<"line6: myrectangle1: ";
    myrectangle1.print();
    cout<<endl;
    cout<<"line 9: area of myrectangle1:"<<myrectangle1.area()<<endl;
    cout<<"line10 : myrectangle2: ";
    myrectangle2.print();
    cout<<"line 13: area of myrectangle2:"<<myrectangle2.area()<<endl;
    cout<<"line 14: mybox1:";
    mybox.print();
    cout<<endl;
    cout<<"lint 17:surface area of mybox1:";
    cout<<mybox1.area()<<endl;
    cout<<"line 18:volume of mybox1:"<<mybox1.volume()<<endl;
    cout<<"line 19: mybox2";
    mybox2.print();
    cout<<endl;
    cout<<"line 22: surface area of mybox2:"<<mybox2.area()<<endl;
    cout<<"line 24: volume of mybox2:"<<mybox2.volume()<<endl;

    return 0;
}













搜索更多相关主题的帖子: private Documents 继承 double include 
2012-10-28 17:11
超级菜鸟手
Rank: 3Rank: 3
来 自:太阳系
等 级:论坛游侠
帖 子:34
专家分:109
注 册:2012-10-24
收藏
得分:0 
#ifndef RECTANGLE_H
#define RECTANGLE_H

class rectangle
{
private:
    double length;
    double width;
public:
    void setdimension(double l, double w);
    double getlength() const;
    double getwidth() const;
    double area() const;
    double perimeter() const;
    void print()  const;
    rectangle();
    rectangle(double l, double w);
};

#endif

在做一个 header file时 一定要有 红颜色标记的
2012-10-28 17:37
超级菜鸟手
Rank: 3Rank: 3
来 自:太阳系
等 级:论坛游侠
帖 子:34
专家分:109
注 册:2012-10-24
收藏
得分:20 
其实 在你的另一个问题 已经有人说过了
https://bbs.bccn.net/thread-384716-1-1.html
2012-10-28 17:38
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
有些明白了,那么类的成员函数的定义是不是要放在类的说明后面就可以了?
2012-10-28 17:52
超级菜鸟手
Rank: 3Rank: 3
来 自:太阳系
等 级:论坛游侠
帖 子:34
专家分:109
注 册:2012-10-24
收藏
得分:0 
类的成员函数 有两种

放在类里面
class rectangle
{
private:
    double length;
    double width;
public:
    void setdimension(double l, double w){
        length = l;
        width = w;}

};

或者
放在类的说明后面

class rectangle
{
private:
    double length;
    double width;
public:
    void setdimension(double l, double w);
};

void rectangle::setdimension(double l, double w){
        length = l;
        width = w;
}


建议您多看看书! 书比较详细

[ 本帖最后由 超级菜鸟手 于 2012-10-28 18:01 编辑 ]
2012-10-28 18:00
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
是以下这样吗?我这样试一下,提示:C:\Documents and Settings\wys\桌面\c++\继承.cpp(137) : fatal error C1083: Cannot open include file: 'rectangle.h': No such file or directory
#ifndef rectangle_h  //rectangle类定义
#define rectangle_h

class rectangle
{
private:
    double length;
    double width;
public:
    void setdimension(double l, double w);
    double getlength() const;
    double getwidth() const;
    double area() const;
    double perimeter() const;
    void print()  const;
    rectangle();
    rectangle(double l, double w);
};

#endif



#ifndef boxtype_h  //boxtype类定义
#define boxtype_h
class boxtype:public rectangle
{
private:
    double height;
public:
    void setdimension(double l,double w,double h);
    double getheight() const;
    double area() const;
    double volume() const;
    void print() const;
    boxtype();
    boxtype(double l, double w, double h);
};
#endif



#include <iostream> //rectangle类成员函数定义
using namespace std;

void rectangle::setdimension(double l, double w)
{
    if(l>0)
        length=l;
    else
        length=0;
    if(w>0)
        length=w;
    else
        length=0;
}

double rectangle::getlength() const
{
    return length;
}

double  rectangle::getwidth() const
{
    return  width;
}

double  rectangle::area() const
{
    return length*width;
}
double  rectangle::perimeter() const
{
    return  2*(length+width);
}

void rectangle::print()  const
{
    cout<<"length="<<length;
    cout<<"width="<<width;
}

rectangle::rectangle(double l, double w)
{
    setdimension(l,w);
}

rectangle::rectangle()

{
    length=0;
    width=0;
}


//boxtype类成员函数定义

void boxtype::setdimension(double l, double w, double h)   
{
    rectangle::setdimension(l,w);
    if(h>0)
        height=h;
    else
        height=0;
}

double boxtype::getheight() const
{
    return height;
}

double boxtype::area() const
{
    return 2*(getlength()*getwidth()+getlength()*height+getwidth()*height);
}

double boxtype::volume() const
{
    return rectangle::area()*height;
}

void boxtype::print() const
{
    rectangle::print();
    cout<<"; height="<<height;
}

boxtype::boxtype()
{
    height=0;
}

boxtype::boxtype(double l, double w, double h)
        :rectangle(l,w)
{
    if(h>0)
        height=h;
    else
        height=0;
}

//main函数编写
#include <iostream>
#include <iomanip>
#include "rectangle.h"
#include "boxtype.h"

using namespace std;

int main()
{
    rectangle myrectangle1;
    rectangle myrectangle2(8,6);
    boxtype mybox1;
    boxtype mybox2(10,7,3);
    cout<<fixed<<showpoint<<setprecision(2);
    cout<<"line6: myrectangle1: ";
    myrectangle1.print();
    cout<<endl;
    cout<<"line 9: area of myrectangle1:"<<myrectangle1.area()<<endl;
    cout<<"line10 : myrectangle2: ";
    myrectangle2.print();
    cout<<"line 13: area of myrectangle2:"<<myrectangle2.area()<<endl;
    cout<<"line 14: mybox1:";
    mybox.print();
    cout<<endl;
    cout<<"lint 17:surface area of mybox1:";
    cout<<mybox1.area()<<endl;
    cout<<"line 18:volume of mybox1:"<<mybox1.volume()<<endl;
    cout<<"line 19: mybox2";
    mybox2.print();
    cout<<endl;
    cout<<"line 22: surface area of mybox2:"<<mybox2.area()<<endl;
    cout<<"line 24: volume of mybox2:"<<mybox2.volume()<<endl;

    return 0;
}













2012-10-28 18:02
超级菜鸟手
Rank: 3Rank: 3
来 自:太阳系
等 级:论坛游侠
帖 子:34
专家分:109
注 册:2012-10-24
收藏
得分:0 
想问下 您的代码是 都写在一起 还是 有分.h 和.cpp 文件?
2012-10-28 18:06
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
我是想在main函数中不显示类,直接调用。我给您发一个wps文档,里面是我做的详细步骤,麻烦您看看对不对。非常感谢。。
类.zip (43.47 KB)
2012-10-28 18:13
超级菜鸟手
Rank: 3Rank: 3
来 自:太阳系
等 级:论坛游侠
帖 子:34
专家分:109
注 册:2012-10-24
收藏
得分:0 
基本上你需要 3个文件


#ifndef A_H
#define A_H
class A{
//类的成员函数 };
#endif        //存为a.h 文件


#include <iostream>
#include "a.h"
using namespace std;
// 类成员函数定义   // 存为a.cpp 文件


#include <iostream>
#include "a.h"
int main(){
    ....//你的代码
} // main 文件
2012-10-28 18:26
wuyushuai521
Rank: 2
等 级:论坛游民
帖 子:80
专家分:47
注 册:2012-10-9
收藏
得分:0 
同仁,小弟不才。请问“//存为a.h 文件,// 存为a.cpp 文件”如何操作?是不是在新建中,输入类名后,直接在后面加上".h"或者".cpp"。。。

我按同仁的意思做了,还是提示同样的错误:C:\Documents and Settings\wys\桌面\c++\继承.cpp(137) : fatal error C1083: Cannot open include file: 'rectangle.h': No such file or directory




2012-10-28 19:23
快速回复:类的继承
数据加载中...
 
   



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

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