task1
1 #include <iostream> 2 #include "point.hpp" 3 #include <vector> 4 5 using std::vector; 6 using std::cin; 7 8 void output(const vector<Point> &v) { 9 for(auto &t: v) 10 t.show(); 11 } 12 void test() { 13 int n; 14 cin >> n; 15 16 vector<Point> x(n); 17 cout << "x对象中所有点坐标信息: " << endl; 18 output(x); 19 20 vector<Point> y(x); // 基于vector<Point>对象x构建对象y 21 cout << "\nx对象中所有点坐标信息: " << endl; 22 output(y); 23 24 cout << "\n更新x对象: " << endl; 25 x.at(0).move(30, 50); // 更新对象x内索引为0的点对象坐标 26 x.push_back(Point(2, 2)); // 向x对象末尾添加一个点对象 27 28 cout << "\nx对象中所有点坐标信息: " << endl; 29 output(x); 30 cout << "\ny对象中所有点坐标信息: " << endl; 31 output(y); 32 } 33 34 int main() { 35 test(); 36 }task1.cpp
1 #pragma once 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 6 class Point { 7 public: 8 Point(int x0 = 0, int y0 = 0); 9 ~Point() = default; 10 11 int get_x() const; 12 int get_y() const; 13 void show() const; 14 void move(int new_x, int new_y); 15 16 private: 17 int x, y; 18 }; 19 20 Point::Point(int x0,int y0):x{x0},y{y0} { 21 } 22 23 int Point::get_x() const { 24 return x; 25 } 26 27 int Point::get_y() const { 28 return y; 29 } 30 31 void Point::show() const { 32 cout << "(" << x << "," << y << ")" <<endl; 33 } 34 35 void Point::move(int new_x, int new_y) { 36 x = new_x; 37 y = new_y; 38 }point.cpp
问题1:对x对象进行更新时,基于 vector<Point> 对象x创建的对象y是否发生变化? 答:没有。 问题2:标准库模板类vector在复制一个动态数组对象时,实现的是深复制还是浅复制? 答:深复制。 task2
1 #include "vectorPoint.hpp" 2 #include <iostream> 3 4 void output(const vectorPoint &v) { 5 for(auto i = 0; i < v.get_size(); ++i) 6 v.at(i).show(); 7 } 8 9 void test() { 10 using namespace std; 11 12 int n; 13 cout << "输入vectorPoint对象中元素个数: "; 14 cin >> n; 15 16 vectorPoint x(n); 17 cout << "x对象中所有点坐标信息: " << endl; 18 output(x); 19 20 vectorPoint y(x); 21 cout << "\ny对象中所有点坐标信息: " << endl; 22 output(y); 23 24 cout << "\n更新x对象中点坐标信息......" << endl; 25 x.at(0).move(30, 50); 26 x.at(1).move(-1, -1); 27 28 cout << "x对象中所有点坐标信息: " << endl; 29 output(x); 30 31 cout << "\ny对象中所有点坐标信息: " << endl; 32 output(y); 33 } 34 35 int main() { 36 test(); 37 }task2.cpp
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 Point(int x0 = 0, int y0 = 0); 10 ~Point() = default; 11 12 int get_x() const; 13 int get_y() const; 14 void show() const; 15 void move(int new_x, int new_y); 16 17 private: 18 int x, y; 19 }; 20 21 Point::Point(int x0, int y0): x{x0}, y{y0} { 22 } 23 24 int Point::get_x() const { 25 return x; 26 } 27 28 int Point::get_y() const { 29 return y; 30 } 31 32 void Point::show() const { 33 cout << "(" << x << ", " << y << ")" << endl; 34 } 35 36 void Point::move(int new_x, int new_y) { 37 x = new_x; 38 y = new_y; 39 }point.hpp
1 #pragma once 2 3 #include "point.hpp" 4 #include <cassert> 5 #include <iostream> 6 7 class vectorPoint{ 8 public: 9 vectorPoint(int n); 10 ~vectorPoint(); 11 12 int get_size() const; 13 Point& at(int index); 14 Point& at(int index) const; 15 16 private: 17 int size; 18 Point *ptr; 19 }; 20 21 vectorPoint::vectorPoint(int n) : size{n} { 22 ptr = new Point[n]; 23 } 24 25 vectorPoint::~vectorPoint() { 26 delete[] ptr; 27 } 28 29 int vectorPoint::get_size() const { 30 return size; 31 } 32 33 Point& vectorPoint::at(int index) { 34 assert(index >= 0 && index < size); 35 return ptr[index]; 36 } 37 38 Point& vectorPoint::at(int index) const { 39 assert(index >= 0 && index < size); 40 return ptr[index]; 41 }vectorPoint.hpp
问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化? 答:发生变化。 问题2:编译器为vectorPoint类创建的默认复制构造函数,在复制一个动态数组对象时,实现 的是深复制还是浅复制? 答:浅复制。 问题3:在类vectorPoint内部,手动增加的以下复制构造函数声明和定义,实现的是浅复制还 是深复制? 声明: vectorPoint(const vectorPoint &vp); 实现: vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{vp.ptr} { } 答:浅复制。 task3
1 #include "vectorPoint.hpp" 2 #include <iostream> 3 4 void output(const vectorPoint &v) { 5 for(auto i = 0; i < v.get_size(); ++i) 6 v.at(i).show(); 7 } 8 9 void test() { 10 using namespace std; 11 12 int n; 13 cout << "输入vectorPoint对象中元素个数: "; 14 cin >> n; 15 16 vectorPoint x(n); 17 cout << "x对象中所有点坐标信息: " << endl; 18 output(x); 19 20 vectorPoint y(x); 21 cout << "\ny对象中所有点坐标信息: " << endl; 22 output(y); 23 24 cout << "\n更新x对象中点坐标信息......" << endl; 25 x.at(0).move(30, 50); 26 x.at(1).move(-1, -1); 27 28 cout << "x对象中所有点坐标信息: " << endl; 29 output(x); 30 31 cout << "\ny对象中所有点坐标信息: " << endl; 32 output(y); 33 } 34 35 int main() { 36 test(); 37 }task3.cpp
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 10 Point(int x0 = 0, int y0 = 0); 11 ~Point() = default; 12 13 int get_x() const; 14 int get_y() const; 15 void show() const; 16 void move(int new_x, int new_y); 17 18 private: 19 int x, y; 20 }; 21 22 Point::Point(int x0, int y0): x{x0}, y{y0} { 23 } 24 25 int Point::get_x() const { 26 return x; 27 } 28 29 int Point::get_y() const { 30 return y; 31 } 32 33 void Point::show() const { 34 cout << "(" << x << ", " << y << ")" << endl; 35 } 36 37 void Point::move(int new_x, int new_y) { 38 x = new_x; 39 y = new_y; 40 }point.hpp
1 #pragma once 2 3 #include "point.hpp" 4 #include <cassert> 5 #include <iostream> 6 7 class vectorPoint{ 8 public: 9 vectorPoint(int n); 10 vectorPoint(const vectorPoint &vp); 11 ~vectorPoint(); 12 13 int get_size() const; 14 15 Point& at(int index); 16 Point& at(int index) const; 17 18 private: 19 int size; 20 Point *ptr; 21 }; 22 23 vectorPoint::vectorPoint(int n) : size{n} { 24 ptr = new Point[n]; 25 } 26 27 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new 28 Point[size]} { 29 for(auto i = 0; i < size; ++i) 30 ptr[i] = vp.ptr[i]; 31 } 32 33 vectorPoint::~vectorPoint() { 34 delete[] ptr; 35 } 36 37 int vectorPoint::get_size() const { 38 return size; 39 } 40 41 Point& vectorPoint::at(int index) { 42 assert(index >= 0 && index < size); 43 return ptr[index]; 44 } 45 46 Point& vectorPoint::at(int index) const { 47 assert(index >= 0 && index < size); 48 return ptr[index]; 49 }vectorPoint.hpp
问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化? 答:没有。 问题2:这个vectorPoint 类的实现中,复制构造函数实现的是深复制还是浅复制? 答:深复制。 task4
1 #include <iostream> 2 using namespace std; 3 // 函数声明 4 void swap1(int &rx, int &ry); // 引用作为形参 5 void swap2(int *px, int *py); // 指针作为形参 6 void print(int x, int y); // 普通变量作为形参 7 // 测试代码 8 void test() { 9 int x = 3, y = 4; 10 print(x, y); 11 swap1(x, y); // 函数调用,注意:引用作为形参时,实参形式 12 print(x, y); 13 cout << endl; 14 x = 3, y = 4; 15 print(x, y); 16 swap2(&x, &y); // 函数调用,注意:指针作为形参时,实参形式 17 print(x, y); 18 } 19 int main() { 20 test(); 21 } 22 // 函数定义:交换两个变量(引用变量作为形参) 23 void swap1(int &rx, int &ry) { 24 int t; 25 t = rx; rx = ry; ry = t; 26 } 27 // 函数定义:交换两个变量(指针变量作为形参) 28 void swap2(int *px, int *py) { 29 int t; 30 t = *px; *px = *py; *py = t; 31 } 32 // 函数定义:输出两个变量(普通变量作为形参) 33 void print(int x, int y) { 34 std::cout << "x = " << x << ", y = " << y << "\n"; 35 }task4_1
1 #include <iostream> 2 #include <typeinfo> 3 using namespace std; 4 int main() { 5 int a; 6 int &ra = a; 7 ra = 4; 8 int *pa = &a; 9 *pa = 5; 10 // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址 11 cout << "&a = " << hex << &a << endl; 12 cout << "&ra = " << hex << &ra << endl; 13 cout << "&pa = " << hex << &pa << "\n\n"; 14 // 输出普通变量a, 引用变量ra,指针变量pa的值 15 cout << "a = " << a << endl; 16 cout << "ra = " << a << endl; 17 cout << "pa = " << hex << pa << endl; 18 // 输出指针变量pa指向的变量的值 19 cout << "*pa = " << *pa << "\n\n"; 20 // 输出普通变量a,引用变量ra, 指针变量pa的类型信息 21 cout << "type a: " << typeid(a).name() << endl; 22 cout << "type ra: " << typeid(ra).name() << endl; 23 cout << "type pa: " << typeid(pa).name() << endl; 24 }task4_2
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 template<typename T> 5 void output(const T &x) { 6 for(auto i: x) 7 std::cout << i << ", "; 8 std::cout << "\b\b \n"; 9 } 10 template<typename T> 11 void square1(T &x) { 12 for(auto i: x) // i是普通类型 13 i *= i; 14 } 15 template<typename T> 16 void square2(T &x) { 17 for(auto &i: x) // i是引用类型 18 i *= i; 19 } 20 void test1() { 21 vector<int> x {1, 2, 3, 4, 5}; 22 cout << "动态int型数组对象x内的元素值: "; 23 output(x); 24 cout << "调用函数square1()......" << endl; 25 square1(x); 26 cout << "动态int型数组对象x内的元素值: "; 27 output(x); 28 } 29 void test2() { 30 vector<int> x {1, 2, 3, 4, 5}; 31 cout << "动态int型数组对象x内的元素值: "; 32 output(x); 33 cout << "调用函数square2()......" << endl; 34 square2(x); 35 cout << "动态int型数组对象x内的元素值: "; 36 output(x); 37 } 38 int main() { 39 cout << "测试1: " << endl; 40 test1(); 41 cout << "\n测试2: " << endl; 42 test2(); 43 }task4_3
task5
标签:const,Point,int,void,实验,vectorPoint,size From: https://www.cnblogs.com/Serenity-X/p/17811809.html