Derived Classes Are Created From Existing Classes Called Base
Derived Classes Are Created From Existing Classes Called Base
Inheritance types
1. Single inheritance:
Only one base class, and one derived class access member
functions, data members from it. and derived class can't
behave again base class.
2. Multilevel inheritance:
eg: base class
l
derived1 class
l
derived2 class
l
derived3 class
.
.
Again derived class acts as another base class
3.Multiple inheritance:
There are two base classes and one derived class which is
derived from both two base classes.
Base1 class base2class
derived class
4. Hierarchical inheritance:
in this one common base class and derived class and from
those another derived classes.
5. Hybrid inheritance:
it is a combination of any above inheritance types.
That is either multiple-multilevel or multiple-hierarchical
MULTIPLE INHERITANCE
Deriving directly from more than one class is usually called
multiple inheritance. Multiple inheritance is an important
feature in C++ and a very good structuring tool.
Friend functions
In principle, private and protected members of a class
cannot be accessed from outside the same class in which
they are declared. However, this rule does not affect friends.
Friends are functions or classes declared with the friend
keyword.
If we want to declare an external function as friend of a
class, thus allowing this function to have access to the
private and protected members of this class, we do it by
class Exforsys
{
protected:
int x;
};
class Exf1:public Exforsys
{ };
class Exf2:public Exforsys
{ };
class Training:public Exf1,public Exf2
{
public:
int example()
{
return x;
}
};
The above program results in a compile time error as the
member function example() of class Training tries to access
member data x of class Exforsys. This results in an error
because the derived classes Exf1 and Exf2 (derived from
base class Exforsys) create copies of Exforsys called
subobjects.
This means that each of the subobjects have Exforsys
member data and member functions and each have one
copy of member data x. When the member function of the
class Training tries to access member data x, confusion
arises as to which of the two copies it must access since it
derived from both derived classes, resulting in a compile
time error.
When this occurs, Virtual base class is used. Both of the
derived classes Exf1 and Exf2 are created as virtual base
classes, meaning they should share a common subobject in
their base class.
For Example:
class Exforsys
{
protected:
int x;
;
POLYMORPHISM
Polymorphism is the ability to use an operator or function in
different ways. Polymorphism gives different meanings or
functions to the operators or functions. Poly, referring to
many, signifies the many uses of these operators and
functions.
A single function usage or an operator functioning in many
ways can be called polymorphism. Polymorphism refers to
codes, operations or objects that behave differently in
different contexts.
Polymorphism is a powerful feature of the object oriented
programming language C++. A single operator + behaves
differently in different contexts such as integer, float or
strings referring the concept of polymorphism. The above
concept leads to operator overloading. The concept of
overloading is also a branch of polymorphism. When the
exiting operator or function operates on new data type it is
of
the
concept
of
Types of Polymorphism:
C++ provides three different types of polymorphism.
Virtual functions
Function name overloading
Operator overloading
run-time
compile-time
ad-hoc polymorphism
parametric polymorphism
polymorphism
is
implemented
with
ad-hoc polymorphism:
If the range of actual types that can be used is finite and the
combinations must be individually specified prior to use, this
is called ad-hoc polymorphism.
parametric polymorphism:
If all code is written without mention of any specific type and
thus can be used transparently with any number of new
types it is called parametric polymorphism.
Concept of Pointers:
Every storage location of memory has an associated
address. Address is a number that grows sequentially. For
every program placed in memory, each variable or function
in the program has an associated address.
The address of operator:
The address of operator or Reference operator is denoted by
the notation &. When the user wants to get the address of a
variable, then the reference operator & can be used. The
operator & is used to find the address associated with a
variable.
The syntax of the reference operator is as follows:
&variablename
This means that the address of the variablename is
achieved.
Example
#include <iostream.h>
void main()
{
int exf=200;
int test=300;
cout<<endl<<&exf
<<endl<<&test;
}
between
void
pointers
and
NULL
new
delete
MANIPULATING STRINGS
Character representation
String Literals
Open a file
The first operation generally performed on an object of one
of these classes is to associate it to a real file. This
procedure is known as to open a file.
An open file is represented within a program by a stream
object these classes, in the previous example this was
myfile) and any input or output operation performed on this
stream object will be applied to the physical file associated
to it.
In order to open a file with a stream object we use its
member function open():
open (filename, mode);
Where filename is a null-terminated character sequence of
type const char * (the same type that string literals have)
representing the name of the file to be opened, and mode is
an optional parameter with a combination of the flags.
Closing a file
When we are finished with our input and output operations
on a file we shall close it so that its resources become
available again. In order to do that we have to call the
stream's member function close().
This member function takes no parameters, and what it does
is to flush the associated buffers and close the file.
Once this member function is called, the stream object can
be used to open another file, and the file is available again
to be opened by other processes.
In case that an object is destructed while still associated