oop 实验1类和对象基础编程

发布时间 2023-10-19 21:30:22作者: 小程夏天
#include <iostream>
#include <string>
#include <vector>
#include <array>
// 通用函数(此处是模板函数)用于输出容器中的元素,支持范围for(范围for循环,是一种用于遍历容器、数组和其他序列容器的现代C++迭代循环结构。它提供了一种更简洁和易读的方法来遍历容器的元素,而无需手动处理迭代器或索引。)循环 template <typename T> void output1(const T &obj) { for (auto i : obj) std::cout << i << ","; std::cout << "\b \n"; // 删除末尾多余的逗号并换行 } // 输出容器中的元素,使用迭代器遍历 template <typename T> void output2(const T &obj) { for (auto p = obj.begin(); p != obj.end(); ++p) std::cout << *p << ","; std::cout << "\b \n"; // 删除末尾多余的逗号并换行 } void test_array() { using namespace std; array<int, 5> x1; // 创建包含5个int的数组对象 cout << "x1.size()=" << x1.size() << endl; x1.fill(42); // 填充数组元素为42 x1.at(0) = 999; // 修改指定位置第一个的元素 x1[4] = -999; // 修改指定位置第五个的元素 cout << "x1:"; output1(x1); // 第一种方法输出数组元素 cout << "x1:"; output2(x1); // 第二种方法输出数组元素 array<int, 5> x2(x1); // 使用另一个数组初始化新数组(此处不能用{}可能由于版本问题) cout << boolalpha << (x1 == x2) << endl; // 判断两个数组是否相等如果是返回ture x2.fill(22); // 填充x2的元素为22 cout << "x2:"; output1(x2); swap(x1, x2); // 交换两个数组x1,x2 cout << "x1:"; output1(x1); cout << "x2:"; output1(x2); } void test_vector() { using namespace std; vector<int> v1; cout << v1.size() << endl; cout << v1.max_size() << endl; v1.push_back(55); cout << "v1:"; output1(v1); // 输出v1全部元素 vector<int> v2{1, 0, 5, 2}; v2.pop_back(); // 用函数pop_back删除最后一个元素 v2.erase(v2.begin()); // 删除第一个元素 v2.insert(v2.begin(), 999); // 在开头插入元素 v2.insert(v2.end(), -999); // 在末尾插入元素 cout << v2.size() << endl; cout << "v2:"; output2(v2); vector<int> v3(5, 42); // 创建一个包含5个42的元素 cout << "v3"; output1(v3); vector<int> v4(v3.begin(), v3.end()-2); // 从v3的开头复制3个元素给v4 cout << "v4:"; output1(v4); } void test_string() { using namespace std; string s1{"opp"}; cout << s1.size() << endl; for (auto &i : s1) i -= 32; // 将字符转换为大写 s1 += "2023"; // 字符串拼接采用直接加的形式 s1.append(",hello"); // 追加字符串采用s1.append append函数 cout << s1 << endl; } int main() { using namespace std; cout << "=======测试1:array模板类基础用法======\n"; test_array(); cout << "\n=======测试2:vector====\n"; test_vector(); cout << "\n=======测试3:string====\n"; test_string(); return 0; }

//任务2
#include <iostream>
#include <complex>
void test_std_complex() {
    using namespace std;
    // 创建复数对象用complex<double>创建 
    complex<double> c1{3, 4};
    complex<double> c2{4.5};
    
    // 创建一个常量复数对象
    const complex<double> c3{c2};
    
    // 输出复数对象c1,c2,c3的值
    cout << "c1=" << c1 << endl;
    cout << "c2=" << c2 << endl;
    cout << "c3=" << c3 << endl;
    
    // 输出复数对象的实部和虚部
    cout << "c3.real=" << c3.real() << ", c3.imag=" << c3.imag() << endl;
    
    // 执行复数的加法和减法
    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    
    // 计算复数c1的模
    cout << "abs(c1) = " << abs(c1) << endl;
    
    // 使用boolalpha输出复数对象的相等性比较结果
    cout << boolalpha;
    cout << "c1 == c2: " << (c1 == c2) << endl;
    cout << "c3 == c2: " << (c3 == c2) << endl;
    
    complex<double> c4 = 2; // 创建并初始化一个复数对象
    cout << "c4=" << c4 << endl;
    c4 += c1;  // 复数对象的复合赋值运算
    cout << "c4=" << c4 << endl;
}

int main() {
    test_std_complex();// 调用测试函数
    return 0;
}

 

//任务三 
#include <iostream>
#include <string>
using namespace std;

class T {
public:
    // 带有形参默认值的普通构造函数的声明 
    T(int x = 0, int y = 0);
    
    // 拷贝构造函数T(const T&t)声明 
    T(const T& t);
    
    // 移动构造函数T(T&&)声明 
    T(T&& t);
    
    // 析构函数~T声明 
    ~T();
    
    void set_m1(int x);
    int get_m1() const;
    int get_m2() const;
    void display() const;
    
    // 友元函数void func()声明
    friend void func();
    
private:
    int m1, m2;

public:
    // 静态成员函数声明 
    static void display_count();
    
public:
    // 静态常量const成员声明 
    static const string doc;
    static const int max_count;
    
private:
    // 静态成员变量声明 
    static int count;
};

// 静态常量成员 doc 和max_count的初始化 
const string T::doc{"a simple class"};
const int T::max_count = 99;

// 初始化静态成员变量count
int T::count = 0;

// 构造函数的实现
T::T(int x, int y) : m1{x}, m2{y} {
    ++count;
    cout << "constructor called." << endl;
}

// 拷贝构造函数的实现
T::T(const T& t) : m1{t.m1}, m2{t.m2} {
    ++count;
    cout << "copy constructor called." << endl;
}

// 移动构造函数的实现
T::T(T&& t) : m1{t.m1}, m2{t.m2} {
    ++count;
    cout << "move constructor called." << endl;
}

// 析构函数的实现
T::~T() {
    --count;
    cout << "destructor called." << endl;
}

// 成员函数的实现
void T::set_m1(int x) {
    m1 = x;
}

int T::get_m1() const {
    return m1;
}

int T::get_m2() const {
    return m2;
}

void T::display() const {
    cout << m1 << "," << m2 << endl;
}

// 静态成员函数的实现
void T::display_count() {
    cout << "T objects: " << count << endl;
}

// 友元函数的实现
void func() {
    T t1;
    t1.set_m1(55);
    t1.m2 = 77;
    t1.display();
}

void test() {
    cout << "T class info: " << T::doc << endl;
    cout << "T objects max_count: " << T::max_count << endl;
    T::display_count();
    
    T t1;
    t1.display();
    t1.set_m1(42);
    
    T t2{t1};
    t2.display();
    
    T t3{std::move(t1)};
    t3.display();
    t1.display();
    
    T::display_count();
}

int main() {
    cout << "======类 T=========" << endl;
    test();
    cout << endl;
    
    cout << "==========测试 func()============" << endl;
    func();
}

 

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Rect {
public:
    // 构造函数和成员函数声明 
    Rect(double l = 2.0, double w = 1.0);
    Rect(const Rect& r);
    ~Rect();
    double get_length() const;
    double get_width() const;
    double get_area() const;
    double get_circumference() const;
    void resize(double times);
    void resize(double l_times, double wide_times);

private:
    double length, width;

public:
    // 静态成员函数的声明 
    static void size_info();
    static const string doc;

private:
    // 静态成员变量(非const)的声明  
    static int size;
};

// 初始化静态成员变量
int Rect::size = 0;
const string Rect::doc = "a simple Rect class";

// 构造函数的实现
Rect::Rect(double l, double w) : length{l}, width{w} {
    ++size;
}

// 拷贝构造函数的实现
Rect::Rect(const Rect& r) : length{r.length}, width{r.width} {
    ++size;
}

// 析构函数的实现
Rect::~Rect() {
    --size;
}

double Rect::get_length() const {
    return length;
}

double Rect::get_width() const {
    return width;
}

double Rect::get_area() const {
    return length * width;
}

double Rect::get_circumference() const {
    return 2 * (length + width);
}

void Rect::resize(double times) {
    length *= times;
    width *= times;
}

void Rect::resize(double l_times, double wide_times) {
    length *= l_times;
    width *= wide_times;
}

void Rect::size_info() {
    cout << "当前矩形对象数目:" << size << endl;
}

// 输出矩形信息
void output(const Rect& r) {
    cout << "矩形信息:" << endl;
    cout << "长:     " << left << setw(9) << fixed << setprecision(2) << r.get_length() << endl;
    cout << "宽:     " << r.get_width() << endl;
    cout << "面积:   " << r.get_area() << endl;
    cout << "周长:   " << r.get_circumference() << endl;
}

void test() {
    cout << "矩形类信息:" << Rect::doc << endl;
    Rect::size_info();

    Rect r1;
    output(r1);

    Rect r2(4, 3);
    output(r2);

    Rect r3(r2);
    r3.resize(2);
    output(r3);

    r3.resize(5, 2);
    output(r3);

    Rect::size_info();
}

int main() {
    test();
    cout << "当前矩形对象数目:";
    Rect::size_info();

    return 0;
}

#include <iostream>
#include <cmath>

class Complex {
public:
    Complex(double x0 = 0, double y0 = 0); // 构造函数
    Complex(const Complex& c); // 拷贝构造函数
    ~Complex(); // 析构函数
    double get_real() const; // 获取实部
    double get_imag() const; // 获取虚部
    void show() const; // 显示复数
    void add(const Complex &c); // 复数相加
    friend Complex add(const Complex &c1, const Complex &c2); // 友元函数,复数相加
    friend bool is_equal(const Complex &c1, const Complex &c2); // 友元函数,判断两个复数是否相等
    friend void abs(const Complex &c); // 友元函数,计算复数的模

private:
    double real, imag; // 实部和虚部
};

Complex::Complex(double x0, double y0) : real{x0}, imag{y0} {} // 构造函数的实现

Complex::Complex(const Complex& c) : real{c.real}, imag{c.imag} {} // 拷贝构造函数的实现

double Complex::get_real() const {
    return real;
}

double Complex::get_imag() const {
    return imag;
}

void Complex::show() const {
    std::cout << real;
    if (imag > 0)
        std::cout << "+" << imag << "i";
    if (imag < 0)
        std::cout << imag << "i";
}

void Complex::add(const Complex& c) {
    real += c.real;
    imag += c.imag;
}

Complex add(const Complex& c1, const Complex& c2) {
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}

void abs(const Complex& c) {
    double mo = sqrt(c.real * c.real + c.imag * c.imag);
    std::cout << mo;
}

bool is_equal(const Complex& c1, const Complex& c2) {
    return c1.real == c2.real && c1.imag == c2.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)=";
    abs(c1);
    cout << 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();
    return 0;
}

 

 

作业一

个人体会 /b/b控制格式的时候,如前面未加上空格容易将位数前移一个导致9被抹去,如果不加空格只需要一个/b。

 

作业二

使用complex<T> 模板类型的实例表示的是复数,标准库了 3 个特化类型:complex<float>、complex<double>、complex<long double>。在这一节中,全部使用 complex<double>,但其他特化类型的操作是基本相同的。

 

作业四感悟

实践发现如果空格不对齐,输出默认是右对齐,这时候就可以用left和setw控制输出格式

实践发现控制输出格式的时候不需要每一行都left<<setw()<<fixed<<setprecision()

 查阅资料得知在输出不同的成员时,你不需要在每一行都写一旦你在某一行中使用了输出格式控制符,它们会持续影响到后续的输出,直到你显式地更改它们或程序结束。你可以在需要的地方单独设置一次,而后续的输出将继续使用这些设置,直到你修改它们或程序结束。

 

作业五感悟

析构函数定义了别忘记初始化,当函数体内部没有内容的时候也要加{}

当你的类没有自定义的析构函数时,编译器会生成默认的析构函数,它会销毁类的成员变量,但不执行其他操作。如果你的类不需要进行额外的清理工作,可以让编译器生成的默认析构函数负责对象的销毁。