C++到Objective-C的转型指南

需积分: 10 13 下载量 42 浏览量 更新于2024-07-27 收藏 1.12MB PDF 举报
"《From C++ to Objective-C》是一本帮助C++程序员过渡到Objective-C的书籍,由Pierre Chatelier编写, Aaron Vegh负责英文改编。本书详细介绍了Objective-C语言的关键特性和Cocoa框架,提供了对Objective-C语法的概述,并讨论了其历史背景。书中还包含了对Objective-C中关键字、注释、代码与声明混合、新类型和值的解释,如BOOL、YES、NO、nil、Nil、id等。此外,还提到了类名前缀NS的由来以及函数与方法的区别。" 在编程领域,Objective-C是一种面向对象的编程语言,特别用于Apple的iOS和macOS操作系统。它是C语言的超集,因此对于已经熟悉C++的开发者来说,过渡到Objective-C相对较为平滑。《From C++ to Objective-C》这本书正是针对这样的转型需求,提供了详细的学习路径。 首先,Objective-C和Cocoa是苹果开发平台上的核心技术栈。Objective-C扩展了C语言,引入了消息传递机制和类的概念,而Cocoa则是一个强大的框架集合,用于构建iOS和macOS应用。理解这两者的关系和交互是成为有效Objective-C开发者的基石。 书中的"Objective-C和Cocoa"章节会引导读者了解Objective-C的基础,包括它的历史发展,这有助于理解语言的设计哲学。"A short history of Objective-C"部分将介绍Objective-C是如何从Smalltalk和其他语言中汲取灵感,逐渐演变为今天的样子。 "Syntax overview"部分详细解析了Objective-C的语法特性,如关键词的使用,如何添加注释,以及如何在代码中混合声明和实现。Objective-C允许在同一个文件中声明和定义类,这是与C++的一个显著不同之处。此外,书中还会讲解Objective-C中的新类型,比如BOOL类型用于布尔值(YES和NO),nil和Nil用于指针(前者用于对象指针,后者用于类指针),以及SEL类型,它表示方法选择器,用于在运行时调用方法。 "Classnames: why NS?"这部分探讨了为什么Objective-C的很多类名前都带有“NS”前缀,这源自NeXTSTEP系统的历史,NeXTSTEP是Objective-C早期的重要平台。这个前缀通常表示命名空间,有助于避免命名冲突。 最后,作者提到了"Differencing functions and method",这是Objective-C与C++的一个关键差异。在Objective-C中,方法调用是通过消息传递完成的,而在C++中,函数调用则是直接的。这种差异影响了代码的编写方式,理解这一点对于从C++转到Objective-C的开发者至关重要。 《From C++ to Objective-C》是一本深入浅出的指南,它不仅介绍了Objective-C的基本语法,还涵盖了与C++的对比,帮助开发者快速适应新的编程环境。通过阅读此书,C++程序员可以更好地理解和应用Objective-C,从而在iOS和macOS开发中游刃有余。

Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks: (a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d)Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++5.11寫出,且使用using namespace std;

2023-06-02 上传