实验二
实验任务1:
1 #pragma once 2 #include <string> 3 // 类T: 声明 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 adjust(int ratio); // 按系数成倍调整数据 12 void display() const; // 以(m1, m2)形式显示T类对象信息 13 private: 14 int m1, m2; 15 // 类属性、方法 16 public: 17 static int get_cnt(); // 显示当前T类对象总数 18 public: 19 static const std::string doc; // 类T的描述信息 20 static const int max_cnt; // 类T对象上限 21 private: 22 static int cnt; // 当前T类对象数目 23 // 类T友元函数声明 24 friend void func(); 25 }; 26 // 普通函数声明 27 void func();
1 #include "T.h" 2 #include <iostream> 3 #include <string> 4 // 类T实现 5 // static成员数据类外初始化 6 const std::string T::doc{ "a simple class sample" }; 7 const int T::max_cnt = 999; 8 int T::cnt = 0; 9 // 类方法 10 int T::get_cnt() { 11 return cnt; 12 } 13 // 对象方法 14 T::T(int x, int y) : m1{ x }, m2{ y } { 15 ++cnt; 16 std::cout << "T constructor called.\n"; 17 } 18 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } { 19 ++cnt; 20 std::cout << "T copy constructor called.\n"; 21 } 22 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } { 23 ++cnt; 24 std::cout << "T move constructor called.\n"; 25 } 26 T::~T() { 27 --cnt; 28 std::cout << "T destructor called.\n"; 29 } 30 void T::adjust(int ratio) { 31 m1 *= ratio; 32 m2 *= ratio; 33 } 34 void T::display() const { 35 std::cout << "(" << m1 << ", " << m2 << ")"; 36 } 37 // 普通函数实现 38 void func() { 39 T t5(42); 40 t5.m2 = 2049; 41 std::cout << "t5 = "; t5.display(); std::cout << '\n'; 42 }
1 #include "T.h" 2 #include <iostream> 3 void test_T(); 4 int main() { 5 std::cout << "test Class T: \n"; 6 test_T(); 7 std::cout << "\ntest friend func: \n"; 8 func(); 9 } 10 void test_T() { 11 using std::cout; 12 using std::endl; 13 cout << "T info: " << T::doc << endl; 14 cout << "T objects'max count: " << T::max_cnt << endl; 15 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 16 T t1; 17 cout << "t1 = "; t1.display(); cout << endl; 18 T t2(3, 4); 19 cout << "t2 = "; t2.display(); cout << endl; 20 T t3(t2); 21 t3.adjust(2); 22 cout << "t3 = "; t3.display(); cout << endl; 23 T t4(std::move(t2)); 24 cout << "t4 = "; t4.display(); cout << endl; 25 cout << "test: T objects'current count: " << T::get_cnt() << endl; 26 }
运行结果:

问题1:
出错,提示error C3861:“func”:找不到标识符
原因:类里面的声明只是说func是T的友元函数,但是并没有在命名空间里生成一个可用的符号声明
问题2:
1:初始化类时调用,初始化x,y参数,默认为0
2:复制构造函数,将括号里的类复制到目标类中,当括号内是已有对象时调用
3:移动构造函数,将括号内的类资源移动到目标类,一般括号内是临时值时调用
4:析构函数,在类被销毁时触发
问题3:
不能
static属于一个类,两个cpp文件都包含h头文件,会出现重复定义,但是static对象是不能重复定义的
实验任务2
Complex.h
1 #pragma once 2 #include<string> 3 4 class Complex 5 { 6 private: 7 8 double real, imag; 9 public: 10 static const std::string doc; 11 Complex& operator=(const Complex&) = default; 12 Complex(double a = 0, double b = 0); 13 Complex(const Complex &t); 14 Complex(Complex&& t); 15 ~Complex(); 16 17 double get_real() const; 18 double get_imag() const; 19 Complex& add(const Complex &t); 20 friend void output(const Complex &t); 21 friend double abs(const Complex &t); 22 friend Complex add(const Complex& a, const Complex& b); 23 friend bool is_equal(const Complex& a, const Complex& b); 24 friend bool is_not_equal(const Complex& a, const Complex& b); 25 }; 26 27 void output(const Complex &t); 28 double abs(const Complex& t); 29 Complex add(const Complex& a, const Complex& b); 30 bool is_equal(const Complex& a, const Complex& b); 31 bool is_not_equal(const Complex& a, const Complex& b);
Complex.cpp
1 #include "Complex.h" 2 #include <iostream> 3 #include <cmath> 4 5 const std::string Complex::doc{ "a simplified complex class" }; 6 7 Complex::Complex(double a, double b) :real{ a }, imag{ b } {} 8 Complex::Complex(const Complex& t) : real{t.real}, imag{ t.imag } {} 9 Complex::Complex(Complex&& t) : real{ t.real }, imag{ t.imag } {} 10 Complex::~Complex() {} 11 12 double Complex:: get_imag()const { 13 return imag; 14 } 15 16 double Complex::get_real()const { 17 return real; 18 } 19 20 Complex& Complex::add(const Complex& t){ 21 real += t.real; 22 imag += t.imag; 23 return *this; 24 } 25 26 void output(const Complex &t) { 27 std::cout << t.real ; 28 if (t.imag >= 0) 29 std::cout << '+'; 30 std::cout<< t.imag << 'i'; 31 } 32 33 double abs(const Complex& t) { 34 double c = t.imag * t.imag + t.real * t.real; 35 return sqrt(c); 36 } 37 38 Complex add(const Complex& a, const Complex& b) { 39 Complex t3; 40 t3.imag = a.imag + b.imag; 41 t3.real = a.real + b.real; 42 return t3; 43 } 44 45 bool is_equal(const Complex& a, const Complex& b) { 46 if (a.imag == b.imag && a.real == b.real) 47 return true; 48 return false; 49 } 50 51 bool is_not_equal(const Complex& a, const Complex& b) { 52 if (a.imag != b.imag || a.real != b.real) 53 return true; 54 return false; 55 }
task2.cpp
#include <iostream> #include <iomanip> #include <complex> #include "Complex.h" void test_Complex(); void test_std_complex(); int main() { std::cout << "*******测试1: 自定义类Complex*******\n"; test_Complex(); std::cout << "\n*******测试2: 标准库模板类complex*******\n"; test_std_complex(); } void test_Complex() { using std::cout; using std::endl; using std::boolalpha; cout << "类成员测试: " << endl; cout << Complex::doc << endl << endl; cout << "Complex对象测试: " << endl; Complex c1; Complex c2(3, -4); Complex c3(c2); Complex c4 = c2; const Complex c5(3.5); 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 << "c5.real = " << c5.get_real() << ", c5.imag = " << c5.get_imag() << endl << 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 != c2 : " << is_not_equal(c1, c2) << endl; c4 = add(c2, c3); cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; } void test_std_complex() { using std::cout; using std::endl; using std::boolalpha; cout << "std::complex<double>对象测试: " << endl; std::complex<double> c1; std::complex<double> c2(3, -4); std::complex<double> c3(c2); std::complex<double> c4 = c2; const std::complex<double> c5(3.5); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c3 = " << c3 << endl; cout << "c4 = " << c4 << endl; cout << "c5.real = " << c5.real() << ", c5.imag = " << c5.imag() << endl << 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 != c2 : " << (c1 != c2) << endl; c4 = c2 + c3; cout << "c4 = c2 + c3, c4 = " << c4 << endl; }
运行结果:

问题一:
标准库更简洁
有关联
问题二:
是
都使用了private数据
否
问题三:
显示写拷贝构造函数
实验任务3
PlayerControl.h
1 #pragma once 2 #include <string> 3 enum class ControlType { Play, Pause, Next, Prev, Stop, Unknown }; 4 class PlayerControl { 5 public: 6 PlayerControl(); 7 ControlType parse(const std::string& control_str); // 实现std::string -->ControlType转换 8 void execute(ControlType cmd) const; // 执行控制操作(以打印输出模拟) 9 static int get_cnt(); 10 private: 11 static int total_cnt; 12 };
PlayerControl.cpp
#include "PlayerControl.h" #include <iostream> #include <algorithm> #include <cctype> int PlayerControl::total_cnt = 0; PlayerControl::PlayerControl() {} // 待补足 // 1. 将输入字符串转为小写,实现大小写不敏感 // 2. 匹配"play"/"pause"/"next"/"prev"/"stop"并返回对应枚举 // 3. 未匹配的字符串返回ControlType::Unknown // 4. 每次成功调用parse时递增total_cnt ControlType PlayerControl::parse(const std::string& control_str) { std::string str = control_str; std::transform(str.begin(), str.end(), str.begin(), ::tolower); PlayerControl::total_cnt++; if (str == "play") return ControlType::Play; else if (str == "pause") return ControlType::Pause; else if (str == "next") return ControlType::Next; else if (str == "prev") return ControlType::Prev; else if (str == "stop") return ControlType::Stop; else return ControlType::Unknown; } void PlayerControl::execute(ControlType cmd) const { 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; }
task3.cpp
#include "PlayerControl.h" #include <iostream> void test() { PlayerControl controller; std::string control_str; std::cout << "Enter Control: (play/pause/next/prev/stop/quit):\n"; while (std::cin >> control_str) { if (control_str == "quit") break; ControlType cmd = controller.parse(control_str); controller.execute(cmd); std::cout << "Current Player control: " << PlayerControl::get_cnt() << "\n\n"; } } int main() { test(); }

实验任务4
#pragma once #include <string> class Fraction { public: Fraction(int a = 0, int b = 1); Fraction(const Fraction& ); int get_up() const; int get_down() const; Fraction negative()const; static const std::string doc; void simplify(); int gcd(int a, int b); private: int up, down; static int gcd2(int a, int b); }; void output(const Fraction&); Fraction add(const Fraction&, const Fraction&); Fraction sub(const Fraction&, const Fraction&); Fraction mul(const Fraction&, const Fraction&); Fraction div(const Fraction&, const Fraction&);
Fraction.cpp
#include "Fraction.h" #include <iostream> #include <string> const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加 / 减 / 乘 / 除运算."; int Fraction::gcd2(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } Fraction::Fraction(int a, int b) : up { a }, down{b}{ } Fraction::Fraction(const Fraction& other) :up{ other.up }, down{ other.down } { } int Fraction::get_up()const { int g = gcd2(abs(up), abs(down)); return up/g; } int Fraction::get_down()const { int g = gcd2(abs(up), abs(down)); return down/g; } Fraction Fraction::negative() const { return Fraction(-up, down); } void output(const Fraction& f) { Fraction temp = f; temp.simplify(); if (temp.get_up() == 0 && temp.get_down() != 0) std::cout << 0; else if (temp.get_down() == 1) std::cout << temp.get_up(); else if (temp.get_down() != 0 && temp.get_up() != 0) std::cout << temp.get_up() << "/" << temp.get_down(); else std::cout << "分母不能为0"; } Fraction add(const Fraction& a, const Fraction& b) { int u = a.get_up() * b.get_down() + b.get_up() * a.get_down(); int d = a.get_down() * b.get_down(); return Fraction(u, d); } Fraction sub(const Fraction& a, const Fraction& b) { int u = a.get_up() * b.get_down() - b.get_up() * a.get_down(); int d = a.get_down() * b.get_down(); return Fraction(u, d); } Fraction mul(const Fraction& a, const Fraction& b) { return Fraction(a.get_up() * b.get_up(), a.get_down() * b.get_down()); } Fraction div(const Fraction& a, const Fraction& b) { if (b.get_up() == 0) return Fraction(a.get_up() * b.get_down(), 0); return Fraction(a.get_up() * b.get_down(), a.get_down() * b.get_up()); } void Fraction::simplify() { int g = gcd(abs(up), abs(down)); up /= g; down /= g; if (down < 0){ up = -up; down = -down; } } int Fraction::gcd(int a, int b) { while (b != 0) { int t = a % b; a = b; b = t; } return a; }
task4.cpp
#include "Fraction.h" #include <iostream> void test1(); void test2(); int main() { std::cout << "测试1: Fraction类基础功能测试\n"; test1(); std::cout << "\n测试2: 分母为0测试: \n"; test2(); } void test1() { using std::cout; using std::endl; 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; const 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() { using std::cout; using std::endl; 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; }

浙公网安备 33010602011771号