lecture 5 Flashcards
what is a constructor?
- 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 do we call constructors?
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
a ……….constructor doesnt have parameters
default
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( );
BankAccount account1;
// uses the default BankAccount constructor
while,
BankAccount account1( );
// Is not legal
what is an initialization section ?
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
Class operations are typically implemented
as ……………….
- member functions
when we want to implement a function that is not a member of a class we use ……………..like:-
…………..and ………and ………….
- public accessor functions like getters and settters and friend functions
what is a friend function?
- 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
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
- class definition
Friend functions can be written as non-friend
functions using the …………………that should be part of the class
normal accessor and mutator
functions
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
- member function
- nonmember function (that could be a friend function )
difference between call by value paraeter and call by reference parameter??
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
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 ………..
- const
- declaration and definition
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 ;
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
}