C++ Notes Module III and IV

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Inheritance

The capability of a class to derive properties and characteristics from another


class is called Inheritance. Inheritance is one of the most important feature of
Object Oriented Programming.

Types Of Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited
from the only one base class.

Multilevel inheritance is a process of deriving a class from another derived class.

Multiple inheritance is the process of deriving a new class that inherits the attributes
from two or more classes.

Hybrid inheritance is a combination of more than one type of inheritance.

Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.

There are 3 types of access modifiers available in C++:


1. Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public
members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by
the member functions inside the class. They are not allowed to be accessed
directly by any object or function outside the class. Only the member functions
or the friend functions are allowed to access the private data members of a
class.
3. Protected: Protected access modifier is similar to private access modifier in
the sense that it can’t be accessed outside of it’s class unless with the help of
friend class, the difference is that the class members declared as Protected
can be accessed by any subclass(derived class) of that class as well.

Abstract class in C++

An abstract class is a class that is designed to be specifically used as a base


class. An abstract class contains at least one pure virtual function. You declare a
pure virtual function by using a pure specifier (= 0) in the declaration of a virtual
member function in the class declaration.
Ambiguity resolution using scope resolution operator and
virtual base class:

Ambiguous base classes (C++ only)


When you derive classes, ambiguities can result if base and derived classes have
members with the same names. Access to a base class member is ambiguous if you
use a name or qualified name that does not refer to a unique function or object. The
declaration of a member with an ambiguous name in a derived class is not an error.
The ambiguity is only flagged as an error if you use the ambiguous member name.

Name hiding
Suppose two subobjects named A and B both have a member name x. The member
name x of subobject B hides the member name x of subobject A if A is a base class
of B.

Ambiguity and using declarations


Suppose you have a class named C that inherits from a class named A, and x is a
member name of A. If you use a using declaration to declare A::x in C, then x is
also a member of C; C::x does not hide A::x. Therefore using declarations cannot
resolve ambiguities due to inherited members.
Unambiguous class members
The compiler can unambiguously find static members, nested types, and
enumerators defined in a base class A regardless of the number of subobjects of
type A an object has.

Pointer conversions
Conversions (either implicit or explicit) from a derived class pointer or reference to a
base class pointer or reference must refer unambiguously to the same accessible
base class object. (An accessible base class is a publicly derived base class that is
neither hidden nor ambiguous in the inheritance hierarchy.)
Overload resolution
Overload resolution takes place after the compiler unambiguously finds a given
function name.

Constructors in C++
A constructor is a special type of member function of a class which
initializes objects of a class. In C++, Constructor is automatically called
when object(instance of class) create. It is special member function of the
class because it does not have any return type.

Types of Constructors

1. Default Constructor : Default constructor is the constructor which doesn’t


take any argument. It has no parameters.

2.Parameterized Constructors: It is possible to pass arguments to


constructors. Typically, these arguments help initialize an object when it is
created. To create a parameterized constructor, simply add parameters to it
the way you would to any other function. When you define the constructor’s
body, use the parameters to initialize the object.

3. Copy Constructor: A copy constructor is a member function which


initializes an object using another object of the same class.

Destructors in C++
Destructor is an instance member function which is invoked automatically
whenever an object is going to be destroyed. Meaning, a destructor is the last
function that is going to be called before an object is destroyed.

Dynamic Constructor in C++


When allocation of memory is done dynamically using dynamic memory
allocator new in a constructor, it is known as dynamic constructor. By using
this, we can dynamically initialize the objects.

Polymorphism in C++
The ability of a message to be displayed in more than one form. A real-life
example of polymorphism, a person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an
employee.
In C++ polymorphism is mainly divided into two types:
1. Compile time
2. Runtime
1.Compile time polymorphism: This type of polymorphism is achieved by
function overloading or operator overloading.
Function Overloading: When there are multiple functions with same name
but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or/and
change in type of arguments.
Operator Overloading: C++ also provide option to overload operators. For
example, we can make the operator (‘+’) for string class to concatenate two
strings. We know that this is the addition operator whose task is to add two
operands. So a single operator ‘+’ when placed between integer operands ,
adds them and when placed between string operands, concatenates them.
2.Runtime Polymorphism: This type of polymorphism is achieved by
Function Overriding.
● Function overriding on the other hand occurs when a derived class
has a definition for one of the member functions of the base class.
That base function is said to be overridden.

Parametric polymorphism occurs when a routine, type or class definition is


parameterized by one or more types. It allows the actual parameter type to be
selected by the user. This way, it is possible to define types or functions that
are generics, which can be expressed by using type variables for the
parameter type.
A pointer to a C++ class is done exactly the same way as a pointer to a structure
and to access members of a pointer to a class you use the member access operator
-> operator, just as you do with pointers to structures. Also as with all pointers, you
must initialize the pointer before using it.

This pointer in C++

The this pointer is an implicit parameter to all member functions. Therefore, inside a
member function, this may be used to refer to the invoking object.

Virtual Function in C++


A virtual function is a member function which is declared within a base class
and is re-defined(Overridden) by a 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.

Pure Virtual Function in C++


A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have an implementation, we only declare it. A pure virtual
function is declared by assigning 0 in the declaration.

You might also like