实验三

4_11源码:

#include<iostream>
using namespace std;
class CRectangle
{
    public :
        int w,h;
        int Area(){
            return w*h;
        }
        void Init(int w_, int h_){
            w=w_;
            h=h_;
        }    
};
int main()
{
    int w,h;
    CRectangle r;
    cin>>w>>h;;
    r.Init(w,h);
    cout<<r.Area();
    return 0;
}

运行结果:

这里写图片描述

4_20源码:

#include<iostream>
using namespace std;
class Complex
{
    private :
        double real,image;//定义的实部和虚部; 
    public :
        Complex(double r,double i);//构造函数; 
        Complex(int i);//类型转换构造函数;
        void add(Complex c);
        void show();
} ;
Complex::Complex(double r,double i)
{
    real=r;
    image=i;
}
Complex::Complex(int i)
{
    real=i;
    image=0;
}
void Complex::add(Complex c)
{
    real+=c.real;
    image+=c.image;
}
void Complex::show()
{
    cout<<real<<"+"<<image<<"i";
}
int main()
{
    Complex c1(3,5);
    Complex c2=4.5;
    c1.add(c2);
    c1.show();
    return 0;
}

运行结果

这里写图片描述


实验感想:

对构造函数,复制构造函数,析构函数,类型转换构造函数的使用还不是很熟练,还没有很好的理解他们。

版权声明:本文为obamax原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/obamax/p/8761866.html