实验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.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉line36,重
新编译,是否能正确运行。如果能,回答说明可以去掉line36。如果不能,以截图形式给出编译报
错信息,分析可能的原因。
不能去掉t.h中的line36
编译报错信息:
原因:友元函数可以访问类的私有成员,但它依然是独立于类的函数。如果在类的实现中使用该函数,则需要为其提供普通函数的声明和定义。
问题二
t.h中,line9-12给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析构函数
的调用时机。
T(int x = 0, int y = 0);为普通构造函数。
功能: 用于初始化类的对象,可以带有默认参数。当创建对象时,如果没有提供任何参数,则使用默认值。
调用时机: 当创建对象的时候,如 T obj; 或 T obj(1, 2);。
T(const T &t);为复制构造函数。
功能: 用于通过另一个同类对象来初始化一个新对象。用于对象的拷贝操作,例如在函数传参时传递对象,或者直接用一个对象初始化另一个对象。
调用时机:
当用一个已存在对象初始化一个新对象时,例如 T obj2 = obj1;。
当将对象作为值传递给函数时,例如 void func(T obj);。
T(T &&t);移动构造函数。
功能: 用于通过移动另一个同类对象来初始化一个新对象,通常用来优化性能。
调用时机:
当用一个右值对象初始化一个新对象时,例如 T obj2 = std::move(obj1);。
当返回一个对象的临时对象时,例如 T createObject() { return T(); }。
~T();析构函数。
功能: 用于清理对象所占用的资源,释放动态分配的内存。
调用时机:
当对象的生命周期结束时,即当对象超出其作用域时,如 T obj; 在函数结束时。
当对象通过 delete 被显式删除时,如 T *obj = new T(); delete obj;。
当栈上的对象超出作用域时,或者容器中的对象被移除时。
问题三
t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。
在C++11中,不能在头文件中直接初始化static成员变量。可以将其声明在头文件中,并在源文件中进行定义和初始化。
任务二
Complex.h源码
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0);
Complex(const Complex &other);
double get_real() const;
double get_imag() const;
void add(const Complex &other) ;
friend bool is_equal(const Complex &other1 , const Complex &other2);
friend bool is_not_equal(const Complex &other1 , const Complex &other2);
friend Complex add(const Complex &other1 , const Complex &other2);
friend void output(const Complex &other);
friend double abs(const Complex &other);
static char doc[40];
};
bool is_equal(const Complex &other1 , const Complex &other2);
bool is_not_equal(const Complex &other1 , const Complex &other2);
Complex add(const Complex &other1 , const Complex &other2);
void output(const Complex &other);
double abs(const Complex &other);
#endif
Complex.cpp源码
#include "Complex.h"
#include <cmath>
Complex::Complex(double r, double i) : real(r), imag(i) {}
double Complex::get_real() const {
return real;
}
double Complex::get_imag() const {
return imag;
}
Complex::Complex(const Complex &other){
real = other.get_real();
imag = other.get_imag();
}
void Complex::add(const Complex& other){
real += other.real;
imag += other.imag;
return ;
}
double abs(const Complex& other){
double sum;
sum = other.imag * other.imag + other.real * other.real;
double ave = sqrt(sum);
return ave;
}
bool is_equal(const Complex& other1 , const Complex& other2){
if(other1.get_imag() == other2.get_imag() && other1.get_real() == other2.get_real()){
return true;
}
else
return false;
}
bool is_not_equal(const Complex& other1 , const Complex& other2){
if(other1.get_imag() == other2.get_imag() && other1.get_real() == other2.get_real()){
return false;
}
else
return true;
}
Complex add(const Complex& other1 , const Complex& other2){
return Complex(other1.get_real() + other2.get_real() , other1.get_imag() + other2.get_imag());
}
void output(const Complex& other){
std::cout << other.get_real();
double imagy;
if(other.get_imag() < 0){
std::cout << " - ";
imagy = (-1.0) * other.get_imag();
}
else{
std::cout << " + ";
imagy = other.get_imag();
}
std::cout << imagy << 'i';
return;
}
char Complex::doc[40] = {"a simplified Complex class"};
task2.cpp
#include "Complex.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();
}
运行测试结果截图
实验三
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);
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;
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();
}
运行测试结果截图
实验四
Fraction.h源码
#pragma once
#include<string>
class Fraction {
public:
static const std::string doc;
Fraction(int x=1, int y = 1);
Fraction(const Fraction& p);
int get_up();
int get_down();
Fraction negative();
public:
friend void output(Fraction p);
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, down;
};
void output( Fraction p);
int computeGCD(int a, int b);
Fraction.cpp源码
#include"Fraction.h"
#include<iostream>
#include<string>
const std::string Fraction::doc = "Fraction类v0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
Fraction::Fraction(int x,int y):up{x},down{y}{}
Fraction::Fraction(const Fraction &p):up{p.up},down{p.down}{}
int Fraction::get_up(){
int gcd = computeGCD(up, down);
up /= gcd;
down /= gcd;
return up;
}
int Fraction::get_down(){
int gcd = computeGCD(up,down);
up /= gcd;
down /= gcd;
return down;
}
Fraction Fraction::negative() {
return Fraction(-up, down);
}
void output(Fraction p) {
int gcd = computeGCD(p.up, p.down);
p.up /= gcd;
p.down /= gcd;
if (p.down == 1) {
std::cout << p.up;
}
else if ( p.down > 0&&p.down!=1) {
std::cout << p.up << "/" << p.down;
}
else if (p.down == 0) {
std::cout << "分母不能为0";
}
else std::cout << -p.up << "/" << -p.down;
}
Fraction add(const Fraction& a, const Fraction& b) {
Fraction c;
c.up = a.up*b.down+ b.up*a.down;
c.down = a.down * b.down;
return c;
}
Fraction sub(const Fraction& a, const Fraction& b) {
Fraction c;
c.down = a.down * b.down;
c.up = a.up * b.down - a.down * b.up;
int gcd = computeGCD(c.up, c.down);
c.up /= gcd;
c.down /= gcd;
return c;
}
Fraction mul(const Fraction& a, const Fraction& b) {
Fraction c;
c.down = a.down * b.down;
c.up = a.up * b.up;
int gcd = computeGCD(c.up, c.down);
c.up /= gcd;
c.down /= gcd;
return c;
}
Fraction div(const Fraction& a, const Fraction& b) {
Fraction c;
if (b.up != 0 && b.down != 0) {
c.down = a.down * b.up;
c.up = a.up * b.down;
int gcd = computeGCD(c.up, c.down);
c.up /= gcd;
c.down /= gcd;
return c;
}
else {
return Fraction(1, 0);
}
}
int computeGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a < 0 ? -a : a;
}
task4.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 SavingAccount{
private:
int id;
double balance;
double rate;
double lastData;
double accumulation;
static double total;
void record(int data,double amount);
double accumulate(int data) const
{
return accumulation+balance*(data-lastData);
}
public:
SavingAccount(int data,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 data,double amount);
void withdraw(int data,double amount);
void settle(int data);
void show() const;
};
#endif
account.cpp源码
#include <bits/stdc++.h>
#include "account.h"
using namespace std;
double SavingAccount::total=0;
SavingAccount::SavingAccount(int data,int id,double rate)
:id(id),balance(0),rate(rate),lastData(data),accumulation(0)
{
cout<<data<<"\t#"<<id<<"is created"<<endl;
}
void SavingAccount::record(int data,double amount)
{
accumulation=accumulate(data);
lastData=data;
amount=floor(amount*100+0.5)/100;
balance+=amount;
total+=amount;
cout<<data<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void SavingAccount::deposit(int data,double amount)
{
record(data,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 data)
{
double interest=accumulate(data)*rate/365;
if (interest!=0)
record(data,interest);
accumulation=0;
}
void SavingAccount::show() const
{
cout<<"#"<<id<<"\tBalance:"<<balance;
}
5_11.cpp源码
#include "account.h"
#include <iostream>
using namespace std;
int main ()
{
SavingAccount sa0(1,21325302,0.015);
SavingAccount sa1(1,58320212,0.015);
sa0.deposit(5,5000);
sa1.deposit(25,10000);
sa0.deposit(45,5500);
sa1.deposit(60,4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();cout<<endl;
sa1.show();cout<<endl;
cout<<"Total:"<<SavingAccount::getTotal()<<endl;
return 0;
}