标题:连接错误。。--------------------Configuration: 派生 - Win32 Debug----- ...
只看楼主
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
结帖率:90.91%
已结贴  问题点数:10 回复次数:10 
连接错误。。--------------------Configuration: 派生 - Win32 Debug--------------------
程序代码:
形状(一)
    编写C++程序完成以下功能:
声明一个基类Shape(形状),其中包含一个方法来计算面积;
从Shape派生两个类矩形和圆形;
从矩形派生正方形;
分别实现派生类构造函数、析构函数和其他方法;
创建派生类的对象,观察构造函数、析构函数调用次序;
不同对象计算面积。


#include<iostream>
using namespace std;
const double Pi=3.14;
class shape
{
   public:
      shape(){cout<<"shape constructor called"<<endl;}
      double Getarea(double x,double y){return area=x*y;}
      ~shape(){cout<<"shape destructor called"<<endl;}
   private:
      double area;
};
class rectangle:public shape
{
   public:
       rectangle();
       double rect(double X,double Y){return Getarea(X,Y);cout<<"rectangle constructor called"<<endl;}
        ~rectangle(){cout<<"rectangle destructor called"<<endl;}
   
};
class circle:public shape
{  public:
      circle();
      double cir_area(double r){cout<<"circle constructor called"<<endl;return  Pi*Getarea(r,r);}
      ~circle(){cout<<"circle destructor called"<<endl;}
};
class square:public rectangle
{
   public:
       square();
       double squa(double X){cout<<"square constructor called"<<endl;return Getarea(X,X);}
       ~square(){cout<<"aquare destructor called"<<endl;}
};
int main()
{   double l,w,r,x;
   
    cout<<"请输入长方形的长与宽"<<endl;
    cin>>l>>w;
    rectangle rec();
    cout<<"the area of rec is:"<<rec.rect(l,w)<<endl;
    cout<<"请输入圆的半径"<<endl;
    cin>>r;
    circle cir( );
    cout<<"the area of cir is:"<<cir.cir_area(r)<<endl;
    cout<<"请输入正方形的边长"<<endl;
    cin>>x;
    square squ( );
    cout<<"the area of squ is:"<<squ.squa(x)<<endl;
    return 0;
}


[ 本帖最后由 fishviv 于 2010-9-11 10:41 编辑 ]
搜索更多相关主题的帖子: 派生 Debug 
2010-09-11 09:45
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
得分:0 
-------------------Configuration: 派生 - Win32 Debug--------------------
Compiling...
派生.cpp
C:\Users\zcy\Desktop\zz5z\派生.cpp(47) : error C2228: left of '.rect' must have class/struct/union type
C:\Users\zcy\Desktop\zz5z\派生.cpp(51) : error C2228: left of '.cir_area' must have class/struct/union type
C:\Users\zcy\Desktop\zz5z\派生.cpp(55) : error C2228: left of '.squa' must have class/struct/union type


[ 本帖最后由 fishviv 于 2010-9-11 10:43 编辑 ]
2010-09-11 09:46
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:10 
不要将派生类的成员 double area;
同名同类型 仔细看书,派生的子类拥有一些什么你都没明白
2010-09-11 10:06
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
得分:0 
回复 3楼 hahayezhe
#include<iostream>
using namespace std;
const double Pi=3.14;
class shape
{
   public:
      shape(){cout<<"shape constructor called"<<endl;}
      double Getarea(double x,double y){return area=x*y;}
      ~shape(){cout<<"shape destructor called"<<endl;}
   private:
      double area;
};
class rectangle:public shape
{
   public:
       rectangle();
       double rect(double X,double Y){return Getarea(X,Y);cout<<"rectangle constructor called"<<endl;}
      
       ~rectangle(){cout<<"rectangle destructor called"<<endl;}
   
};
class circle:public shape
{  public:
      circle();
      double cir_area(double r){cout<<"circle constructor called"<<endl;return  Pi*Getarea(r,r);}
      ~circle(){cout<<"circle destructor called"<<endl;}
};
class square:public rectangle
{
   public:
       square();
       double squa(double X){cout<<"square constructor called"<<endl;return Getarea(X,X);}
       ~square(){cout<<"aquare destructor called"<<endl;}
};
int main()
{   double l,w,r,x;
   
    cout<<"请输入长方形的长与宽"<<endl;
    cin>>l>>w;
    rectangle rec();
    cout<<"the area of rec is:"<<rec.rect(l,w)<<endl;
    cout<<"请输入园的半径"<<endl;
    cin>>r;
    circle cir( );
    cout<<"the area of cir is:"<<cir.cir_area(r)<<endl;
    cout<<"请输入正方形的边长"<<endl;
    cin>>x;
    square squ( );
    cout<<"the area of squ is:"<<squ.squa(x)<<endl;
    return 0;
}
现在呢,为什么还说rect没有类啊?
2010-09-11 10:34
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
得分:0 
形状(一)//这是原题目
    编写C++程序完成以下功能:
声明一个基类Shape(形状),其中包含一个方法来计算面积;
从Shape派生两个类矩形和圆形;
从矩形派生正方形;
分别实现派生类构造函数、析构函数和其他方法;
创建派生类的对象,观察构造函数、析构函数调用次序;
不同对象计算面积。
2010-09-11 10:36
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:0 
请在每个默认构造后面加上{}
你的构造函数 只声明 未定义
2010-09-11 11:01
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:0 
最后结贴 分给我
2010-09-11 11:02
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
得分:0 
回复 7楼 hahayezhe
我看书上的默认构造函数就是  类名()  就好了啊,再说我加上{}之后,还是这个错误啊。
还有,什么是分给你啊?
2010-09-11 11:08
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
得分:0 
孩子 请将rectangle rec();后的括号去掉 rectangle rec;后面的一样!
最后请开窍吧
2010-09-11 11:14
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
得分:0 
好了,调好了,谢谢您的帮忙..麻烦再看看有什么不专业的地方。
#include<iostream>
using namespace std;
const double Pi=3.14;
class shape
{
   public:
      shape(){cout<<"shape constructor called"<<endl;}
      double Getarea(double x,double y){return area=x*y;}
      ~shape(){cout<<"shape destructor called"<<endl;}
   private:
      double area;
};
class rectangle:public shape
{
   public:
       rectangle(){cout<<"rectangle constructor called"<<endl;}
       double rect(double X,double Y){return Getarea(X,Y);}
       ~rectangle(){cout<<"rectangle destructor called"<<endl;}
   
};
class circle:public shape
{  public:
      circle(){cout<<"circle constructor called"<<endl;}
      double cir_area(double r){return  Pi*Getarea(r,r);}
      ~circle(){cout<<"circle destructor called"<<endl;}
};
class square:public rectangle
{
   public:
       square(){cout<<"square constructor called"<<endl;}
       double squa(double X){return Getarea(X,X);}
       ~square(){cout<<"aquare destructor called"<<endl;}
};
int main()
{   double l,w,r,x;
   
    cout<<"请输入长方形的长与宽"<<endl;
    cin>>l>>w;
    rectangle rec ;
    cout<<"the area of rec is:"<<rec.rect(l,w)<<endl;
    cout<<"请输入圆的半径"<<endl;
    cin>>r;
    circle cir ;
    cout<<"the area of cir is:"<<cir.cir_area(r)<<endl;
    cout<<"请输入正方形的边长"<<endl;
    cin>>x;
    square squ ;
    cout<<"the area of squ is:"<<squ.squa(x)<<endl;
    return 0;
}

2010-09-11 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-320104-1-1.html




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

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