实验任务一
源代码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:
不能
fun要访问T的m2的话 没有友元关系 就无法访问
问题2:
-
普通构造函数 T(int x = 0, int y = 0);
功能:创建一个 T 类的对象,允许用户指定成员变量 m1 和 m2 的初始值,默认为 0。
调用时机:当使用默认参数或提供具体参数创建对象时,如 T t1; 或 T t2(3, 4);。
复制构造函数 T(const T &t);
功能:通过已有的 T 类对象来初始化新的对象,实现成员变量的拷贝。
调用时机:当以现有对象初始化新对象时,如 T t3(t2);,或当对象以值传递方式作为函数参数或返回值时。
移动构造函数 T(T &&t);
功能:移动资源,而非复制,用于优化临时对象或右值的初始化,减少不必要的拷贝。
调用时机:当使用 std::move 转换为右值引用,或函数返回临时对象时,如 T t4(std::move(t2));。
析构函数 ~T();
功能:在对象生命周期结束时执行清理操作,如释放资源、更新静态计数器等。
调用时机:当对象超出其作用域、被显式删除或程序结束时,系统自动调用析构函数
问题3:
不能
实验任务二:
#include <iostream> #include <cmath> class Complex { private: double real; double imag; public: // 构造函数 Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} // 获取实部 double get_real() { return real; } // 获取虚部 double get_imag() { return imag; } // 加法操作 void add(Complex c) { real += c.real; imag += c.imag; } // 计算复数的模 double abs() { return std::sqrt(real * real + imag * imag); } // 相等判断 bool is_equal(Complex c) { return (real == c.real) && (imag == c.imag); } // 不相等判断 bool is_not_equal(Complex c) { return!is_equal(c); } // 输出复数 void output() { std::cout << real << " + " << imag << "i" << std::endl; } }; // 复数相加函数 Complex add(Complex c1, Complex c2) { return Complex(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag()); } void test() { Complex c1(0, 0); Complex c2(3, -4); Complex c3(3.5, 0); Complex c4(3.5, 0); std::cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << std::endl; std::cout << std::endl; std::cout << "复数运算测试:" << std::endl; std::cout << "abs(c2) = " << c2.abs() << std::endl; c1.add(c2); std::cout << "c1 += c2, c1 = "; c1.output(); std::cout << std::boolalpha; std::cout << "c1 == c2 : " << c1.is_equal(c2) << std::endl; std::cout << "c1!= c3 : " << c1.is_not_equal(c3) << std::endl; c4 = add(c2, c3); std::cout << "c4 = c2 + c3, c4 = "; c4.output(); } int main() { test(); return 0; }
实验任务三:
计算复数的模:通过 abs(c2)
计算了复数 c2
的模,即 sqrt(3^2 + (-4)^2) = sqrt(25) = 5
。
复数的加法运算:在 c4 = c2 + c3
中,对复数进行了加法操作。
复数的赋值和比较:包括 c1 += c2
进行赋值,以及 c1 = c2
和 c1!= c3
的比较操作。
实验任务四:
Fraction.h
#ifndef FRACTION_H #define FRACTION_H #include <iostream> class Fraction { public: // 描述类的doc字符串,常量,公有 static const std::string doc; // 构造函数 Fraction(int up = 0, int down = 1); Fraction(const Fraction& other); // 获取分子 int get_up() const; // 获取分母 int get_down() const; // 求负运算 Fraction negative() const; // 友元函数声明 friend void output(const Fraction& fraction); 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); private: // 分子和分母,私有 int up; int down; // 内部工具函数,用于化简分数 void simplify(); }; #endif
Fraction.cpp
#include "Fraction.h" // 初始化类的doc字符串 const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算."; // 构造函数 Fraction::Fraction(int up, int down) : up(up), down(down) { if (down == 0) { throw std::runtime_error("分母不能为零"); } 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); } // 内部工具函数,用于化简分数 void Fraction::simplify() { int gcd = 1; int a = abs(up); int b = abs(down); while (b!= 0) { int temp = b; b = a % b; a = temp; } gcd = a; up /= gcd; up = (down < 0 && up!= 0)? -up : up; down /= gcd; down = abs(down); } // 友元函数实现:输出分数 void output(const Fraction& fraction) { if (fraction.up == 0) { std::cout << "0"; } else if (fraction.down == 1) { std::cout << fraction.up; } else { if (fraction.up < 0 && fraction.down < 0) { fraction.simplify(); std::cout << abs(fraction.up) << "/" << abs(fraction.down) << std::endl; } else if (fraction.down < 0) { fraction.simplify(); std::cout << -fraction.up << "/" << abs(fraction.down) << std::endl; } else { fraction.simplify(); std::cout << fraction.up << "/" << fraction.down << std::endl; } } } // 友元函数实现:分数相加 Fraction add(const Fraction& f1, const Fraction& f2) { int new_up = f1.up * f2.down + f2.up * f2.down; int new_down = f1.down * f2.down; return Fraction(new_up, new_down); } // 友元函数实现:分数相减 Fraction sub(const Fraction& f1, const Fraction& f2) { int new_up = f1.up * f2.down - f2.up * f1.down; int new_down = f1.down * f2.down; return Fraction(new_up, new_down); } // 友元函数实现:分数相乘 Fraction mul(const Fraction& f1, const Fraction& f2) { int new_up = f1.up * f2.up; int new_down = f1.down * f2.down; return Fraction(new_up, new_down); } // 友元函数实现:分数相除 Fraction div(const Fraction& f1, const Fraction& f2) { if (f2.up == 0) { throw std::runtime_error("除数不能为零"); } int new_up = f1.up * f2.down; int new_down = f1.down * f2.up; return Fraction(new_up, new_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(); }
实验任务五:
Account.h
#ifndef _ _ACCOUNT_H_ _ #define _ _ACCOUNT_H_ _ class SavingsAccount{ private: int id; double balance; double rate; int lastDate; double accumulation; static double total; void record(int date,double amount); double accumulate(int date)const{ return accumulation+balance*(date-lastDate); } public: SavingsAccount(int date,int id,double rate); int getId()const{return id;} double getBalance()const{return balance;} double getRate()const{return rate;} static double getTotal(){return total;} void deposit(int date,double amount); void withdraw(int date,double amount); void settle(int date); void show()const; }; #endif//_ _ACCOUNT_H_ _
Account.cpp
#include"account.h" #include<cmath> #include<iostream> using namespace std; double SavingAccount::total=0; SavingAccount::SavingAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ cout<<date<<"\t#"<<id<<" is created"<<endl; } void SavingAccount::record(int date,double amount){ accumulation=accumulate(date); lastDate=date; amount=floor(amount*100+0.5)/100; balance+=amount; total+=amount; cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; } void SavingAccount::deposit(int date,double amount){ record(date,amount); } void SavingAccount::withdraw(int data,double amount){ if(amount>getBalance()) cout<<"Error: not enough money"<<endl; else record(data,-amount); } void SavingAccount::settle(int date){ double interest=accumulate(date)*rate/365; if(interest!=0) record(date,interest); accumulation=0; } void SavingAccount::show() const{ cout<<"# "<<id<<"\tBalance: "<<balance; }
5_11.cpp
#include"account.h" #include<iostream> using namespace std; int main(){ //建立几个账户 SavingsAccount sa0(1,21325302,0.015); SavingsAccount sa1(1,58320212,0.015); //几笔账目 sa0.deposit(5,5000); sa1.deposit(25,10000); sa0.deposit(45,5500); sa1.withdraw(60,4000); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个账户信息 sa0.show();cout<<endl; sa1.show();cout<<endl; cout<<"Total:"<<SavingsAccount::getTotal()<<endl; return 0; }
标签:std,const,int,double,up,实验,Fraction From: https://www.cnblogs.com/cjchh222/p/18514481