实验1 类和对象
实验任务1
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <array> 5 // 函数模板 6 // 对满足特定条件的序列类型T对象,使用范围for输出 7 template<typename T> 8 void output1(const T &obj) { 9 for(auto i: obj) 10 std::cout << i << ", "; 11 std::cout << "\b\b \n"; 12 } 13 // 函数模板 14 // 对满足特定条件的序列类型T对象,使用迭代器输出 15 template<typename T> 16 void output2(const T &obj) { 17 for(auto p = obj.begin(); p != obj.end(); ++p) 18 std::cout << *p << ", "; 19 std::cout << "\b\b \n"; 20 } 21 // array模板类基础用法 22 void test_array() { 23 using namespace std; 24 array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化 25 cout << "x1.size() = " << x1.size() << endl; // 输出元素个数 26 x1.fill(42); // 把x1的所有元素都用42填充 27 x1.at(0) = 999; // 把下标为0的元素值修改为999 28 x1[4] = -999; // 把下表为4的元素值修改为-999 29 cout << "x1: "; 30 output1(x1); // 调用模板函数output1输出x1 31 cout << "x1: "; 32 output2(x1); // 调用模板函数output1输出x1 33 array<int, 5> x2{x1}; 34 cout << boolalpha << (x1 == x2) << endl; 35 x2.fill(22); 36 cout << "x2: "; 37 output1(x2)swap(x1, x2); // 交换array对象x1, x2 38 cout << "x1: "; 39 output1(x1); 40 cout << "x2: "; 41 output1(x2); 42 } 43 // vector模板类基础用法 44 void test_vector() { 45 using namespace std; 46 vector<int> v1; 47 cout << v1.size() << endl; // 输出目前元素个数 48 cout << v1.max_size() << endl; // 输出元素个数之最大可能个数 49 v1.push_back(55); // 在v1末尾插入元素 50 cout << "v1: "; 51 output1(v1); 52 vector<int> v2 {1, 0, 5, 2}; 53 v2.pop_back(); // 从v2末尾弹出一个元素 54 v2.erase(v2.begin()); // 删除v2.begin()位置的数据项 55 v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入 56 v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入 57 cout << v2.size() << endl; 58 cout << "v2: "; 59 output2(v2); 60 vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是 61 42 62 cout << "v3: "; 63 output1(v3); 64 vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的 65 [v3.begin(), v3.end()-2)区间作为元素值 66 cout << "v4: "; 67 output1(v4); 68 } 69 // string类基础用法 70 void test_string() { 71 using namespace std; 72 string s1{"oop"}; 73 cout << s1.size() << endl; 74 for(auto &i: s1) 75 i -= 32; 76 s1 += "2023"; 77 s1.append(", hello"); 78 cout << s1 << endl; 79 } 80 int main() { 81 using namespace std; 82 cout << "===========测试1: array模板类基础用法===========" << endl;test_array(); 83 cout << "\n===========测试2: vector模板类基础用法===========" << endl; 84 test_vector(); 85 cout << "\n===========测试3: string类基础用法===========" << endl; 86 test_string(); 87 }

实验任务2
#include <iostream> 2 #include <complex> 3 4 void test_std_complex() 5 { 6 using namespace std; 7 complex<double> c1{3, 4}, c2{4.5}; 8 const complex<double> c3{c2}; 9 cout << "c1 = " << c1 << endl; 10 cout << "c2 = " << c2 << endl; 11 cout << "c3 = " << c3 << endl; 12 cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() 13 << endl; 14 cout << "c1 + c2 = " << c1 + c2 << endl; 15 cout << "c1 - c2 = " << c1 - c2 << endl; 16 cout << "abs(c1) = " << abs(c1) << endl; 17 18 cout << boolalpha; 19 cout << "c1 == c2: " << (c1 == c2) << endl; 20 cout << "c3 == c2: " << (c3 == c2) << endl; 21 complex<double> c4 = 2; 22 cout << "c4 = " << c4 << endl; 23 c4 += c1; 24 cout << "c4 = " << c4 << endl; 25 } 26 int main() 27 { 28 test_std_complex(); 29 }

实验任务3
#include <iostream> 2 #include <string> 3 using namespace std; 4 class T 5 { 6 public: 7 T(int x = 0, int y = 0); 8 T(const T &t); 9 T(T &&t); 10 ~T(); 11 void set_m1(int x); 12 int get_m1() const; 13 int get_m2() const; 14 void display() const; 15 friend void func(); 16 private: 17 int m1, m2; 18 public: 19 static void disply_count(); 20 public: 21 static const string doc; 22 static const int max_count; 23 private: 24 static int count; 25 }; 26 const string T::doc{"a simple class"}; 27 const int T::max_count = 99; 28 int T::count = 0; 29 T::T(int x, int y): m1{x}, m2{y} 30 { 31 ++count; 32 cout << "constructor called.\n"; 33 } 34 T::T(const T &t): m1{t.m1}, m2{t.m2} 35 { 36 ++count; 37 cout << "copy constructor called.\n"; 38 } 39 T::T(T &&t): m1{t.m1}, m2{t.m2} 40 { 41 ++count; 42 cout << "move constructor called.\n"; 43 } 44 T::~T() 45 { 46 --count; 47 cout << "destructor called.\n"; 48 } 49 void T::set_m1(int x) 50 { 51 m1 = x; 52 } 53 int T::get_m1() const 54 { 55 return m1; 56 } 57 int T::get_m2() const 58 { 59 return m2; 60 } 61 void T::display() const 62 { 63 cout << m1 << ", " << m2 << endl; 64 } 65 void T::disply_count() 66 { 67 cout << "T objects: " << count << endl; 68 } 69 void func() 70 { 71 T t1; 72 t1.set_m1(55); 73 t1.m2 = 77; 74 t1.display(); 75 } 76 void test() { 77 cout << "T class info: " << T::doc << endl; 78 cout << "T objects max_count: " << T::max_count << endl; 79 T::disply_count(); 80 T t1; 81 t1.display(); 82 t1.set_m1(42); 83 T t2{t1}; 84 t2.display(); 85 T t3{std::move(t1)}; 86 t3.display(); 87 t1.display(); 88 T::disply_count(); 89 } 90 int main() 91 { 92 cout << "============测试类T============" << endl; 93 test(); 94 cout << endl; 95 cout << "============测试友元函数func()============" << endl; 96 func(); 97 }

实验任务4
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Rect {
private:
static int size;
public:
static const string doc;
double length;
double width;
Rect() : length(2.0), width(1.0) {
size++;
}
Rect(double l, double w) : length(l), width(w) {
size++;
}
Rect(const Rect& other) : length(other.length), width(other.width) {
size++;
}
~Rect() {
size--;
}
static int size_info() {
return size;
}
double len() {
return length;
}
double wide() {
return width;
}
double area() {
return length * width;
}
double circumference() {
return 2 * (length + width);
}
void resize(double times) {
length *= times;
width *= times;
}
void resize(double l_times, double w_times) {
length *= l_times;
width *= w_times;
}
};
const string Rect::doc = "a simple Rect class.";
int Rect::size = 0;
// 普通函数:输出矩形信息
void output(const Rect& r) {
cout << "矩形信息: " << endl;
cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位
cout << "矩形长: " << r.length << endl;
cout << "矩形宽: " << r.width << endl;
cout << "矩形面积: " << r.length * r.width << endl;
cout << "矩形周长: " << 2 * (r.length + r.width) << endl;
}
void test() {
cout << "矩形类信息: " << Rect::doc << endl;
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
Rect r1;
output(r1);
Rect r2(4, 3);
output(r2);
Rect r3(r2);
r3.resize(2);
output(r3);
r3.resize(5, 2);
output(r3);
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
}
// 主函数
int main() {
test();
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
return 0;
}

实验任务5
#include<iostream>
#include<math.h>
using namespace std;
class Complex {
public:
Complex() :real{ 0 }, imag{ 0 } {}
~Complex() {}
Complex(double real) : real{ real }, imag{ 0 } {}
Complex(double real, double imag) : real{ real }, imag{ imag } {}
Complex(const Complex& c0) :real{ c0.real }, imag{ c0.imag } {}
double get_real()const { return real; }
double get_imag()const { return imag; }
void show() const;
void add(Complex c0);
friend Complex add(Complex c1, Complex c2);
friend bool is_equal(Complex x, Complex rx);
friend double abs(Complex c0);
private:
double real, imag;
};
Complex add(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
bool is_equal(Complex x, Complex rx) {
if (x.real == rx.real && x.imag == rx.imag)
return true;
else
return false;
}
double abs(Complex c) {
double result = (double)sqrt((double)pow(c.real, 2) + (double)pow(c.imag, 2));
return result;
}
void Complex::show() const {
if (real != 0) {
cout << real;
}
if (imag != 0 && imag > 0) {
cout << " + " << imag << 'i';
}
else if (imag != 0 && imag < 0) {
cout << " " << imag << 'i';
}
}
void Complex::add(Complex c0) {
real += c0.real;
imag += c0.imag;
}
void test() {
using namespace std;
Complex c1(3, -4);
const Complex c2(4.5);
Complex c3(c1);
cout << "c1 = ";
c1.show();
cout << endl;
cout << "c2 = ";
c2.show();
cout << endl;
cout << "c2.imag = " << c2.get_imag() << endl;
cout << "c3 = ";
c3.show();
cout << endl;
cout << "abs(c1) = ";
cout << abs(c1) << endl;
cout << boolalpha;
cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
Complex c4;
c4 = add(c1, c2);
cout << "c4 = c1 + c2 = ";
c4.show();
cout << endl;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}
int main() {
test();
}

浙公网安备 33010602011771号