实验2 类和对象_基础编程1

实验2 类和对象_基础编程1

实验任务 1

t.h

#pragma once

#include <string>

// 类T: 声明
class T {
  // 对象属性、方法
public:
  T(int x = 0, int y = 0); // 普通构造函数
  T(const T &t);           // 复制构造函数
  T(T &&t);                // 移动构造函数
  ~T();                    // 析构函数

  void adjust(int ratio); // 按系数成倍调整数据
  void display() const;   // 以(m1, m2)形式显示T类对象信息

private:
  int m1, m2;

  // 类属性、方法
public:
  static int get_cnt(); // 显示当前T类对象总数

public:
  static const std::string doc; // 类T的描述信息
  static const int max_cnt;     // 类T对象上限

private:
  static int cnt; // 当前T类对象数目

  // 类T友元函数声明
  friend void func();
};

// 普通函数声明
void func();

t.cpp

// 类T: 实现
// 普通函数实现

#include "t.h"
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

// static成员数据类外初始化
const std::string T::doc{"a simple class sample"};
const int T::max_cnt = 999;
int T::cnt = 0;

// 对象方法
T::T(int x, int y) : m1{x}, m2{y} {
  ++cnt;
  cout << "T constructor called.\n";
}

T::T(const T &t) : m1{t.m1}, m2{t.m2} {
  ++cnt;
  cout << "T copy constructor called.\n";
}

T::T(T &&t) : m1{t.m1}, m2{t.m2} {
  ++cnt;
  cout << "T move constructor called.\n";
}

T::~T() {
  --cnt;
  cout << "T destructor called.\n";
}

void T::adjust(int ratio) {
  m1 *= ratio;
  m2 *= ratio;
}

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

// 类方法
int T::get_cnt() { return cnt; }

// 友元
void func() {
  T t5(42);
  t5.m2 = 2049;
  cout << "t5 = ";
  t5.display();
  cout << endl;
}

task1.cpp

#include "t.h"
#include <iostream>

using std::cout;
using std::endl;

void test();

int main() {
  test();
  cout << "\nmain: \n";
  cout << "T objects'current count: " << T::get_cnt() << endl;
}

void test() {
  cout << "test class T: \n";
  cout << "T info: " << T::doc << endl;
  cout << "T objects'max count: " << T::max_cnt << endl;
  cout << "T objects'current count: " << T::get_cnt() << endl << endl;

  T t1;
  cout << "t1 = ";
  t1.display();
  cout << endl;

  T t2(3, 4);
  cout << "t2 = ";
  t2.display();
  cout << endl;

  T t3(t2);
  t3.adjust(2);
  cout << "t3 = ";
  t3.display();
  cout << endl;

  T t4(std::move(t2));
  cout << "t3 = ";
  t4.display();
  cout << endl;

  cout << "T objects'current count: " << T::get_cnt() << endl;

  func();
}

问题1:


错误原因:'func' was not declared in this scope

问题2:

  1. 普通构造函数 T(int x = 0, int y = 0)

    • 功能:用于创建 T 类的对象,并初始化对象的属性 m1 和 m2。可以通过参数传递自定义的初始值,若未提供则默认初始化为 0。
    • 调用时机:在创建 T 类对象时自动调用,例如 T obj(5, 10);
  2. 复制构造函数 T(const T &t)

    • 功能:用于通过已有的 T 类对象创建新对象,通常用于实现对象的复制。在复制时,会将传入对象 t 的 m1 和 m2 属性复制到新对象中。
    • 调用时机:当用一个已有对象初始化另一个对象时自动调用,例如 T obj2(obj);
  3. 移动构造函数 T(T &&t)

    • 功能:用于通过移动语义优化对象的创建,将资源的所有权从一个对象转移到另一个对象。移动构造函数通常会将原对象的属性值“窃取”给新对象,同时将原对象的属性置为无效状态。
    • 调用时机:当用一个临时对象初始化另一个对象时自动调用,例如 T obj3(std::move(obj2));
  4. 析构函数 ~T()

    • 功能:用于在对象生命周期结束时释放对象占用的资源,例如释放动态分配的内存。
    • 调用时机:当对象生命周期结束,或者对象超出作用域时自动调用,例如,当 objobj2obj3 的作用域结束时,析构函数会被调用。

问题3:

不能

ld: 3 duplicate symbols

实验任务 2

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <cmath>
#include <string>

class Complex {
  double real;
  double imag;

public:
  static const std::string doc;

  Complex();
  Complex(double real);
  Complex(double real, double imag);
  Complex(const Complex &other);

  double get_real() const;
  double get_imag() const;

  void add(const Complex &other);
  friend Complex add(const Complex &c1, const Complex &c2);

  friend bool is_equal(const Complex &c1, const Complex &c2);
  friend bool is_not_equal(const Complex &c1, const Complex &c2);

  friend void output(const Complex &c);
  friend double abs(const Complex &c);
};

#endif // COMPLEX_H

Complex.cpp

#include "Complex.h"
#include <iostream>

const std::string Complex::doc = "a simplified complex class";

Complex::Complex() : real(0), imag(0) {}

Complex::Complex(double real) : real(real), imag(0) {}

Complex::Complex(double real, double imag) : real(real), imag(imag) {}

Complex::Complex(const Complex &other) : real(other.real), imag(other.imag) {}

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

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

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

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

bool is_equal(const Complex &c1, const Complex &c2) {
  return c1.real == c2.real && c1.imag == c2.imag;
}

bool is_not_equal(const Complex &c1, const Complex &c2) {
  return c1.real != c2.real || c1.imag != c2.imag;
}

void output(const Complex &c) {
  if (c.imag == 0) {
    std::cout << c.real;
  }
  else if (c.imag > 0) {
    std::cout << c.real << "+" << c.imag << "i";
  }
  else {
    std::cout << c.real << c.imag << "i";
  }
}

double abs(const Complex &c) {
  return std::sqrt(c.real * c.real + c.imag * c.imag);
}

test2.cpp

#include "Complex.h"
#include <iostream>

using std::boolalpha;
using std::cout;
using std::endl;

void test() {
  cout << "类成员测试: " << endl;
  cout << Complex::doc << endl;

  cout << endl;

  cout << "Complex对象测试: " << endl;
  Complex c1;
  Complex c2(3, -4);
  const Complex c3(3.5);
  Complex c4(c3);

  cout << "c1 = ";
  output(c1);
  cout << endl;
  cout << "c2 = ";
  output(c2);
  cout << endl;
  cout << "c3 = ";
  output(c3);
  cout << endl;
  cout << "c4 = ";
  output(c4);
  cout << endl;
  cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag()
       << endl;

  cout << endl;

  cout << "复数运算测试: " << endl;
  cout << "abs(c2) = " << abs(c2) << endl;
  c1.add(c2);
  cout << "c1 += c2, c1 = ";
  output(c1);
  cout << endl;
  cout << boolalpha;
  cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
  cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
  c4 = add(c2, c3);
  cout << "c4 = c2 + c3, c4 = ";
  output(c4);
  cout << endl;
}

int main() { test(); }

实验任务 3

#include <complex>
#include <iostream>

using std::boolalpha;
using std::complex;
using std::cout;
using std::endl;

void test() {
  cout << "标准库模板类complex测试: " << endl;
  complex<double> c1;
  complex<double> c2(3, -4);
  const complex<double> c3(3.5);
  complex<double> c4(c3);

  cout << "c1 = " << c1 << endl;
  cout << "c2 = " << c2 << endl;
  cout << "c3 = " << c3 << endl;
  cout << "c4 = " << c4 << endl;
  cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
  cout << endl;

  cout << "复数运算测试: " << endl;
  cout << "abs(c2) = " << abs(c2) << endl;
  c1 += c2;
  cout << "c1 += c2, c1 = " << c1 << endl;
  cout << boolalpha;
  cout << "c1 == c2 : " << (c1 == c2) << endl;
  cout << "c1 != c3 : " << (c1 != c3) << endl;
  c4 = c2 + c3;
  cout << "c4 = c2 + c3, c4 = " << c4 << endl;
}

int main() { test(); }

观察:c++stl 实现了 complex<T> operator "" i(T)

实验任务 4

Fraction.h

// Fraction.h
#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>
#include <string>

class Fraction {
public:
  static const std::string doc;

  Fraction(int up = 0, int down = 1); // 构造函数
  Fraction(const Fraction &other);    // 拷贝构造函数
  ~Fraction() {};                     // 析构函数

  int get_up() const;
  int get_down() const;
  Fraction negative() const;

  friend Fraction add(const Fraction &f1, const Fraction &f2);
  friend Fraction sub(const Fraction &f1, const Fraction &f2);
  friend Fraction mul(const Fraction &f1, const Fraction &f2);
  friend Fraction div(const Fraction &f1, const Fraction &f2);
  friend void output(const Fraction &f);

private:
  int up;          // 分子
  int down;        // 分母
  void simplify(); // 化简分数
};

#endif

Fraction.cpp

// Fraction.cpp
#include "Fraction.h"
#include <numeric>

const std::string Fraction::doc =
    "Fraction类 v 0.01版.\n"
    "目前仅支持分数对象的构造、输出、加/减/乘/除运算.";

// 构造函数
Fraction::Fraction(int up, int down) : up(up), down(down) {
  if (down == 0) {
    throw std::invalid_argument("Denominator cannot be zero.");
  }
  simplify();
}

// 拷贝构造函数
Fraction::Fraction(const Fraction &other) : up(other.up), down(other.down) {}

// 获取分子
int Fraction::get_up() const { return up; }

// 获取分母
int Fraction::get_down() const { return down; }

// 求负
Fraction Fraction::negative() const { return Fraction(-up, down); }

// 分数加法
Fraction add(const Fraction &f1, const Fraction &f2) {
  int up = f1.up * f2.down + f2.up * f1.down;
  int down = f1.down * f2.down;
  return Fraction(up, down);
}

// 分数减法
Fraction sub(const Fraction &f1, const Fraction &f2) {
  int up = f1.up * f2.down - f2.up * f1.down;
  int down = f1.down * f2.down;
  return Fraction(up, down);
}

// 分数乘法
Fraction mul(const Fraction &f1, const Fraction &f2) {
  int up = f1.up * f2.up;
  int down = f1.down * f2.down;
  return Fraction(up, down);
}

// 分数除法
Fraction div(const Fraction &f1, const Fraction &f2) {
  if (f2.up == 0) {
    throw std::invalid_argument("Denominator cannot be zero.");
  }
  int up = f1.up * f2.down;
  int down = f1.down * f2.up;
  return Fraction(up, down);
}

// 输出分数
void output(const Fraction &f) {
  if (f.down == 1) {
    std::cout << f.up;
    return;
  }
  std::cout << f.up << "/" << f.down;
}

// 化简分数
void Fraction::simplify() {
  int gcd = std::gcd(abs(up), abs(down));
  up /= gcd;
  down /= gcd;
  if (down < 0) {
    up = -up;
    down = -down;
  }
}

test4.cpp

#include "Fraction.h"
#include <iostream>

using std::cout;
using std::endl;

void test1() {
  cout << "Fraction类测试: " << endl;
  cout << Fraction::doc << endl << endl;

  Fraction f1(5);
  Fraction f2(3, -4), f3(-18, 12);
  Fraction f4(f3);
  cout << "f1 = ";
  output(f1);
  cout << endl;
  cout << "f2 = ";
  output(f2);
  cout << endl;
  cout << "f3 = ";
  output(f3);
  cout << endl;
  cout << "f4 = ";
  output(f4);
  cout << endl;

  Fraction f5(f4.negative());
  cout << "f5 = ";
  output(f5);
  cout << endl;
  cout << "f5.get_up() = " << f5.get_up()
       << ", f5.get_down() = " << f5.get_down() << endl;

  cout << "f1 + f2 = ";
  output(add(f1, f2));
  cout << endl;
  cout << "f1 - f2 = ";
  output(sub(f1, f2));
  cout << endl;
  cout << "f1 * f2 = ";
  output(mul(f1, f2));
  cout << endl;
  cout << "f1 / f2 = ";
  output(div(f1, f2));
  cout << endl;
  cout << "f4 + f5 = ";
  output(add(f4, f5));
  cout << endl;
}

void test2() {
  Fraction f6(42, 55), f7(0, 3);
  cout << "f6 = ";
  output(f6);
  cout << endl;
  cout << "f7 = ";
  output(f7);
  cout << endl;
  cout << "f6 / f7 = ";
  output(div(f6, f7));
  cout << endl;
}

int main() {
  cout << "测试1: Fraction类基础功能测试\n";
  test1();

  cout << "\n测试2: 分母为0测试: \n";
  test2();
}

实验任务 5

(没教材)

...

实验总结

posted @ 2024-10-23 08:47  iuy1  阅读(53)  评论(0编辑  收藏  举报