obamax

实验三

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;
}

运行结果

这里写图片描述


实验感想:

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

分类:

技术点:

相关文章:

  • 2022-01-01
  • 2021-12-03
  • 2022-02-04
  • 2021-05-24
猜你喜欢
相关资源
相似解决方案