lecture 6 Flashcards
how do operators act as functions different than that of an ordinary function?
An ordinary function call enclosed its arguments in
parenthesis
add(cost, tax)
while binary operator, the arguments are on either
side of the operator
cost + tax
At least one argument of an overloaded operator
must be of a ……… or …………..type (user-defined type).
◼ An overloaded operator can be a friend of a class
◼ New operators CANNOT be created
◼ The number of arguments for an operator CANNOT
be changed (must keep the ……. of the operator)
◼ The precedence and associativity of an operator CANNOT be changed
◼ …….. , …….. , …….. , …… cannot be overloaded
- class or ennumeration type
- arity
- . and :: and .* and ?
to perform an operation like
class_object + integer
1st : compiler looks for an overloaded + operator
if it exist
2nd : if it doesn’t exist , the compiler looks for a constructor that takes a single integer that converts this integer to a class object so it can be added
1st : friend Money operator +(const Money& amount1,const int& amount2);
2nd : Money(long dollars)
a program uses several classes each could be tsored in its own header and implementation files
c++ does NOT allow multiple declarations of a class
so we use ………..
ifndef directive
To prevent multiple declarations of the class,
we can use these directives:
1- …………… to add this class to a list indicating that it has been seen
2- ……………to see if the class has been defined
3- ……………, skip to it If the class has been defined
- # define FILENAME _ H
- # ifndef FILENAME _ H
- # endif FILENAME _ H
we have a class “car” saved in a file “automotives.h” whats the normal convention for creating an identifier to use with #ifndef
ifndef AUTOMOTIVES_H
whats a namespace?
is a collection of name definitions,
such as class definitions and variable declarations
Code you write is in the…………….unless you specify
a namespace
global namespace
The …….. namespace does not require the
…….directive
A using directive placed at the beginning of a
file, outside any block, applies to the entire file
global , using
declare a function named greeting in a namespace called ns1 that doesnt return a value
namespace ns1
{
void greeting() ;
}
define a function named greeting in a namespace called ns1 that doesnt return a value and prints out “hello”
namespace ns1
{
void greeting()
{
cout «_space;“hello” ;
}
}
Using …………….(not directives) allow us to
select individual functions to use from
namespaces
declarations
difference between
1 - using ns1 :: fun1 ;
2 - ns1 :: fun1( ) ;
3 -
int main( )
{
{
using namespace ns1 ;
fun1( );
}
}
1 - makes only fun1 in ns1 available , not the whole namepace .
meaning if there are multiple namespaces having fun1, it only uses the ns1 ‘s version of fun1 throughout the whole program.
2 - only makes use of the ns1 ‘s version of fun1 ONCE
3 - includes the whole ns1 namespace to be used in the program using the “using directive”
A …………..(using std::cout;) makes
only one name available from the namespace
A ………….makes all the names in the
namespace available
- using declaration
- using directive
assuming that there is a function “my_function” that is defined in 2 namespaces “ns1” and “ns2” and has NOT been used in the program , which is illegal and why ?
1 - A using-………….. :
using ns1 :: my_function;
using ns2 :: my_function;
2 - A using-………….. :
using namespace ns1;
using namespace ns2;
1 - A using declaration is illegal, even if my_function is never used
2 - A using directive is OK, provided my_function is never used!