南京信息工程大学实验报告(三)

四、实验结论
4-11

# include <iostream>
using namespace std;

class Square{
public:
	Square(float a,float b);
	float area();
private:
	float length,width;
};

Square::Square(float a,float b){
	length=a;
	width=b;
}

float Square::area(){
    return length*width;
}

int main(){
	float length,width;
	
	cout<<"Enter the length:";
	cin>>length;
	cout<<"Enter the width:";
	cin>>width;
	
    Square Square(length,width);
	cout<<"The area is:"<<Square.area()<<endl;
	return 0;
}

4-20

#include<iostream>
using namespace std;
class Complex
{
public:
 Complex(double real,double imaginary);
 Complex(double real);
 void add(Complex &c);
 void show();
 
private:
  double r0;
  double i0;
};
Complex::Complex(double real,double imaginary)
{
 r0=real;
 i0=imaginary;
}
Complex::Complex(double real)
{
  r0=real;
  i0=0;
}
void Complex::add(Complex &c0)
{
 r0+=c0.r0;
 i0+=c0.i0;
}
void Complex::show()
{
 cout<<r0<<"+"<<i0<<'i'<<endl;
}
 
int main()
{
 Complex c1(3,5);
 Complex c2=4.5;
 c1.add(c2);
 c1.show();
 return 0;
}

1.类—构造函数(默认值)—参数—成员函数,其中调用实现都很细碎,马虎就容易漏掉细节,导致bug。
2.复制构造函数和析构函数还不是很懂得使用。默认的复制构造只是浅复制,会带来数据安全方面的隐患。浅复制和深复制还要在查一下。
3.第一个程序加复制构造反而不会了。

五、实验总结和体会
1.公共里面的参数要赋值给私有里面的,如果主函数要用到里面的函数,要调用,还有“类名.”总是会忘记。
2.4-11一开始有问题,是因为忘记把输入的两个变量值放进函数中了,都不把要调用的函数写进main函数,直接输出,当然就bug了。
3. 4-20百度了一下,有个网友在输出的时候的条件是(i>0 ? '+':'-')但是感觉这样不对。
4.其实自己还是挺混乱的,并没有搞清楚每一块程序的作用,才会导致自己编写的时候问题百出。

posted @ 2018-04-08 22:42  花逢君  阅读(166)  评论(10编辑  收藏  举报