| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4055 人关注过本帖, 1 人收藏
标题:帮忙分析一下这个程序的每一步,我是初学者,因为刚转专业,没学过c++,谢谢 ...
只看楼主 加入收藏
_Strike
Rank: 2
等 级:论坛游民
帖 子:133
专家分:72
注 册:2016-3-22
结帖率:90.63%
收藏(1)
已结贴  问题点数:15 回复次数:4 
帮忙分析一下这个程序的每一步,我是初学者,因为刚转专业,没学过c++,谢谢了。
#include <iostream>
 using namespace std;
 class rectangle{
 public:
     rectangle(int len=10,int wid=10);
     rectangle(const rectangle &p);
     ~rectangle();
     void disp()
     {cout<<length<<"  "<<width<<endl;}
     void showarea();
     void setlengthandwidth();
 private:
     int length,width;
 };
 rectangle::rectangle(int len, int wid)
 { length=len;
   width=wid;
   cout<<"Using  normal constructor\n";
 }
 rectangle::rectangle(const rectangle &p)
 { length=2*p.length;
   width=2*p.width;
   cout<<"Using copy constructor\n";
 }
 rectangle::~rectangle()
 {cout<<"destructing..."<<endl;
 }
 void rectangle::showarea()
 {cout<<"该矩形的面积为:"<< length*width<<endl;}
 void rectangle::setlengthandwidth()
 {cout<<"该矩形的周长为:"<<(length+width)*2<<endl;}
 int main()
 {rectangle rec1(5,5);
 cout<<"rectangle1 output1:"<<endl;
 rec1.showarea();
 rec1.setlengthandwidth();
 return 0;
 }
搜索更多相关主题的帖子: private include public normal 
2016-04-21 21:58
_Strike
Rank: 2
等 级:论坛游民
帖 子:133
专家分:72
注 册:2016-3-22
收藏
得分:0 
也没学过c
2016-04-21 22:19
c974288432
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:44
专家分:104
注 册:2015-7-18
收藏
得分:5 
#include <iostream>
 using namespace std;
 class rectangle{
 public:
     rectangle(int len=10,int wid=10); // 与类同名 构造函数
     rectangle(const rectangle &p);    //用类对象为参数
     ~rectangle();                    //析构
     void disp()
     {cout<<length<<"  "<<width<<endl;}
     void showarea();
     void setlengthandwidth();
 private:
     int length,width;
 };
 rectangle::rectangle(int len, int wid)  
 { length=len;
   width=wid;
   cout<<"Using  normal constructor\n";
 }
 rectangle::rectangle(const rectangle &p)
 { length=2*p.length;
   width=2*p.width;
   cout<<"Using copy constructor\n";
 }
 rectangle::~rectangle()
 {cout<<"destructing..."<<endl;
 }
 void rectangle::showarea()                       
 {cout<<"该矩形的面积为:"<< length*width<<endl;}
 void rectangle::setlengthandwidth()
 {cout<<"该矩形的周长为:"<<(length+width)*2<<endl;}
 int main()
 {rectangle rec1(5,5);               //REC1  是类RECANGLE1对象  REC1(5,5)构造函数在建对象时调用
 cout<<"rectangle1 output1:"<<endl;  
 rec1.showarea();               
 rec1.setlengthandwidth();
 return 0;
 }

你调试  看看运行的每步   把不清楚全部标出  我也是新手   Q :974288432  
2016-04-22 08:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
初学者更应该细致严谨,否则这样子下去学废了就没救了。

程序代码:
class rectangle
{
public:
    rectangle();
    rectangle( int width, int length );

    int perimeter() const;
    int area() const;

private:
    int width_, length_;
};

rectangle::rectangle() : width_(0), length_(0)
{
}
rectangle::rectangle( int width, int length ) : width_(width), length_(length)
{
}
int rectangle::perimeter() const
{
    return 2*(width_+length_);
}
int rectangle::area() const
{
    return width_*length_;
}

#include <iostream>
using namespace std;

int main( void )
{
    rectangle rect( 5, 5 );

    cout << "rectangle output:\n"
         << "    面积 = " << rect.area() << '\n'
         << "    周长 = " << rect.perimeter() << endl;

    return 0;
}

2016-04-22 12:17
c974288432
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:44
专家分:104
注 册:2015-7-18
收藏
得分:5 
回复 4楼 rjsp

class rectangle
{
public:
    rectangle();
    rectangle( int width, int length );

    int perimeter() const ;
    int area() const;

private:
    int width_, length_;
};

rectangle::rectangle() : width_(0), length_(0)
{
}
rectangle::rectangle( int width, int length ) : width_(width), length_(length) //为什么:: width_(width), length_(length)
{
 //width_=width;
 //length_=length;
}
int rectangle::perimeter() const
{
    return 2*(width_+length_);
}
int rectangle::area() const
{
    return width_*length_;
}

#include <iostream>            //还可以放这  
using namespace std;           //

int main( void )
{
    rectangle rect( 5, 5 );

    cout << "rectangle output:\n"
         << "    面积 = " << rect.area() << '\n'
         << "    周长 = " << rect.perimeter() << endl;

    return 0;
}
//缩小了
2016-04-22 21:15
快速回复:帮忙分析一下这个程序的每一步,我是初学者,因为刚转专业,没学过c++ ...
数据加载中...
 
   



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

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