1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
using std::cout;
using std::endl;
// class
class Cube {
float width;
float height;
float length;
public:
Cube(float, float, float); // declaration
Cube(const Cube&);
Cube(Cube&&);
float volume() { return (width * height * length); }
};
// definition
Cube::Cube(float w, float h, float l)
: width {w},
height {h},
length {l}
{
cout << "Custom ctor\n";
}
Cube::Cube(const Cube& rhs)
: width {rhs.width},
height {rhs.height},
length {rhs.length}
{
cout << "Copy ctor\n";
}
Cube::Cube(Cube&& rhs)
: width {rhs.width},
height {rhs.height},
length {rhs.length}
{
rhs.width = 0;
rhs.height = 0;
rhs.length = 0;
cout << "Move ctor\n";
}
int main()
{ // option 1
Cube c1(5, 4, 3.5); // Cube c1{ 5, 4, 3.5 };
cout << "Cube option 1 volume : " << c1.volume() << endl;
// option 2
Cube c2 = Cube(5, 4, 3.5); // Cube c2 = Cube{ 5, 4, 3.5 };
cout << "Cube option 2 volume : " << c2.volume() << endl;
// option 3
Cube c3(Cube(5, 4, 3.5)); // auto c3{ Cube{ 5, 4, 3.5 } };
cout << "Cube option 3 volume : " << c3.volume() << endl;
// option 4
Cube c4 = std::move( Cube(5, 4, 3.5) );
cout << "Cube option 4 volume : " << c4.volume() << endl;
}
| |