实验2 类和对象_基础编程1
实验任务一
源码
1 #pragma once 2 3 #include<string> 4 5 class T { 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)形式显示对象信息 13 14 private: 15 int m1, m2; 16 17 public: 18 static int get_cnt();//显示对象总数 19 20 public: 21 static const std::string doc;//描述信息 22 static const int max_cnt;//对象上限 23 24 private: 25 static int cnt;//对象数目 26 friend void func(); 27 }; 28 29 void func();
1 #include "t.h" 2 #include <iostream> 3 #include <string> 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 // static成员数据类外初始化 10 const std::string T::doc{ "a simple class sample" }; 11 const int T::max_cnt = 999; 12 int T::cnt = 0; 13 14 15 // 对象方法 16 T::T(int x, int y) : m1{ x }, m2{ y } { 17 ++cnt; 18 cout << "T constructor called.\n"; 19 } 20 21 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } { 22 ++cnt; 23 cout << "T copy constructor called.\n"; 24 } 25 26 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } { 27 ++cnt; 28 cout << "T move constructor called.\n"; 29 } 30 31 T::~T() { 32 --cnt; 33 cout << "T destructor called.\n"; 34 } 35 36 void T::adjust(int ratio) { 37 m1 *= ratio; 38 m2 *= ratio; 39 } 40 41 void T::display() const { 42 cout << "(" << m1 << ", " << m2 << ")"; 43 } 44 45 // 类方法 46 int T::get_cnt() { 47 return cnt; 48 } 49 50 // 友元 51 void func() { 52 T t5(42); 53 t5.m2 = 2049; 54 cout << "t5 = "; t5.display(); cout << endl; 55 }
1 #include "t.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test(); 8 9 int main() { 10 test(); 11 cout << "\nmain: \n"; 12 cout << "T objects'current count: " << T::get_cnt() << endl; 13 } 14 15 void test() { 16 cout << "test class T: \n"; 17 cout << "T info: " << T::doc << endl; 18 cout << "T objects'max count: " << T::max_cnt << endl; 19 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 20 21 22 T t1; 23 cout << "t1 = "; t1.display(); cout << endl; 24 25 T t2(3, 4); 26 cout << "t2 = "; t2.display(); cout << endl; 27 28 T t3(t2); 29 t3.adjust(2); 30 cout << "t3 = "; t3.display(); cout << endl; 31 32 T t4(std::move(t2)); 33 cout << "t4 = "; t4.display(); cout << endl; 34 35 cout << "T objects'current count: " << T::get_cnt() << endl; 36 37 func(); 38 }
运行结果
问题一:
如果func()
没有被调用,并且没有其他地方依赖于它的实现,那么从项目中移除func()
的定义或调用通常不会影响编译和运行。然而,如果func()
的声明(作为友元)被保留,但实现被移除,并且其他代码(如测试代码或未来的开发)试图调用func()
,那么这将导致链接错误,因为找不到func()
的实现。
问题二:
1.T(int x = 0, int y = 0);为带有默认参数的默认构造函数。
功能:当创建对象时没有提供任何初始化参数时,使用此构造函数。它允许对象以某种默认状态被创建。
调用时机:在创建对象时,如果没有提供足够的参数来匹配其他构造函数,则调用默认构造函数。
2.T(const T& t);为拷贝构造函数。
功能:用于创建一个新对象,作为另一个同类型对象的副本。它接受一个对同类型对象的常量引用作为参数。
调用时机:当使用另一个同类型对象初始化新对象时(如T obj2 = obj1;
);
当对象作为函数参数按值传递时;
当对象从函数按值返回时。
3.T(T&& t);为移动构造函数。
功能:从t中窃取资源并初始化新对象,为避免不必要的资源复制
调用时机:当使用std::move
显式地将对象转换为右值,并用于初始化新对象时。
4.~T();为析构函数。
功能:析构函数在对象生命周期结束时自动调用,用于执行清理操作,如释放对象占用的资源(如动态分配的内存、文件句柄等)。
调用时机:当对象的生命周期结束时,如离开作用域(对于局部变量)、被删除(对于动态分配的对象)、或容器(如std::vector
)被销毁且包含该类型的对象时。
问题三:
不行
实验任务二
源码
1 #pragma once 2 3 #include<string> 4 5 class Complex { 6 public: 7 static const std::string doc; 8 private: 9 double real; double imag; 10 public: 11 Complex(double r = 0, double i = 0); 12 Complex(const Complex& c); 13 14 double get_real()const; 15 double get_imag()const; 16 17 void add(const Complex& other); 18 19 friend Complex add(const Complex& c1, const Complex& c2); 20 friend bool is_equal(const Complex& c1, const Complex& c2); 21 friend bool is_not_equal(const Complex& c1, const Complex& c2); 22 friend double abs(const Complex& c); 23 friend void output(const Complex& c); 24 };
1 #include"Complex.h" 2 #include<iostream> 3 #include<string> 4 5 using std::cout; 6 using std::endl; 7 using std::cin; 8 using std::string; 9 10 const std::string Complex::doc{ "a simplified complex class" }; 11 12 Complex::Complex(double r,double i):real{r},imag{i}{} 13 Complex::Complex(const Complex& comp) :real{ comp.real }, imag{ comp.imag } {}; 14 15 double Complex::get_real()const { return real; } 16 double Complex::get_imag()const { return imag; } 17 18 void Complex::add(const Complex& other) { 19 real += other.real; 20 imag += other.imag; 21 } 22 23 Complex add(const Complex& c1, const Complex& c2) { 24 return Complex(c1.real + c2.real, c1.imag + c2.imag); 25 } 26 27 bool is_equal(const Complex& c1, const Complex& c2) { 28 if (c1.real == c2.real) 29 if (c1.imag == c2.imag) 30 return true; 31 return false; 32 } 33 34 bool is_not_equal(const Complex& c1, const Complex& c2) { 35 if (c1.real == c2.real) 36 if (c1.imag == c2.imag) 37 return false; 38 return true; 39 } 40 41 double abs(const Complex& c) { 42 return sqrt(c.real * c.real + c.imag * c.imag); 43 } 44 45 void output(const Complex& c) { 46 cout << c.real << "+" << c.imag; 47 }
1 #include "Complex.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 8 void test() { 9 cout << "类成员测试: " << endl; 10 cout << Complex::doc << endl; 11 12 cout << endl; 13 14 cout << "Complex对象测试: " << endl; 15 Complex c1; 16 Complex c2(3, -4); 17 const Complex c3(3.5); 18 Complex c4(c3); 19 20 cout << "c1 = "; output(c1); cout << endl; 21 cout << "c2 = "; output(c2); cout << endl; 22 cout << "c3 = "; output(c3); cout << endl; 23 cout << "c4 = "; output(c4); cout << endl; 24 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 25 26 cout << endl; 27 28 cout << "复数运算测试: " << endl; 29 cout << "abs(c2) = " << abs(c2) << endl; 30 c1.add(c2); 31 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 32 cout << boolalpha; 33 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 34 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 35 c4 = add(c2, c3); 36 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 37 } 38 39 int main() { 40 test(); 41 }
运行结果
实验任务三
源码
1 #include<iostream> 2 #include<complex> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 using std::complex; 8 9 void test() { 10 cout << "标准库模板类complex测试:" << endl; 11 complex<double>c1; 12 complex<double>c2(3, -4); 13 const complex<double>c3(3.5); 14 complex<double>c4(c3); 15 16 cout << "c1 = " << c1 << endl; 17 cout << "c2 = " << c2 << endl; 18 cout << "c3 = " << c3 << endl; 19 cout << "c4 = " << c4 << endl; 20 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() <<endl; 21 cout << endl; 22 23 cout << "复数运算测试:" << endl; 24 cout << "abs(c2) = "<<abs(c2) << endl; 25 c1 += c2; 26 cout << "c1 +=c2, c1 = " << c1 << endl; 27 cout << boolalpha; 28 cout << "c1 == c2 : " << (c1 == c2) << endl; 29 cout << "c1 != c3 : " << (c1 != c3) << endl; 30 c4 = c2 + c3; 31 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 32 } 33 34 int main() { 35 test(); 36 }
运行结果
思考:
1.提供了:构造函数(能自定义对象类型),取模,比较
2.可以提供接口自定义对象类型
实验任务四
源码
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<algorithm> 5 6 class Fraction { 7 private: 8 int up; 9 int down; 10 public: 11 static const std::string doc; 12 13 Fraction(int u = 0, int d = 1); 14 Fraction(const Fraction& other); 15 16 int get_up() const; 17 int get_down() const; 18 19 Fraction negative() const; 20 21 friend void simplify(Fraction&f); 22 23 friend void output(const Fraction& f); 24 friend Fraction add (const Fraction& f1, const Fraction& f2); 25 friend Fraction sub(const Fraction& f1, const Fraction& f2); 26 friend Fraction mul(const Fraction& f1, const Fraction& f2); 27 friend Fraction div(const Fraction& f1, const Fraction& f2); 28 29 };
1 #include"Fraction.h" 2 #include<iostream> 3 4 5 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 6 7 Fraction::Fraction(int u, int d) :up(u), down(d) { simplify(*this); }; 8 //用this指针 9 Fraction::Fraction(const Fraction& other) :up(other.up), down(other.down) {}; 10 11 int Fraction::get_up() const{ 12 return up; 13 } 14 int Fraction::get_down() const{ 15 return down; 16 } 17 18 //求负分数 19 Fraction Fraction:: negative() const{ 20 return Fraction(-up, -down); 21 } 22 23 void simplify(Fraction& f) { 24 //化简,先求最大公约数 25 int a = f.up, b = f.down; 26 int temp; 27 while (b != 0) { 28 temp = b; 29 b = a % b; 30 a = temp; 31 } 32 int gcd = a;//最大公约数 33 f.up /= gcd; 34 f.down /= gcd; 35 36 //确保分母为正 37 if (f.down < 0) { 38 f.up = -f.up; 39 f.down = -f.down; 40 } 41 } 42 43 //输出分数 44 void output(const Fraction& f) { 45 if (f.down == 0)return; 46 if (f.down == 1) { 47 std::cout << f.up << std::endl; 48 return; 49 } 50 std::cout << (f.up<0?"-" :"") <<std::abs(f.up)<< "/" << f.down << std::endl; 51 } 52 53 //分数加法 54 Fraction add(const Fraction& f1, const Fraction& f2) { 55 Fraction result; 56 result.up = f1.up * f2.down + f2.up * f1.down; 57 result.down = f1.down * f2.down; 58 simplify(result); 59 return result; 60 } 61 62 //分数减法 63 Fraction sub(const Fraction& f1, const Fraction& f2) { 64 Fraction result(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down); 65 simplify(result); 66 return result; 67 } 68 69 //分数乘法 70 Fraction mul(const Fraction& f1, const Fraction& f2) { 71 Fraction result(f1.up * f2.up, f1.down * f2.down); 72 simplify(result); 73 return result; 74 } 75 76 //分数除法 77 Fraction div(const Fraction& f1, const Fraction& f2) { 78 if (f2.up == 0) { 79 std::cout<<"分母不能为0"<<std::endl; 80 return Fraction(0,0); 81 } 82 Fraction result; 83 result.up = f1.up * f2.down; // 分子相乘(f1的分子乘以f2的分母) 84 result.down = f1.down * f2.up; // 分母相乘(f1的分母乘以f2的“分子”,但f2的分子在这里视为其倒数的分母部分) 85 simplify(result); // 化简 86 return result; 87 }
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 8 void test1() { 9 cout << "Fraction类测试: " << endl; 10 cout << Fraction::doc << endl << endl; 11 12 Fraction f1(5); 13 Fraction f2(3, -4), f3(-18, 12); 14 Fraction f4(f3); 15 cout << "f1 = "; output(f1); cout << endl; 16 cout << "f2 = "; output(f2); cout << endl; 17 cout << "f3 = "; output(f3); cout << endl; 18 cout << "f4 = "; output(f4); cout << endl; 19 20 Fraction f5(f4.negative()); 21 cout << "f5 = "; output(f5); cout << endl; 22 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 23 24 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 25 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 26 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 27 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 28 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 29 } 30 31 void test2() { 32 Fraction f6(42, 55), f7(0, 3); 33 cout << "f6 = "; output(f6); cout << endl; 34 cout << "f7 = "; output(f7); cout << endl; 35 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 36 } 37 38 int main() { 39 cout << "测试1: Fraction类基础功能测试\n"; 40 test1(); 41 42 cout << "\n测试2: 分母为0测试: \n"; 43 test2(); 44 }
运行结果
实验任务五
源码
1 #pragma once 2 class SavingsAccount { 3 private: 4 int id; 5 double balance; 6 double rate; 7 int lastDate; 8 double accumulation; 9 static double total; 10 void record(int date, double amount); 11 double accumulate(int date) const { 12 return accumulation + balance * (date - lastDate); 13 } 14 public: 15 SavingsAccount(int date, int id, double rate); 16 int getId() const { return id; } 17 double getBalance() const { return balance; } 18 double getRate() const { return rate; } 19 static double getTotal() { return total; } 20 void deposit(int date, double amount); 21 void withdraw(int date, double amount); 22 void settle(int date); 23 void show() const; 24 };
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 8 cout << date << "\t#" << id << " is created" << endl; 9 } 10 void SavingsAccount::record(int date, double amount) { 11 accumulation = accumulate(date); 12 lastDate = date; 13 amount = floor(amount * 100 + 0.5) / 100; 14 balance += amount; 15 total += amount; 16 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 17 } 18 19 void SavingsAccount::deposit(int date, double amount) { 20 record(date, amount); 21 } 22 23 24 void SavingsAccount::withdraw(int date, double amount) { 25 if (amount > getBalance()) 26 cout << "Error:not enough money" << endl; 27 else 28 record(date, -amount); 29 } 30 void SavingsAccount::settle(int date) { 31 double interest = accumulate(date) * rate / 365; 32 if (interest != 0) 33 record(date, interest); 34 accumulation = 0; 35 } 36 void SavingsAccount::show() const { 37 cout << "#" << id << "\tBalance:" << balance; 38 }
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() 5 { 6 SavingsAccount sa0(1, 21325302, 0.015); 7 SavingsAccount sa1(1, 58320212, 0.015); 8 9 sa0.deposit(5, 5000); 10 sa1.deposit(25, 10000); 11 sa0.deposit(45, 5500); 12 sa1.withdraw(60, 4000); 13 14 sa0.settle(90); 15 sa1.settle(90); 16 17 sa0.show(); cout << endl; 18 sa1.show(); cout << endl; 19 cout << "Total:" << SavingsAccount::getTotal() << endl; 20 return 0; 21 }
运行结果