实验二
实验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();
}

问题一:
报错如下
错误原因:只在类内部声明友元函数时,编译器无法找到函数声明,所以必须在类外部声明。
问题二:

普通构造函数
功能:创建T类的对象,初始化对象的属性m1和 m2。并且可以通过参数传递自定义初始值,若未提供则默认初始化的值为0。
调用时机:在创建T类对象时自动调用。
复制构造函数
功能:通过已有的T类对象来创建新对象,通常用于实现新的对象的复制。在复制时,会将传入对象t的m1和m2属性复制到新对象中。
调用时机:当用一个已有对象初始化另一个对象时,函数自动调用。
移动构造函数
功能:将已有对象的值移动给同类的一个新对象。通常会将原对象的属性值给新对象,同时将原对象的属性置为无效状态。
调用时机:当用一个临时对象初始化另一个对象时,自动调用。
析构函数
功能:用于在对象生命周期结束时释放对象占用的资源。
调用时机:当对象生命周期结束,或者对象超出作用域时自动调用。
问题三:
不能。因为静态成员变量的定义和初始化应该在类定义之外,而不是在类定义内部???
实验任务2
main.cpp
#include"A.h"
#include <iostream>
using std::cout;
using std::endl;
using std::boolalpha;
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();
}
A.cpp
#include"A.h"
#include<iostream>
/*在 C++ 中,如果你在 友元函数 中访问一个 static const 变量,你应该使用 const 关键字,而不是 static const。这是因为:
static 修饰符限定了变量的 链接性,使得该变量只能在声明它的源文件中访问,而不是在其他文件或作用域中。
*/
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::
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 a.real!=b.real||a.imag!=b.imag;
}
void output(const Complex &a)
{
if(a.imag==0)std::cout<<a.real;
// else if(a.real==0)std::cout<<a.imag<<'i';
else if(a.imag<0)std::cout<<a.real<<a.imag<<"i";
else std::cout<<a.real<<"+"<<a.imag<<"i";
}
double abs(const Complex &a)
{
return std::sqrt(a.real*a.real+a.imag*a.imag);
}
A.h
#ifndef A_H
#define A_H
#include<string>
#include<cmath>
class Complex{
//在C++中,类的成员(变量和函数)默认的访问控制是private,但是如果没有明确指定访问控制(public、private、protected),编译器会默认将其视为private。
double imag,real;
public:
//static const属性虽然也可以取址,但是对其写操作将会导致程序崩溃。因为static const 存在于常量区,而const存在于动态区。
//static const 和 const static 在 C/C++ 中是等价的
static const std::string doc;
Complex();
//为了检验,我故意设一个
Complex(double real);
Complex(double real,double imag);
//C++规定:临时变量不可以被修改。
//为什么这里一定要用const?如果不用const,则主函数中c=c1+d不能通过。
Complex(const Complex & other);
double get_real() const;
double get_imag() const;
void add(const Complex & other);//
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);
friend void output(const Complex & a);
friend double abs(const Complex & a);
};
#endif
//对于const 参考文章:
//https://timothy-liuxf.github.io/tm-blogs/blogs/zh-CN/c_cpp/why-const-is-essential-in-cpp.html

实验任务3
task3.cpp
#include <iostream>
#include <complex>
using std::cout;
using std::endl;
using std::boolalpha;
using std::complex;
void test() {
cout << "标准库模板类comple测试: " << endl;
complex<double> c1;
complex<double> c2(3, -4);
const complex<double> c3(3.5);//这个const 貌似可以不加
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;//把下面的1变成true
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();
}

1、标准库模板类有一个不一样的点,就是直接输出复数c,输出格式是(real,imag).很巧妙。
2、使用了complex
3、我还发现了:cout << boolalpha;//把下面的1变成true
实验任务4
task4.cpp
#include "B.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();
}
B.cpp
#include"B.h"
#include<numeric>
//#include<string>
const std::string Fraction::doc="Fraction类 v 0.01版\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
Fraction::Fraction(int a,int b):a(a),b(b){
if(b==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int to=a;
int fo=b;
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
a=to;b=fo;
}
Fraction::Fraction(const Fraction& c):a(c.a),b(c.b){}
int Fraction::get_up() const{return a;}
int Fraction::get_down()const{return b;}
Fraction Fraction::negative() const
{
int to=-a;
int fo=b;
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
return Fraction(to,fo);
}
// {return Fraction(-a,b);}
void output(const Fraction &c)
{
if(c.b==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int to=c.a;
int fo=c.b;
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
if(fo==1) {std::cout<<to;return;}
std::cout<<to<<"/"<<fo;
}
Fraction add(const Fraction &c1,const Fraction &c2)
{
int to,fo;
fo=c1.b*c2.b;
to=c1.a*c2.b+c2.a*c1.b;
if(fo==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
return Fraction(to,fo);
}
Fraction sub(const Fraction &c1,const Fraction &c2)
{
int to,fo;
fo=c1.b*c2.b;
to=c1.a*c2.b-c2.a*c1.b;
if(fo==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
return Fraction(to,fo);
}
Fraction mul(const Fraction &c1,const Fraction &c2)
{
int to,fo;
fo=c1.b*c2.b;
to=c1.a*c2.a;
if(fo==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
return Fraction(to,fo);
}
Fraction div(const Fraction &c1,const Fraction &c2)
{
int to,fo;
fo=c1.b*c2.a;
to=c1.a*c2.b;
if(fo==0)
{
throw std::invalid_argument("Denominator cannot be zero.");
}
int gd=std::gcd(abs(to),abs(fo));
to/=gd;
fo/=gd;
if(fo<0){to*=-1;fo*=-1;}
return Fraction(to,fo);
}
B.h
#ifndef B_H
#define B_H
#include<string>
#include<iostream>
class Fraction{
//int Numerator;//分子
//int denominator;//分母
public:
static const std::string doc;
// Fraction();
// Fraction(int a=0);
Fraction(int a=0,int b=1);
Fraction(const Fraction& c);
~Fraction(){};//
int get_up() const;
int get_down() const;
Fraction negative()const;
friend void output(const Fraction & c);
friend Fraction add(const Fraction & c1,const Fraction & c2);
friend Fraction sub(const Fraction & c1,const Fraction & c2);
friend Fraction mul(const Fraction & c1,const Fraction & c2);
friend Fraction div(const Fraction & c1,const Fraction & c2);
private:
int a,b;
};
#endif

实验任务5
A.cpp
#include "B.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;
}
B.cpp
#include "B.h"
#include <cmath>
#include <iostream>
using namespace std;
double SavingsAccount::total = 0;
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date, int id, double rate)
: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount::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 SavingsAccount::deposit(int date, double amount) {
record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
if (amount > getBalance())
cout << "Error: not enough money" << endl;
else
record(date, -amount);
}
void SavingsAccount::settle(int date) {
double interest = accumulate(date) * rate / 365; //计算年息
if (interest != 0)
record(date, interest);
accumulation = 0;
}
void SavingsAccount::show() const {
cout << "#" << id << "\tBalance: " << balance;
}
B.h
#ifndef __B_H__
#define __B_H__
class SavingsAccount { //储蓄账户类
private:
int id; //账号
double balance; //余额
double rate; //存款的年利率
int lastDate; //上次变更余额的时期
double accumulation; //余额按日累加之和
static double total; //所有账户的总金额
//记录一笔帐,date为日期,amount为金额,desc为说明
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); //取出现金
//结算利息,每年1月1日调用一次该函数
void settle(int date);
//显示账户信息
void show() const;
};
#endif //__ACCOUNT_H__

优点:1、增加const修饰成员函数,在一定程度上增加了账户的安全性。
2、增加了静态数据成员total 记录账户总金额,使得用户使用更便捷。
缺点:1、安全性不足,没有设计密码成员,导致查询余额等函数接口不安全。
2、在结息时操作数据精度不够。

浙公网安备 33010602011771号