lecture 5 Flashcards

1
Q

what is a constructor?

A
  • its a public member function
  • A constructor’s name must be the name of the class
  • A constructor cannot return a value No return type, not even void, is used in declaring or defining a constructor
  • A constructor is automatically called when an object
    of the class is declared
  • can be used to initialize member
    variables when an object is declared
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how do we call constructors?

A

A constructor is called in the object declaration

exampe:-

BankAccount account1(10, 50, 2.0);

// Creates a BankAccount object “account1” and calls the
constructor to initialize the member variables

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

a ……….constructor doesnt have parameters

A

default

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

The default constructor is called during
declaration of an object An argument list is not used

which call is legal and which is not ?

  • BankAccount account1;
  • BankAccount account1( );
A

BankAccount account1;
// uses the default BankAccount constructor

while,
BankAccount account1( );
// Is not legal

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

what is an initialization section ?

A

An initialization section in a function definition
provides an alternative way to initialize
member variables

example:-

  • BankAccount::BankAccount( ): balance(0),
    interest_rate(0.0);
  • BankAccount::BankAccount(int dollars, int cents, double rate)
    : balance (dollars + 0.01 * cents),
    interest_rate(rate)

NOTE : the parameters could be arguments in the initialization section

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

Class operations are typically implemented
as ……………….

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

when we want to implement a function that is not a member of a class we use ……………..like:-
…………..and ………and ………….

A
  • public accessor functions like getters and settters and friend functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is a friend function?

A
  • frriend functions are NOT members of a class, but
    can access private member variables of the class
  • A friend function is declared using the keyword
    friend in the class definition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A friend function is declared as a friend in the
……………..
◼ A friend function is defined as a nonmember
function WITHOUT using the “::” operator
◼ A friend function is called WITHOUT using the
‘.’ operator

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

Friend functions can be written as non-friend
functions using the …………………that should be part of the class

A

normal accessor and mutator
functions

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

use a …………… if the task
performed by the function involves only one object

use a……………. if the task
performed by the function involves more than
one object

A
  • member function
  • nonmember function (that could be a friend function )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

difference between call by value paraeter and call by reference parameter??

A

call by reference parameter is MORE efficient because there is only 1 copy of the argument as the parameter here acts as a place holder replaced by the argument .

A call-by-value parameter LESS efficient because the parameter is a local variable initialized to the value of the argument this results in two copies of the argument

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

To “mark” a call-by-reference parameter so it
cannot be changed:
◼ Use the modifier ……. before the parameter type
◼ The parameter becomes a constant parameter
◼ const used in the function-……….. and ………..

A
  • const
  • declaration and definition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

To declare a function that will not change the
value of any member variables:
◼ Use const after the parameter list and
just before the semicolon

example :-

void output (ostream& outs) const ;

A

To define a function that will not change the
value of any member variables:
◼ Use const in the same location as the function
declaration

example :-

void Money :: output(ostream& outs) const
{
// lines
}

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