首页 > 编程语言 > 详细

C++作业3

时间:2018-04-08 23:51:27      阅读:255      评论:0      收藏:0      [点我收藏+]
//4-11

#include<iostream> using namespace std; class Rectangle{ public: Rectangle(double L=1,double W=1); Rectangle(Rectangle &r); ~Rectangle(){}; void show(double L,double W); private: double length; double width; }; Rectangle::Rectangle(double L,double W){ length=L; width=W; } Rectangle::Rectangle(Rectangle &r){ length = r.length; width = r.width; } void Rectangle::show(double L,double W) { length=L; width=W; cout<<"length:"<<length<<endl; cout<<"width:"<<width<<endl; cout<< "area:"<<length*width<<endl; } int main(){ double length,width; cin>>length>>width; Rectangle area(length,width); area.show(length,width); return 0; }

技术分享图片

#include<iostream> 
using namespace std;
class Complex
{
    public:
    Complex(double r0=0,double i0=0);
    void add(Complex &c0);
    void show()
    {
        cout<<"complex:"<<Real<<"+"<<imaginary<<"i"<<endl;
    }
    
    private:
        double Real;
        double imaginary; 
    
    
    
};
Complex::Complex(double r0,double i0)
{
    Real=r0;
    imaginary=i0;
}

void Complex::add(Complex &c0)
{
    Real+=c0.Real;
    imaginary+=c0.imaginary;
}
int main()
{
    Complex c1(3,5);
    Complex c2=4.5;
    c1.show();
    c2.show();
    c1.add(c2);
    cout<<"additon:"<<endl;
    c1.show();
    return 0;
}

技术分享图片

 

 

 

 

 

在做第一个程序的时候 构造函数没有设默认形参 导致在使用的时候一直不知道怎么改错  使用时应该直接赋值  一开始自己看书真的是模模糊糊  在写了代码自己慢慢找错误 该错误 对这单元的内容才有了比较清晰的概念 可以慢慢理解 在第一个实验理解的基础上 第二个写下来就比较顺利了

C++作业3

原文:https://www.cnblogs.com/rohahaablog/p/8747287.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!