C++ Information Hiding & Classes Lecture Notes PDF
Document Details
Uploaded by FervidDune
ETH Zurich
Tags
Related
Summary
These lecture notes cover information hiding, encapsulation principles, and their application within the C++ programming language. The content explores the differences between structures (structs) and classes (classes) in terms of data access and function declarations, showcasing both approaches, as well as examples demonstrating the use of constructors in C++.
Full Transcript
17. Information Hiding Encapsulation, Classes, Member Functions, Constructors 441 A new Type with Functionality... struct rational { int n; int d; // INV: d != 0 }; // POST: return value is the sum of a and b rational operator+ (rat...
17. Information Hiding Encapsulation, Classes, Member Functions, Constructors 441 A new Type with Functionality... struct rational { int n; int d; // INV: d != 0 }; // POST: return value is the sum of a and b rational operator+ (rational a, rational b) { rational result; result.n = a.n * b.d + a.d * b.n; result.d = a.d * b.d; return result; }... 442... should be in a Library! rational.h Definition of a struct rational Function declarations rational.cpp arithmetic operators (operator+, operator+=,...) relational operators (operator==, operator>,...) in/output (operator >>, operator ” next week) 452 Comparison Roughly like this it were...... without member functions class rational { struct fraction { int n; int n;...... public: }; int numerator () const { int numerator (const fraction& that) return this->n; { } return that.n; }; } rational r; fraction r;..... std::cout name >> score) { Input next pair Record name if score suf- if (50 score) { if (50