3. Access Specifies in C++
Public
-Any code can access public
Data Members
Member Functions
Protected
-Any member function of the class
-Member function of the derived class
-Can access protected class
Private
Only member function of the class can access private class members
4. Friend functions & Classes in C++
If a function is defined as a friend function then private & protected data of
a class can be accessed using the function
The compiler knows a given function is a friend function by the use of the
keyword friend
The declaration of a friend function should be made inside the body of the
class(Anywhere inside class either in private or public section starting with
keyword friend
5.
6.
7. Friend Class
Like a friend function , a class can also be a friend of another class. A friend
class can access all protected & private variables of a class.
9. Difference Between Class & Struct in C++
Members of a class are private by default and members of struct are public
by default
10. Different kinds of data members in C++
Static
static member is associated with class instead of an object
Accessing static members within a class methods
same as regular data members
Accessing static data members outside the class
int c = Class_Name::variable;
Static Methods
They do not have this pointer
Can access non static data members private and protected data members
11. Const
Const variables cannot be changed(Except mutable & const cast)
We can declare const methods.const members give a guarantee that method
will not change the members of that class
12. Const object
A const object’s values cannot be changed
Non const object can call both const & non const methods
Const object can call only const methods
Mutable Modifier
Mutable data member allow user to modify a particular data member
13. Default Constructor
Zero argument constructor or constructor added by compiler
Print Bingo ten times without using loop, recursion or without writing ten
times
14.
15. Constructor Initializer List C++
Initializer List is used to initialize data members of a class. The list of
members to be initialized is indicated with constructor as a comma separated
list followed by a colon
24. Default Copy Constructor perform shallow copy .We can write our copy
constructor to perform deep copy(Deep copy & Shallow copy will be discuses
in a future session)
25. For More Information refer below link
Write Bug Free Efficient Code in C++ with Dynamic Memory Allocation
26. C++ inheritance
C++ supports 3 types of inheritance
-public
-private
-protected
Public Inheritance
30. C++ supports both single inheritance &
Multiple inheritance
Single Inheritance Multiple Inheritance
31. Method Overriding in C++
Method overriding add or replace existing functions
In C++ Methods declared as virtual in the base class can be properly
overridden by Derived class
We don’t need to repeat virtual keyword in front of the method definition in
base classes
It is recommended to add the override keyword to the end of the overriding
method(C++ 11)
By adding final keyword at the end of the methods, we can prevent
overriding(C++ 11)
32. A pointer or reference type of a class can refer to an object of that class or
its derived classes
Here Comes to the C++ polymorphism
C++ supports different types of polymorphism
1)Ad hoc polymorphism
2)Parametric polymorphism
3)Subtyping
33. Static polymorphism & Dynamic Polymorphism
Static polymorphism – Compile Time
Dynamic Polymorphism –Run time Polymorphism
34.
35. In C++ Methods declared as virtual in the base class
can be properly overridden by Derived class
38. Virtual Functions in C++
A virtual function a member function which is declared within base class and
is re-defined (Overriden) by derived class.When you refer to a derived class
object using a pointer or a reference to the base class, you can call a virtual
function for that object and execute the derived class’s version of the
function.
42. How virtual Functions resolves
C++ compiler creates a hidden class member called
virtual-pointer or in short vfptr when there are one or
more virtual functions. This vfptr is a pointer that points
to a table of function pointers. This table is also created
by compiler and called virtual function table or vftable.
Each row of the vftable is a function pointer pointing to a
corresponding virtual function.
46. Making base class destructor virtual guarantees that the
object of derived class is destructed properly
If derived class allocates dynamic memory ,open files, database connections
if base class destructor is not virtual then only base destructor may call and
oboe resources will not be released;
47.
48. Pure virtual Functions & Abstract Classes
A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have implementation, we only declare it. A pure virtual
function is declared by assigning 0 in declaration
When a class has one or more than pure virtual functions. That class becomes
an abstract class & we cannot create an object out of it. But we can create
pointers or reference types in abstract classes
If we inherit from abstract class we have to add implementation to all pure
virtual functions ,Otherwise it will also become abstract
C++ does not have interfaces, Only abstract classes
53. The solution to this problem is ‘virtual’ keyword. We make the classes
‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of
‘Person’ in ‘TA’ class. For example, consider the following program.