【C++】VS2019类库创建和几个运算符重载示例

写几个类,一个是MyTime,表示以秒为基本单位的时间量。实现:

  • 插入运算符<<的重载,通过cout直接输出时间量,格式为“XX时XX分XX秒”;
  • 提取运算符>>的重载,实现通过cin直接输入时间量;
  • 自增运算符++的重载,前缀和后缀形式都要有,以秒为单位增加;
  • 加法运算符+的重载,计算两个时间量的和;
  • 实现时间量和整数(单位秒)的直接加法+运算。

顺便说一下,有时候听一些老师讲课毫无用处,一些书也是,浪费时间。不听/看这些无意义的课/书,是对生命最好的珍惜。学C++,直接看C++ Primer 5th,看一遍,解决你90%的困惑,学到现代C++的最佳实践。用别的方式学到的C++,就很遗憾了…我现在就在后悔,没有在早期就学C++,学C++的时候看了一些破书,听了一些?课。

实现如下:(至于为甚麽这么写,看C++ Primer14章)
MyTime.h:

#pragma once
#include <iostream>
#include <sstream>
#include <string> 
using namespace std;

class MyTime {
private:
	int seconds;
public:
	MyTime(int s = 0, int m = 0, int h = 0); 
	string toString() const;

	friend istream& operator>> (istream& is, MyTime& m);
	friend ostream& operator<<(ostream& os, const MyTime& m);
	MyTime& operator++();    // 前置
	MyTime operator++(int);  // 后置
	friend MyTime operator+(const MyTime &a, const MyTime &b);
	friend MyTime operator+(const MyTime& a, int b); 
};

MyTime.cpp:

#include "MyTime.h"

MyTime::MyTime(int s, int m, int h) {
	seconds = s + m * 60 + h * 3600;
}

istream& operator>> (istream& is, MyTime& m) { 
	int temp;
	is >> temp;
	if (is && temp >= 0) m.seconds = temp;  // 检查输入是否成功;并且秒参数有意义
	else m.seconds = 0;  // 输入失败或不合法,赋值为默认的0
	return is;
}

string MyTime::toString() const {
	int h = seconds / 3600;
	int m = seconds % 3600 / 60;
	int s = seconds % 60;
	stringstream ss;
	ss << h << "时" << m << "分" << s << "秒";
	string my_s;
	ss >> my_s;
	return my_s;
}

ostream& operator<< (ostream& os, const MyTime& m) {
	os << m.toString();
	return os;
}

MyTime& MyTime::operator++() { // 前置++
	++seconds;
	return *this;
}

MyTime MyTime::operator++(int) { // 后置++
	MyTime t = *this;
	++* this;
	return t;
}

MyTime operator+(const MyTime& a, const MyTime& b) {
	return MyTime(a.seconds + b.seconds);
}

MyTime operator+(const MyTime& a, int b) {
	return MyTime(a.seconds + b);
}

test.cpp:

#include "MyTime.h"
#include <iostream>
using namespace std;

int main() {
	MyTime a(600), b; 
	cout << a << endl;
	cout << "input a integer time value(second): " << endl;
	cin >> b; 
	cout << ++b << endl; 
	cout << b++ << endl;
	cout << a + b << endl;
	return 0;
}

在这里插入图片描述

第二个类是Complex,复数类。像上面一样,实现如下操作:

  • 插入运算符<<的重载,通过cout直接输出复数,格式为“a+bi”;
  • 提取运算符>>的重载,实现通过cin直接输入复数,先实部,再虚部;
  • 加法运算符+的重载,计算两个复数的和;
  • 复数和浮点数的直接加法运算;

Complex.h:

#pragma once
#include <sstream>
#include <iostream>
using namespace std;

class Complex {
private:
	double real;
	double imag;
public:
	Complex(double r = 0, double i = 0) : real(r), imag(i) {} 
	string toString() const;

	friend istream& operator>> (istream& is, Complex& c);
	friend ostream& operator<< (ostream& os, const Complex& c);
	friend Complex operator+(const Complex& a, const Complex& b);
	friend Complex operator+(const Complex& a, double b); // 复数和浮点数的直接加法
};

Complex.cpp:

#include "Complex.h"

Complex operator+(const Complex& a, const Complex& b) {
	return Complex(a.real + b.real, a.imag + b.imag);
}  
Complex operator+(const Complex& a, double b) { // 复数和浮点数的直接加法
	return Complex(a.real + b, a.imag);
}

istream& operator>> (istream& is, Complex& c) {
	is >> c.real >> c.imag;
	return is;
}
ostream& operator<< (ostream& os, const Complex& c) {
	os << c.toString();
	return os;
}

string Complex::toString() const
{ 
	stringstream ss;
	ss << real << "+" << imag << "i";
	string my_s;
	ss >> my_s;
	return my_s;
}

接下来就是创建属于我们自己的类库的时候了。

在VS2019中创建一个静态库工程(Win32 Static Library)。
在这里插入图片描述
把Complex和MyTime的.h 和.cpp文件添加到该工程,编译、链接、生成后得到MyUtility.lib库文件。该工具库的发布文件包括2个头文件和1个库文件。(这里我遇到了在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "pch"错误,直接把预编译头去掉就可以了)。
在这里插入图片描述

创建一个测试工程TestMyUtility ,将MyUtility.lib和MyTime.h、Complex.h放进文件夹中,#include "MyTime.h"#include "Complex.h",引入静态库使用#pragma comment(lib, "MyUtility.lib")。然后写几个测试语句测试自己的静态库。

#include <iostream>
#include "MyTime.h"
#include "Complex.h"
#pragma comment(lib, "MyUtility.lib")

using namespace std;

int main() {
	MyTime a(600), b; 
	cout << a << endl;
	cout << "input a integer time value(second): " << endl;
	cin >> b; 
	cout << ++b << endl; 
	cout << b++ << endl;
	cout << a + b << endl;

	Complex t, x(3, 4);
	cin >> t;
	cout << t + x << endl;
	cout << t + 5.3 << endl;
	return 0;
}

拥有自己的工具库是一件很有意思的事情,从现在开始积攒吧!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值