Classes and Objects in C++ 3
Classes and Objects in C++ 3
in C++
A public member can be
accessed from outside the class
anywhere within the scope of
the class object. You can also
specify the members of a class
as private or protected which
we will discuss.
Default Constructor
Parameterized Constructor
A destructor is a
special member
function of a
class that is
executed
whenever an
object of it's
class goes out
of scope
Mutators and Accessors
A mutator is a function that can change the state of a host object, that is
of the object that invokes it.
A Accessor is a function that cannot change the state of it’s invoking
object.
Inline function
If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
To inline a function, place the keyword inline before the function name
and define the function before any calls are made to the function. The
compiler can ignore the inline qualifier in case defined function is more
than a line.
A function definition in a class definition is an inline function definition, even
without the use of the inline specifier.
Polymorphism
Compile time
Operator Overloading
Function Overloading
Run Time
Using Virtual Functions
Inheritance
Function Overloading
You can have multiple definitions for the same function name in the same
scope.
The definition of the function must differ from each other by the (signature)
types and/or the number of arguments in the argument list. You cannot
overload function declarations that differ only by return type.
void area(int a);
void area(int a, int b);
Operator Overloading
C++ allows you to specify more than one definition for an operator in the
same scope, which is called operator overloading .
Overloaded operators are functions with special names: the keyword
"operator" followed by the symbol for the operator being defined. Like any
other function, an overloaded operator has a return type and a parameter
list.
Box operator+(const Box&);
Static Data Members
A friend class can access private and protected members of other class in which it is
declared as friend. It is sometimes useful to allow a particular class to access private
members of other class.
class Node
{
private:
int key;
friend class LinkedList; // Now class LinkedList can access private members of node
};
Inheritance
When creating a class, instead of writing completely new data members
and member functions, the programmer can designate that the new class
should inherit the members of an existing class.
This existing class is called the base class, and the new class is referred to as
the derived class.
Private, Protected and Public
Refers to those function calls that are not resolved until run time.
Virtual functions are used to achieve late binding.
Advantage : Flexibility
Virtual Functions