Functional Programming in C++ 学习笔记FPpr8

晕了,因为面向对象编程是以后的必修课,现在想先学一下c++函数式编程,结果还是逃不了。幸好之前学过python面向对象,重温一下吧

//调用操作符重载
//之前虽然学过python面向对象,但是还是有必要学习c++的——为FP准备
#include <iostream>
#include <string>
using namespace std;
typedef void return_type ;
typedef void arguments;
//对之前代码的理解
int ask() { return 42; }//ask函数名,返回int类型变量42
typedef decltype (ask)* function_ptr;// function_ptr是指向ask返回数据类型的指针
class convertible_to_function_ptr {
public:
	operator function_ptr() const//我们将convertivle_to_function_ptr定义为了指向ask函数的int*类型
	{
		return ask;
	}
};//operator作类的转换函数
/*int main(int argc, char* argv[])
{
	auto ask_ptr = &ask;
	cout << ask_ptr() << endl;
	auto* ask_ref1 = ask;
	auto& ask_ref2 = ask;
	cout << ask_ref1() << ' ' << ask_ref2 << endl;//加括号直接调用子程序,不加括号可以显示子程序储存的地址
	convertible_to_function_ptr ask_wrapper;//指向ask函数的int*类型,这种数据类型有个新变量叫做ask_wrapper
	std::cout << ask_wrapper() << endl;
}*/

//重载调用操作符的语法和定义成员函数一样简单——operator()需要指明返回值类型和函数所需要的参数
//e.g.
class funciont_object
{
public:
	return_type operator()(arguments) const {
	}
};
//C++面向对象,注意和python的区别,不需要self
//为类定义,其实没有定义任何数据,只定义了类的名称意味着什么
//也就是定义了类的对象包括了什么,以及可以执行哪些操作
//命名规范:每个单词都以大写字母开头,不包含下划线。
class Person {
public:
	int gender;//1为男,0为女
	int age;
	//类的成员函数:那些把定义和原型写在类定义内部的函数
	//它可以操作类的任意对象,可以访问对象中的所有成员
	bool olderThan42(void) {
		return age > 42 ? true : false;
	}
	bool isMale(void);
//类的访问修饰符,一个类可以有多个 public、protected 或 private 标记区域,默认访问修饰符是 private
private://私有成员变量或函数在类的外部是不可访问的,和封装特性有关
	string personality;
public://一般在public定义访问的函数
	void enterPersonality()
	{
		cout << "Enter personality: ";
		cin >> personality;
	};
	void seePersonality() 
	{
		cout << personality << endl;
	}
};//protected可以在子类中访问,今晚继续。
//也可以在外部使用范围解析运算符::定义该函数,但是要在类中先声明
bool Person::isMale(void)
{
	return gender == 1 ? true : false;
}
//person1,person2就是Person类的对象,下面做的是声明对象
Person person1;
Person person2;
//二者都有数据成员——也就是person.age这些
int main()
{
	person1.gender = 1;
	person1.age = 43;
	person2.gender = 0;
	person2.age = 17;
	cout << person1.olderThan42()<<' '<< person2.isMale() << endl;
}

Learning C++ Functional Programming by Wisnu Anggoro English | 10 Aug. 2017 | ISBN: 1787281973 | ASIN: B06WVD7CVT | 304 Pages | AZW3 | 2.4 MB Key Features Modularize your applications and make them highly reusable and testable Get familiar with complex concepts such as metaprogramming, concurrency, and immutability A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases Book Description Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application's performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis. What you will learn Get to know the difference between imperative and functional approaches See the use of first-class functions and pure functions in a functional style Discover various techniques to apply immutable state to avoid side effects Design a recursive algorithm effectively Create faster programs using lazy evaluation Structure code using design patterns to make the design process easier Use concurrency techniques to develop responsive software Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization About the Author Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net. Table of Contents Diving into Modern C++ Manipulating functions in functional programming Applying immutable state to the function Recurring method invocation using recursive algorithm Procrastinating the execution process using Lazy Evaluation Optimizing code with Metaprogramming Running parallel execution using Concurrency Creating and debugging application in functional approach
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值