1. declaration and definition

Any variable can be defined for only once. On the contrary, declaration is different. Extern declaration is not definition and does not allocate memory for the variable.

    2.   cin and getline

getline ignore space, so in case I wanna input something like space inside my string, getline is for sure a better choice.

    3.   something about vector

Vector is a useful class template, in order to use it, we need to include certain headfiles. Actually, to ensure we can have a continuous piece of memory, we tend to create an empty vector first, can increase its element dynamically.

    4.   about constructor

#include <iostream>
namespace std;
class Complex{
double real;
double imag;
public:
//无参构造函数
void)
   9:     {
  10:         real = 0.0;
  11:         imag = 0.0;
<<endl;
  13:     }
//一般构造函数
double i)
  16:     {
  17:         real = r;
  18:         imag = i;
<<endl;
  20:     }
//复制构造函数
const Complex &s)
  23:     {
  24:         real = s.real;
  25:         imag = s.imag;
<<endl;
  27:     }
//类型转换构造函数
double r)
  30:     {
  31:         real = r;
  32:         imag = 0.0;
<<endl;
  34:     }
//等号运算符重载
const Complex &s)
  37:     {
<<endl;
this == &s)
  40:         {
this;
  42:         }
this->real = s.real;
this->imag = s.imag;
this;
  46:     }
  47: };
int main()
  49: {
//constructor with no parameter
//normal constructor
//normal constructor
//copy constructor
//a is already constructed, = operator overload
//type transform constructor 
//copy constructor
  57:  
  58: }

this little piece of code outputs:

Constructor with no parameter gets called!
Constructor with no parameter gets called!
Normal constructor gets called!
Normal constructor gets called!
Copy constructor gets called!
Equality operator overload!
Type transform constructor gets called!
Equality operator overload!
Copy constructor gets called!
请按任意键继续. . .

 

Complex test1(const Complex& c)
{
return c;
}

Complex test2(const Complex c)
{
return c;
}

Complex test3()
{
static Complex c(1.0,5.0);
return c;
}

Complex& test4()
{
static Complex c(1.0,5.0);
return c;
}
int main()
{
Complex a,b; //constructor with no parameter
//Complex c(1.2,3.4); //normal constructor
//Complex d = Complex(1.2,3.4); //normal constructor
//Complex e = c; //copy constructor
//a = c; //a is already constructed, = operator overload
//b = 5.2; //type transform constructor
//Complex f(c); //copy constructor
//// 下面函数执行过程中各会调用几次构造函数,调用的是什么构造函数?

test1(a); //copy copy
test2(a); //copy

b = test3(); //normal copy = overload
b = test4(); //normal overload

test2(1.2); //type transform, copy
// 下面这条语句会出错吗?
test1(1.2);
test1( Complex(1.2 ));



 

相关文章:

  • 2021-07-01
  • 2019-05-26
  • 2021-11-07
  • 2022-01-02
  • 2021-09-28
  • 2022-12-23
猜你喜欢
  • 2022-02-12
  • 2022-12-23
  • 2021-09-01
  • 2021-12-09
  • 2021-07-24
  • 2022-12-23
相关资源
相似解决方案