实验1 类和对象(1)

task1:

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 int main()
 5 {
 6 using namespace std;
 7 string s1; // 创建一个string对象
 8 string s2{"c plus plus"}; // 创建一个string对象,并初始化
 9 string s3{s2}; // 创建一个string对象,并用s2对其进行初始化
10 string s4 = s2; // 创建一个string对象,并用s2对其进行初始化
11 s1 = "oop";
12 1
13 2
14 3
15 4
16 5
17 6
18 7
19 8
20 9
21 10
22 11
23 12
24 13
25 14
26 测试结果:
27 说明: 
28 1. xx.size() 是容器类对象的方法,返回容器数据项个数。
29 2. xx.begin() , xx.end() , xx.rbegin() , xx.rend() 是序列类容器(container)对象的迭代器
30 (iterator)接口。某种程度上,是比指针更高层的抽象。
31 xx.begin() 指向容器内的第一个数据项, xx.end() 指向容器内最后一个数据项后面的位置;
32 xx.rbegin() 指向容器内的最后一个数据项, xx.rend() 指向容器内第一个数据项前面的位置;
33 迭代器指示的是位置,通过迭代器访问数据项要通过星号运算,即代码中的line30 *itetator 这样的方
34 式。
35 vector<string> v1; // 创建一个vector对象
36 v1.push_back(s1); // 向v1末尾添加数据项s1
37 v1.push_back(s2 + "1");
38 v1.push_back(s3 + "2");
39 v1.push_back(s4 + "3");
40 // 输出方式1:使用自动类型推导、范围for
41 cout << "output1: " << endl;
42 for(auto item: v1)
43 cout << item << endl;
44 // 输出方式2:使用自动类型推导、迭代器
45 cout << "ouput2: ";
46 for(auto p = v1.begin(); p != v1.end(); ++p)
47 cout << *p << endl;
48 // 输出方式3:使用自动类型推导、索引
49 cout << "output3: " << endl;
50 for(auto i = 0; i < v1.size(); ++i)
51 cout << v1[i] << endl;
52 vector<string> v2{v1.rbegin(), v1.rend()}; // 使用vector对象v1极其迭代器,
53 构造对象v2
54 cout << "v2: " << endl;
55 for(auto item: v2)
56 cout << item << endl;
57 }
 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <time.h>
 7 // 定义模板函数,用于输出vector容器对象元素的值
 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 int main()
16 {
17 using namespace std;
18 vector<int> v1{1, 9, 8, 4};
19 v1.insert(v1.begin(), 2022); // 在v1.begin()之前的位置插入
20 v1.insert(v1.end(), 2023); // 在v1.end()之前的位置插入
21 cout << "v1: ";
22 output(v1);
23 v1.pop_back(); // 从v1尾部删除数据项
24 v1.erase(v1.begin()); // 删除v1.begin()位置的数据项
25 cout << "v1: ";
26 output(v1);
27 vector<string> v2{"《1984》", "《动物农场》", "《美丽新世界》"};
28 cout << "v2: ";
29 output(v2);
30 }

 

测试结果:

 

 

 

 

 

 

task2:

代码:

 1 #include <iostream>
 2 using std::cout;
 3 using std::endl;
 4 // 定义Point类
 5 class Point {
 6 public:
 7 Point(int x0 = 0, int y0 = 0);
 8 Point(const Point&p );
 9 ~Point() = default;
10 int get_x() const { return x; } // 内联成员函数
11 int get_y() const { return y; } // 内联成员函数
12 void show() const;
13 private:
14 int x, y;
15 };
16 // Point类的实现
17 // 构造函数(带有默认形参值)
18 Point::Point(int x0, int y0): x{x0}, y{y0} {
19 cout << "constructor called." << endl;
20 }
21 // 复制构造函数
22 // 参数必须是自身对象的引用类型
23 Point::Point(const Point& p): x{p.x}, y{p.y} {
24 cout << "copy constructor called." << endl;
25 }

 

测试结果:

 

 

 

task3:

代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 using std::cout;
 4 using std::endl;
 5 // 定义时钟类Clock
 6 class Clock {
 7 public:
 8 Clock(int h = 0, int m = 0, int s = 0);
 9 Clock(const Clock& t);
10 ~Clock() = default;
11 void set_time(int h, int m = 0, int s = 0);
12 void show_time() const;
13 private:
14 int hour, minute, second;
15 };
16 // 类Clock实现
17 Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} {
18 cout << "constructor called" << endl;
19 }
20 Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute},
21 second{t.second} {
22 cout << "copy constructor called" << endl;
23 }
24 void Clock::set_time(int h, int m, int s) {
25 hour = h;
26 minute = m;
27 second = s;
28 }
29 void Clock::show_time() const {
30 using std::setw;
31 using std::setfill;
32 cout << setfill('0') << setw(2) << hour << ":"
33 << setw(2) << minute << ":"
34 << setw(2) << second << endl;
35 }
36 // 普通函数定义
37 Clock reset() {
38 return Clock(0, 0, 0); // 构造函数被调用
39 }
40 int main()
41 {
42 Clock c1(12, 0, 5); // 构造函数被调用
43 c1.show_time();
44 c1 = reset(); // 理论上:复制构造函数被调用
45 c1.show_time();
46 Clock c2(c1); // 复制构造函数被调用
47 c2.set_time(6);
48 c2.show_time();
49 }

 

测试结果:

 

 

 

task4:

代码:

 1 class X{
 2 public:
 3 X(); // 默认构造函数
 4 ~X(); // 析构函数
 5 X(int m); // 构造函数
 6 X(const X& obj); // 复制构造函数
 7 X(X&& obj) noexcept; // 移动构造函数
 8 void show() const; // 显示数据
 9 private:
10 int data;
11 };
12 X::X(): data{42} {
13 std::cout << "default constructor called.\n";
14 }
15 X::~X() {
16 std::cout << "destructor called.\n";
17 }
18 X::X(int m): data{m} {
19 std::cout << "constructor called.\n";
20 }
21 X::X(const X& obj): data{obj.data} {
22 std::cout << "copy constructor called.\n";
23 }
24 X::X(X&& obj) noexcept: data{obj.data} {
25 std::cout << "move constructor called.\n";
26 }
27 void X::show() const {
28 std::cout << data << std::endl;
29 }
30 int main() {
31 X x1; //默认构造函数被编译器自动调用
32 x1.show();
33 X x2{2049};
34 x2.show(); // 构造函数被编译器自动调用
35 X x3{x1}; // 复制构造函数被编译器自动调用
36 x3.show();
37 X x4{ std::move(x2) }; // 移动构造函数被编译器调用
38 x4.show();
39 }

 

测试结果:

 

 

 

task5:

代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 // 矩形类Rectangle的定义和实现
 4 class Rectangle
 5 {
 6     public:
 7         Rectangle(int x=2.0,int y=1.0)
 8         {
 9             length=x;
10             width=y;
11         }
12         Rectangle(const Rectangle &a){
13             length=a.len();
14             width=a.wide();
15         }
16         ~Rectangle(){}
17         float len() const{
18             return length;
19         }
20         float wide() const{
21             return width;
22         }
23         float area() const{
24             return length*width;
25         }
26         float circumference() const
27         {
28             return 2*(length+width);
29         }
30         void resize(float times){
31             width*=times;
32             length*=times;
33         }
34         void resize(float l_times,float w_times)
35         {
36             width*=w_times;
37             length*=l_times;
38         }
39     private:
40         float length,width;
41  } ;
42 // 补足代码
43 // ×××
44 // 普通函数, 用于输出矩形信息
45 void output(const Rectangle &rect) {
46 using namespace std;
47 cout << "矩形信息: \n";
48 cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出、小数部分保留两位
49 printf("长:\t%.2f\n",rect.len());// 补足代码:分行输出矩形长、宽、面积、周长
50 printf("宽:\t%.2f\n",rect.wide());
51 printf("面积:\t%.2f\n",rect.area());
52 printf("周长:\t%.2f\n",rect.circumference()); 
53 // ×××
54 }
55 // 主函数,测试Rectangle类
56 int main() {
57 Rectangle rect1; // 默认构造函数被调用
58 output(rect1);
59 Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
60 output(rect2);
61 Rectangle rect3(rect1); // 复制构造函数被调用
62 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
63 output(rect3);
64 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
65 output(rect3);
66 }

 

测试结果:

 

posted @ 2022-10-29 17:45  【VC】熬夜不吃饭的兔子  阅读(52)  评论(0)    收藏  举报