lecture 6 Flashcards

1
Q

how do operators act as functions different than that of an ordinary function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A
  • class or ennumeration type
  • arity
  • . and :: and .* and ?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

1st : friend Money operator +(const Money& amount1,const int& amount2);

2nd : Money(long dollars)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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 ………..

A

ifndef directive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
  • # define FILENAME _ H
  • # ifndef FILENAME _ H
  • # endif FILENAME _ H
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

we have a class “car” saved in a file “automotives.h” whats the normal convention for creating an identifier to use with #ifndef

A

ifndef AUTOMOTIVES_H

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

whats a namespace?

A

is a collection of name definitions,
such as class definitions and variable declarations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code you write is in the…………….unless you specify
a namespace

A

global namespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

global , using

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

declare a function named greeting in a namespace called ns1 that doesnt return a value

A

namespace ns1
{
void greeting() ;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

define a function named greeting in a namespace called ns1 that doesnt return a value and prints out “hello”

A

namespace ns1
{
void greeting()
{
cout &laquo_space;“hello” ;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using …………….(not directives) allow us to
select individual functions to use from
namespaces

A

declarations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

difference between

1 - using ns1 :: fun1 ;
2 - ns1 :: fun1( ) ;
3 -
int main( )
{
{
using namespace ns1 ;
fun1( );
}
}

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A …………..(using std::cout;) makes
only one name available from the namespace
A ………….makes all the names in the
namespace available

A
  • using declaration
  • using directive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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;

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly