Introduction to Computer Science PDF Fall 2024

Summary

This document introduces custom data types in C++ programming, specifically focusing on rational numbers. The authors, Manuela Fischer and Felix Friedrich from ETH Zurich, provide a detailed explanation and examples of how to create and utilize custom data types in C++ for calculations.

Full Transcript

INTRODUCTION TO COMPUTER SCIENCE 252-0032, 252-0047, 252-0058 Document authors: Manuela Fischer and Felix Friedrich Department of Computer Science, ETH Zurich Fall 2024 1 Custom Data Types...

INTRODUCTION TO COMPUTER SCIENCE 252-0032, 252-0047, 252-0058 Document authors: Manuela Fischer and Felix Friedrich Department of Computer Science, ETH Zurich Fall 2024 1 Custom Data Types 148 Section 12 Custom Data Types In this section, we learn how to build our own types if the built-in types provided by C++ are not enough for our purposes. For instance, suppose we want to perform calculations with rational numbers Q, which are numbers of the form nd with n and d in Z. C++ does not have a built-in type for rational numbers, but we can build our own C++ type rational. Our vision is that we can use this type rational just as we can, say, int: // input std::cout > r; std::cout > s; // calculation rational t = r + s; // output std::cout

Use Quizgecko on...
Browser
Browser