C Note Part2
C Note Part2
Static Member function: The member functions of the class can also be
made static which are known as static member functions. Some of the
features/characteristics of such function are as follows:
• It is declared with the keyword static.
• It is also known as class member function.
• It can be invoked(called) with or without object
• It can only access static members of the class.
Example:
class Student{
int x;
static int y; //static member variable
1
By: Manoj Sapkota, United Technical College
Example program-24:
Q. Illustrate an example how the destructor is used to release dynamically
allocated memory and avoid memory leakage.
2
By: Manoj Sapkota, United Technical College
friend function: It is a special function that can access any members of the
class to which it is declared as a friend.
It is declared in the class with ‘friend’ keyword.
It is not a member function of the class to which it is a friend.
It must be defined outside the class.
It cannot directly access the members of the class.
It uses objects to access the members of the class.
Output: Value: 2
3
By: Manoj Sapkota, United Technical College
Q. The concept of friend function is against the philosophy of OOP. Justify it.
Answer: One of the important characteristics of OOP is data encapsulation.
That means, the data and functions are wrapped into a single unit. The data
are kept private and are not accessible to the outside world. These data are
accessed by only the member functions that are present in the same unit (i.e.
in the same class). This process is also known as data hiding as the data are
hidden from outside.
But, there is a special function known as friend function which violates the
above principle of data hiding. If any non-member function is declared as a
friend inside a class, then such function can access all the private data
members of the class.
Output: Value: 2
5
By: Manoj Sapkota, United Technical College
Syntax:
class Base_class
{
// data members and member functions
};
Example:
class Person
{
// data members and member functions
};
class Student : public Person
{
// data members and member functions
};
Visibility modes in inheritance: These modes specify how the features of the
base class are derived to the base class. It controls the accessibility of the
private, protected and public members of the base class while deriving on the
base class. The visibility modes could be private, protected or public.
a) Public: If the visibility mode is public, the members in the derived class
remain as it is as that of base class. That means, public members of the
base class remain public in the derived class and the protected members
of the base class remain protected in the derived class.
6
By: Manoj Sapkota, United Technical College
b) Protected: If the visibility mode is protected, the public and the protected
members of the base class become protected in the derived class.
c) Private: If the visibility mode is private, the protected and the public
members of the base class become private in the derived class.
Note: Private members of the base class are not accessible to the derived class.
Summary Table:
Example:
class NPI {
private:
int a;
protected:
int b;
public:
int c;
};
class Utec : private NPI {
//a is not accessible
//b is private
//c is private
};
class Kabilas_resort : protected NPI {
//a is not accessible
//b is protected
//c is protected
};
class NS_hospital : public NPI {
//a is not accessible
//b is protected
//c is public
};
7
By: Manoj Sapkota, United Technical College
General form:
class Base_class
{
// body of Base_class
};
class Derived_class : visibility_mode Base_class
{
// body of Derived_class
};
General form:
class Base_class1
{
// body of Base_class1
};
8
By: Manoj Sapkota, United Technical College
class Base_class2
{
// body of Base_class2
};
class Derived_class : visibility_mode Base_class1, visibility_mode Base_class2
{
// body of Derived_class
};
General form:
class Base_class2
{
// body of Base_class2
};
class Base_class1 : visibility_mode Base_class2
{
// body of Base_class1
};
class Derived_class : visibility_mode Base_class1
{
// body of Derived_class
};
9
By: Manoj Sapkota, United Technical College
General form:
class A
{
// body of class A
};
class B : visibility_mode A
{
// body of class B
};
class C : visibility_mode A
{
// body of class C
};
5. Hybrid inheritance: When two or more than two types of inheritances are
combined, it is known as hybrid inheritance. It could be the combination of
multiple and multi-level, multiple and hierarchical, multi-level and hierarchical
or any other combinations of inheritance. An example of a hybrid inheritance is
shown in figure, which is a combination of hierarchical & multiple inheritance.
General form:
class A
{
// body of class A
};
class B : visibility_mode A
{
// body of class B
};
class C : visibility_mode A
{
// body of class C
};
class D: visibility_mode B, visibility_mode C
{
// body of class D
};
10
By: Manoj Sapkota, United Technical College
Explanation: In the above program, the base class A and the derived class B
have the same function fun( ) with same name and signature. Now, when this
function is called from the derived object, the fun( ) in the derived class is
executed as it overrides the same in base class.
11
By: Manoj Sapkota, United Technical College
Explanation: In the above program, the two base classes ‘Russia’ and ‘Ukraine’
have same function acceptWar( ), and is absent (is not overridden) in derived
class ‘World’. If this function is called by creating an object of derived class
‘World’, then the compiler gets confused which function of the base class to
call. This is the ambiguity problem.
12
By: Manoj Sapkota, United Technical College
Here, both the class B and class C are inheriting the features from class A.
They both have the single copy of class A. Both these classes are inherited
further into another class D. So, the features from class A are inherited twice to
class D. The class D will have two copies of A, one from class B and another
from class C. Now, if any member of class A is accessed by an object of class D,
the compiler gets confused which path should it follow either from B, or from C
and hence displays error. This is the ambiguity problem resulting in hybrid
inheritance.
13
By: Manoj Sapkota, United Technical College
};
class B: virtual public A {
};
class C: virtual public A {
};
class D: public B, public C {
};
14
By: Manoj Sapkota, United Technical College
A use of virtual base class that solves the ambiguity problem occurred in
program-34 is shown below.
If the base class has parameterized constructors, then they should be explicitly
called from the constructor of the derived class using the following syntax:
Derived_class_constructor : Base_class_constructor(arguments)
15
By: Manoj Sapkota, United Technical College
Explanation: When an object of derived class Ukraine is created, both the constructors
of the base class and the derived class are executed automatically. At first, the
constructor of the base class Russia is executed and then the constructor of the
derived class Ukraine. An example that demonstrates the case of base class having
parameterized constructor is shown below. [Example program- 36]
16