The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Programming Languages and Tools:
CS:3210:0001
Lecture/Lab #11
Programming with C++
Operator overloading (part II), enums, member access control
Warm up
-
Is the following a valid C++ program? If so, what does it do?
int main() {
int n = 42;
int& r;
r = n;
std::cout << r << std::endl;
}
-
How about this one?
auto square( int n ) { return n * n; }
int main() {
int& i = 17;
std::cout << i << std::endl;
int& j = square( 2 );
std::cout << j << std::endl;
}
Operator overloading (part II)
- Operators =, [], (), and -> are required to be defined as member functions
- Operators +=, -=, ++, --, *=, /=, unary +, unary - are commonly defined as member functions
- Operators +, -, *, /, <, >, <=, >=, ==, !=, >>, << are commonly defined as freestanding functions
- The first parameter of an output operator << is a reference to a nonconst std::ostream object
- The first parameter of an input operator >> is a reference to a nonconst std::istream object
Exercise 1
Define a stream output operator for type `rectangle`
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab11 exercises" poll in #general
User-defined types: enum
- Enumerations let us group together sets of integral constants.
- In scoped enumerations the constants are scoped within the enumeration type.
- In unscoped enumerations the constants are placed into the same scope as the enumeration itself.
- By default, constant values (aka enumerators) start at 0 and each next constant has a value 1 greater than the preceding one.
- But we can supply explicit initializers to any or all of the enumerators.
- Objects/constants of an unscoped enumeration type are implicitly converted to an integral type.
Exercise 2
Define a stream output operator for an enumeration type
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab11 exercises" poll in #general
Member access control
- Public members define the interface of the type and are accessible to all parts of the program.
- Private members are considered to be implementation details and are accessible to the member functions of the type only.
- A class may contain zero or more access specifiers, and there are no restrictions on how often an access specifier may appear.
- Each access specifier specifies the access level of the succeeding members.
- The specified access level remains in effect until the next access specifier or the end of the class body.
- The only difference between class and struct keywords is the default access level.
Programming with C++, Spring 2020, Lecture #11
By Aleksey Gurtovoy
Programming with C++, Spring 2020, Lecture #11
Operator overloading (part II), enums, member access control
- 636