码迷,mamicode.com
首页 > 编程语言 > 详细

C++作业3

时间:2018-04-09 00:26:45      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:img   .com   cout   int   space   using   c++   cin   使用   

//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

标签:img   .com   cout   int   space   using   c++   cin   使用   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!