实验二
task1
问题一
T(int x = 0, int y = 0); // 普通构造函数
用来初始化对象,在创建新对象时调用。
T(const T &t); // 复制构造函数
用一个已存在的对象来初始化新对象,在使用这个功能、或者是对象作为返回值时调用
T(T &&t); // 移动构造函数
通过直接修改临时对象的资源来初始化新对象,避免深拷贝,在使用临时对象初始化新对象时调用
~T(); // 析构函数
用于释放内存,调用于对象离开作用域或者临时对象生命周期结束时;
问题二
可以成功编译
问题三
作用域:对应的{}内
不能,体现了类的封装性。
问题四
会被正常销毁,在出现异常时,析构函数依然会调用,在异常结果报告出来前将其正常销毁,避免内存的占用。
task2
问题一
类的声明
`#pragma once
include
class Complex {
public:
static const std::string doc;
Complex(double r = 0.0, double i = 0.0);
Complex(const Complex& c);
double real() const;
double imag() const;
void add(const Complex& c);
friend void output(const Complex& c);
friend double abs(const Complex& c);
friend Complex add(const Complex& a, const Complex& b);
friend bool is_equal(const Complex& a, const Complex& b);
friend bool is_not_equal(const Complex& a, const Complex& b);
private:
double real_;
double imag_;
};`
类的实现
`#include "Complex.h"
include
include
const std::string Complex::doc = "a simplified complex class";
Complex::Complex(double r, double i) : real_(r), imag_(i) {}
Complex::Complex(const Complex& c) : real_(c.real_), imag_(c.imag_) {}
double Complex::real() const { return real_; }
double Complex::imag() const { return imag_; }
void Complex::add(const Complex& c) {
real_ += c.real_;
imag_ += c.imag_;
}
void output(const Complex& c) {
std::cout << c.real_ << " + " << c.imag_ << "i";
}
double abs(const Complex& c) {
return std::sqrt(c.real_ * c.real_ + c.imag_ * c.imag_);
}
Complex add(const Complex& a, const Complex& b) {
return Complex(a.real_ + b.real_, a.imag_ + b.imag_);
}
bool is_equal(const Complex& a, const Complex& b) {
return a.real_ == b.real_ && a.imag_ == b.imag_;
}
bool is_not_equal(const Complex& a, const Complex& b) {
return !is_equal(a, b);
}`
问题一
1.必须加const,一个const常量去调用一个非const成员函数,有修改值的可能性,从而导致编译错误。

2..编译不能通过,因为add未经const修饰,体现了类的只读保护
问题二
标准库类模板更简洁,运算符与函数定义名称基本相同。
问题三
必须加,因为类中的两个变量是私有的,如果不加friend的话是无法直接访问到类中的2个变量的,所以必须加上friend,以共享自己的private变量,方便运算。
task3
问题一

类的声明
`#pragma once
include
enum class ControlType {Play, Pause, Next, Prev, Stop, Unknown};
class PlayerControl {
public:
PlayerControl();
ControlType parse(const std::string& control_str);
void execute(ControlType cmd) const;
static int get_cnt();
private:
static int total_cnt;
};`
类的实现
`#include "PlayerControl.h"
include
include
int PlayerControl::total_cnt = 0;
PlayerControl::PlayerControl() {}
ControlType PlayerControl::parse(const std::string& control_str) {
}
void PlayerControl::execute(ControlType cmd) const {
++total_cnt;
switch (cmd) {
case ControlType::Play: std::cout << "[play] Playing music...\n"; break;
case ControlType::Pause: std::cout << "[Pause] Music paused\n"; break;
case ControlType::Next: std::cout << "[Next] Skipping to next track\n"; break;
case ControlType::Prev: std::cout << "[Prev] Back to previous track\n"; break;
case ControlType::Stop: std::cout << "[Stop] Music stopped\n"; break;
default: std::cout << "[Error] unknown control\n"; break;
}
}
int PlayerControl::get_cnt() {
return total_cnt;
}`
问题
由于const保护的是对象自身的非静态成员函数,而static数据是独立于对象而存在的,在任何地方或者函数中都可以被操作修改。
task4
`#pragma once
include
class Fraction {
public:
static const std::string doc;
Fraction(int up0 = 0, int down0 = 1);
Fraction(const Fraction& f) = default;
int get_up() const;
int get_down() const;
Fraction negative() const;
friend void output(const Fraction& f);
friend Fraction add(const Fraction& a, const Fraction& b);
friend Fraction sub(const Fraction& a, const Fraction& b);
friend Fraction mul(const Fraction& a, const Fraction& b);
friend Fraction div(const Fraction& a, const Fraction& b);
private:
int up;
int down;
void normalize();
};
include "Fraction.h"
include
include
include
const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
Fraction::Fraction(int up0, int down0) : up(up0), down(down0) {
if (down == 0) {
throw std::invalid_argument("Fraction: 分母不能为0");
}
normalize();
}
int Fraction::get_up() const { return up; }
int Fraction::get_down() const { return down; }
Fraction Fraction::negative() const {
return Fraction(-up, down);
}
void Fraction::normalize() {
if (up == 0) {
down = 1;
return;
}
if (down < 0) {
up = -up;
down = -down;
}
int g = std::gcd(std::abs(up), down);
up /= g;
down /= g;
}
void output(const Fraction& f) {
if (f.down == 1) {
std::cout << f.up;
} else {
std::cout << f.up << "/" << f.down;
}
}
Fraction add(const Fraction& a, const Fraction& b) {
long long new_up = (long long)a.up * b.down + (long long)b.up * a.down;
long long new_down = (long long)a.down * b.down;
return Fraction(static_cast
}
Fraction sub(const Fraction& a, const Fraction& b) {
long long new_up = (long long)a.up * b.down - (long long)b.up * a.down;
long long new_down = (long long)a.down * b.down;
return Fraction(static_cast
}
Fraction mul(const Fraction& a, const Fraction& b) {
long long new_up = (long long)a.up * b.up;
long long new_down = (long long)a.down * b.down;
return Fraction(static_cast
}
Fraction div(const Fraction& a, const Fraction& b) {
if (b.up == 0) {
throw std::invalid_argument("Fraction: 除数为0");
}
long long new_up = (long long)a.up * b.down;
long long new_down = (long long)a.down * b.up;
return Fraction(static_cast
}`

采用方案一,采用友元函数可以避免破坏封装性。

浙公网安备 33010602011771号