实验任务(1)
task1_1
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 int main() { 5 using namespace std; 6 string s1; 7 string s2{ "c plus plus" }; 8 string s3{ s2 }; 9 string s4 = s2; 10 s1 = "oop"; 11 vector<string>v1; 12 v1.push_back(s1); 13 v1.push_back(s2 + "1"); 14 v1.push_back(s3 + "2"); 15 v1.push_back(s4 + "3"); 16 cout << "output1:" << endl; 17 for (auto& item : v1) 18 cout << item << endl; 19 cout << "output2:"<<endl; 20 for (auto p = v1.begin(); p != v1.end(); ++p) 21 cout << *p << endl; 22 cout << "output3:" << endl; 23 for (auto i = 0; i < v1.size(); ++i) 24 cout << v1[i] << endl; 25 vector<string>v2{ v1.rbegin(),v1.rend() }; 26 cout << "v2:" << endl; 27 for (auto& item : v2) 28 cout << item << endl; 29 }
task1_2
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<cmath> 5 #include<cstdlib> 6 #include<time.h> 7 8 template<typename T> 9 void output(const T& obj) 10 { 11 for (auto item : obj) 12 std::cout <<item<< " " ; 13 std::cout << std::endl; 14 } 15 16 17 int main() { 18 19 using namespace std; 20 21 vector<int>v1{1, 9, 8, 4}; 22 v1.insert(v1.begin(),2022); 23 v1.insert(v1.end(),2023 ); 24 //调用output函数 25 cout << "v1:"; 26 output(v1); 27 28 v1.pop_back(); 29 v1.erase(v1.begin()); 30 cout << "v1:"; 31 output(v1); 32 33 vector<string> v2{ "《1984》", "《动物农场》", "《美丽新世界》" }; 34 cout << "v2:"; 35 output(v2); 36 }
实验任务(2)
1 #include<iostream> 2 3 using std::cout; 4 using std::endl; 5 //point的定义 6 7 class Point { 8 public: 9 Point(int x0 = 0, int y0 = 0); 10 Point(const Point& p); 11 ~Point() = default; 12 int get_x() const { return x; } 13 int get_y() const { return y; } 14 void show() const; 15 private: 16 int x, y; 17 }; 18 19 Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } 20 { cout << "constructor called." << endl; } 21 22 Point::Point(const Point& p) : x{ p.x }, y{ p.y } 23 { cout << "copy constructor called." << endl; } 24 25 void Point::show() const 26 { cout << "(" << x << ", " << y << ")" << endl; } 27 28 int main() { 29 Point p1(1, 2); 30 p1.show(); 31 32 Point p2 = p1; 33 p2.show(); 34 35 Point p3{ p2 }; 36 p3.show(); 37 cout << p3.get_x() << endl; 38 }
实验任务(3)
1 #include<iostream> 2 #include<iomanip> 3 4 using std::cout; 5 using std::endl; 6 7 class Clock { 8 public: 9 Clock(int h = 0, int m = 0, int s = 0); 10 Clock(const Clock& t); 11 ~Clock() = default; 12 13 void set_time(int h, int m = 0, int s = 0); 14 15 void show_time()const; 16 private: 17 int hour, minute, second; 18 }; 19 20 Clock::Clock(int h, int m, int s) :hour{ h }, minute{ m }, second{ s } 21 { 22 cout << "constrctor called" << endl; 23 } 24 25 Clock::Clock(const Clock& t) :hour{ t.hour }, minute{ t.minute }, second{ t.second } 26 { 27 cout << "copy constructor called" << endl; 28 } 29 30 void Clock::set_time(int h, int m, int s) 31 { 32 hour = h; 33 minute = m; 34 second = s; 35 } 36 37 void Clock::show_time()const 38 { 39 using std::setw; 40 using std::setfill; 41 42 cout << setfill('0') 43 << setw(2) << hour << ":" 44 << setw(2) << minute << ":" 45 << setw(2) << second << endl; 46 } 47 48 Clock reset() { 49 return Clock(0, 0, 0); 50 } 51 52 53 int main() { 54 Clock c1(12, 2, 9); 55 c1.show_time(); 56 57 c1 = reset(); 58 c1.show_time(); 59 60 Clock c2(c1); 61 c2.set_time(7,7,7); 62 c2.show_time(); 63 }
实验任务(4)
1 #include<iostream> 2 3 class x{ 4 public: 5 x(); 6 ~x(); 7 x(int m); 8 x(const x& obj); 9 x(x&& obj)noexcept; 10 void show()const; 11 private: 12 int data; 13 }; 14 15 x::x() :data{ 42 } 16 { 17 std::cout << "default constructor called.\n"; 18 } 19 x::~x() 20 { 21 std::cout << "destructor called.\n"; 22 } 23 x::x(int m) :data{m} 24 { 25 std::cout << "constructor called.\n"; 26 } 27 x::x(const x& obj) :data{ obj.data } 28 { 29 std::cout << "copy constructor called.\n"; 30 } 31 x::x(x&& obj)noexcept :data{ obj.data } 32 { 33 std::cout << "move constructor called.\n"; 34 } 35 void x::show()const 36 { 37 std::cout << data << std::endl; 38 } 39 40 int main() { 41 x x1; 42 x1.show(); 43 44 x x2{ 4096 }; 45 x2.show(); 46 47 x x3{ x1 }; 48 x3.show(); 49 50 x x4{ std::move(x2) }; 51 x4.show(); 52 }
构造函数被调用:line44,line47;
析构函数被调用:line50;
实验任务(5)
1 #include<iostream> 2 #include<iomanip> 3 4 using std::cout; 5 using std::endl; 6 7 class Rectangle { 8 public: 9 Rectangle(); //默认函数 10 Rectangle(double l, double w); //带两个参数的函数 11 Rectangle(const Rectangle& rect); //复制构造函数 12 ~Rectangle() = default; 13 14 void resize(double times); //普通重载函数 15 void resize(double l_times, double w_times); //普通重载函数 16 17 double len() const; 18 double wide() const; 19 double area() const; 20 double circumference() const; 21 22 private: 23 double length, width; 24 }; 25 26 27 Rectangle::Rectangle() { 28 length = 2.0; 29 width = 1.0; 30 } //默认矩形 31 32 Rectangle::Rectangle(double l, double w) { 33 length = l; 34 width = w; 35 } //两个参数定义矩形 36 37 Rectangle::Rectangle(const Rectangle& rect) :length{ rect.length }, width{ rect.width }{} //复制构造函数 38 39 void Rectangle::resize(double times) { //同时resize times倍 40 length *= times; 41 width *= times; 42 } 43 44 void Rectangle::resize(double l_times, double w_times) { //分别resize 45 length *= l_times; 46 width *= w_times; 47 } 48 //普通函数的定义 49 double Rectangle::len() const { return length; } 50 double Rectangle::wide() const { return width; } 51 double Rectangle::area() const { return length * width; } 52 double Rectangle::circumference() const { return 2 * (length + width); } 53 54 void output(const Rectangle& rect) 55 { 56 using namespace std; 57 cout << "矩阵信息:\n"; 58 cout << fixed << setprecision(2); 59 cout << "长: " << rect.len() << endl; 60 cout << "宽: " << rect.wide() << endl; 61 cout << "面积: " <<rect.area() << endl; 62 cout << "周长: " <<rect.circumference() << endl; 63 } 64 65 66 int main() { 67 Rectangle rect1; // 默认构造函数被调用 68 output(rect1); 69 70 Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用 71 output(rect2); 72 73 Rectangle rect3(rect1); // 复制构造函数被调用 74 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍 75 output(rect3); 76 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍 77 output(rect3); 78 }
标签:const,cout,对象,double,int,实验,include,Rectangle From: https://www.cnblogs.com/zx1229/p/16757709.html