Task1:
//t.h
pragma once
include
// 类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;
private:
int m1, m2;
// 类属性、方法
public:
static int get_cnt();
public:
static const std::string doc;
static const int max_cnt;
private:
static int cnt;
// 类T友元函数声明
friend void func();
};
// 普通函数声明
void func();
const std::string T::doc{"a simple class sample"};
const int T::max_cnt = 999;
int T::cnt = 0;
//t.cpp
// 类T: 实现
// 普通函数实现
include "t.h"
include
include
using std::cout;
using std::endl;
using std::string;
// static成员数据类外初始化
// 对象方法
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;
}
//test
include "t.h"
include
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();
}
Q1、能正确运行
Q2、默认构造函数:在没有提供任何参数的情况下创建对象时调用的构造函数
拷贝构造函数:使用另一个对象来初始化新对象时,将对象作为参数传递给函数时,从函数返回对象时
移动构造函数:从临时对象(右值)转移资源
委托构造函数:一个构造函数调用同一个类的另一个构造函数
析构函数:在对象生命周期结束时调用,用于释放对象占用的资源;在对象离开其作用域时,显式删除动态分配的对象时,程序结束时调用
Task2:
//Complex.h
pragma once
ifndef COMPLEX_H
define COMPLEX_H
include
using std::string;
class Complex{
public:
//Complex ();
//Complex (double newreal);
Complex (double newreal=0,double newimag=0);//
Complex (const Complex &p);
//~Complex();
friend Complex add(const Complex &c1,const Complex &c2);
friend bool is_equal(const Complex &c1,const Complex &c2);
friend bool is_not_equal(const Complex &c1,const Complex &c2);
double get_real() const{return real;}
double get_imag() const{return imag;}
friend void output(const Complex &c);
friend double abs(const Complex &c);
void add(const Complex &p)
{
this->real+=p.real;
this->imag+=p.imag;
}
private:
double real;
double imag;
public:
static const string doc;
};
/*class func{
public:
Complex add(const Complex &c1,const Complex &c2);
bool is_equal(const Complex &c1,const Complex &c2);
bool is_not_equal(const Complex &c1,const Complex &c2);
void output(const Complex &c);
double abs(const Complex &c);
private:
Complex C1,C2,C3;
};*/
endif
include
include
include
include"Complex.h"
using std::cout;
using std::endl;
using std::string;
//要在源文件中初始化,头文件的初始化会重复赋值
const std::string Complex::doc{"a simple class sample"};
Complex c1,c2,c3,c;
Complex::Complex (double newreal,double newimag):real{newreal},imag{newimag}{}
//Complex::Complex()real{p.real},imag{p.imag}{}错误!这是拷贝构造函数
Complex::Complex(const Complex &p) {
// 假设Complex有两个成员变量:real和imag
this->real = p.real; // 复制实部
this->imag = p.imag; // 复制虚部
}
/*double Complex::get_real() const
{
return real;
}
double Complex::get_imag() const
{
return imag;
}
void Complex::add(const Complex &p)
{
this->real+=p.real;
this->imag+=p.imag;
}*/
Complex add(const Complex &c1,const Complex &c2)//
{
double newreal=c1.real+c2.real;
double newimag=c1.imag+c2.imag;
return Complex(newreal,newimag);
}
bool is_equal(const Complex &c1,const Complex &c2)
{
if(c1.real==c2.real&c1.imag==c2.imag)
return true;
else return false;
}
bool is_not_equal(const Complex &c1,const Complex &c2)
{
if(c1.real!=c2.real||c1.imag!=c2.imag)
return true;
else return false;
}
void output(const Complex &c)
{
if(c.imag>=0)
cout<<c.real<<'+'<<c.imag<<'i'<<endl;
else
cout<<c.real<<c.imag<<'i'<<endl;
}
double abs(const Complex &c)
{
return sqrt(c.realc.real+c.imagc.imag);
}
//test
// 待补足(多文件组织代码时,需要包含的头文件)
include
include
include"Complex.h"
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:
include
include
using std::cout;
using std::endl;
using std::boolalpha;
using std::complex;
void test() {
cout << "标准库模板类comple测试: " << endl;
complex
complex
const complex
complex
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();
}
Task4:
//Fraction.h
pragma once
include
include
using std::string;
class Fraction
{
public:
static const string doc;
//Fraction();
Fraction(int u=1,int d=1);
//~Fraction();
int get_up()const{return up;
}
int get_down()const{return down;
}
Fraction negative()const;
friend void output(const Fraction &f);
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 output(const Fraction &f);
Fraction add(const Fraction &f1,const Fraction &f2);
Fraction sub(const Fraction &f1,const Fraction &f2);
Fraction mul(const Fraction &f1,const Fraction &f2);
Fraction div(const Fraction &f1,const Fraction &f2);
include"Fraction.h"
include
include
include
using std::cout;
using std::endl;
using std::string;
const std::string Fraction::doc{"a simple class sample"};
Fraction f,f1,f2,f3;//如果在f2的声明中不小心加上了括号(即使没有参数),编译器可能会将其误解为函数调用
Fraction::Fraction(int u,int d):up{u},down{d}{ //声明中初始化过了,定义时需删去初始化内容,不然会被认为重复赋值
}
/Fraction::int get_up(){应把int放在首位
return up;
}
Fraction::int get_down(){
return down;
}/
Fraction Fraction::negative()const{ //返回值为Fraction类对象的函数 ,注意Fraction::的位置!!
int new_up=-this->up;
int new_down=this->down;
return Fraction(new_up,new_down);
}
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void output(const Fraction &f)
{
if(f.up0)
cout<<0<<endl;
else if(f.down1)
cout<<f.up<<endl;
else if(f.down0)
cout<<"分母不能为0"<<endl;
else
{
int sign=(f.up<0)(f.down<0)?-1:1;//是异或运算符,如何优化??
int divisor=gcd(f.up,f.down);
int newup=f.up/divisor;
int newdown=f.down/divisor;
if(sign-1)
cout<<'-'<<abs(newup)<<"/"<<abs(newdown)<<endl;
else if(sign==1)
cout<<abs(newup)<<"/"<<abs(newdown)<<endl;
}
}
Fraction add(const Fraction &f1,const Fraction &f2){
int new_down=f1.down+f2.down;
int new_up=f1.upf2.down+f2.upf1.down;
return Fraction(new_up,new_down);
}
Fraction sub(const Fraction &f1,const Fraction &f2){
int new_down=f1.down+f2.down;
int new_up=f1.upf2.down-f2.upf1.down;
return Fraction(new_up,new_down);
}
Fraction mul(const Fraction &f1,const Fraction &f2){
int new_down=f1.downf2.down;
int new_up=f1.upf2.up;
return Fraction(new_up,new_down);
}
Fraction div(const Fraction &f1,const Fraction &f2){
int new_up=f1.upf2.down;
int new_down=f1.downf2.up;
return Fraction(new_up,new_down);
}
include "Fraction.h"
include
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();
}
Task5:
//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
include
include
include"account.h"
using namespace std;
double SavingsAccount::total=0;
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(amount100+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;}
//test
include
include"account.h"
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);
sa0.settle(90);
sa1.settle(90);
sa0.show();cout<<endl;
sa1.show();cout<<endl;
cout<<"Total:"<<SavingsAccount::getTotal()<<endl;
return 0;
}
标签:const,cout,int,double,Complex,实验,Fraction From: https://www.cnblogs.com/karis/p/18504197