2. INTRODUCTION TO C++
C++ Is Developed by Bjarne stroustrup in 1979 at Bell labs.
All the programs written in c language can be run in all c++
compilers.
C++ IS AN EXTENSION OR ADVANCE VERSION OF C
LANGAUGE.
C++ IS BASED ON OOPs (OBJECT ORIENTED
PROGRAMMING).
3. HEADER FILE DECLARATION
GLOBAL DECLARATION
CLASS DECLARATION
AND
METHOD DECLARATION SECTION
MAIN FUNCTION
METHOD DEFINITION SECTION
4. INPUT OUTPUT STREAMS IN C++
In c++, input and output are performed using stream. If you
want to output something, you put it into an output stream,
and when you want something to be input , you get it from an
input stream. The standard output and input streams in c++ are
called cout and cin and they use your computer‘s screen and
keyboard respectively . The code above outputs the character
string ― The best place to start is at the beginning‖ to your
screen by placing it in the output stream with insertion
operator <<, when we come to write programs that involve
input using the extraction operator >>.
The name cout is defined in the header file iostream.h . This is
a standard header file that provides the definition necessary for
you to use the standard input and output facilities in c++.
6. It is a way of combining data and functions into an independent entity called
class. A class is like a blueprint of object.
A class is a user defined datatype which has data members and member functions
used to access the data members.
DECLARATION OF CLASSES
A class can be defined by the keyword class. The format of class declaration is given
below:
Class class_name
{
private:
variable declarations
function declarations
public :
variable declarations
function declarations
};
7. The variables and functions declared in a class under keyword
private are not accessible to any outside function. This feature
of class declaration is called data hiding.
Access specifiers :
The keywords private and public are called access specifiers. In
the absence of a specifier , all declaration defined in a class by
default private.
Member function definition :
The member functions can be defined inside as well as outside
the class declaration. If a member function is very small then it
can be defined inside the class itself. And if a member function is
large then it can be defined outside the class.
8. MEMBER FUNCTION INSIDE CLASS
DECLARATION
#include<iostream>
using namespace std;
class class _name
{
public:
Void function_name()
{
statement1;
Statement 2;
----
}
9. MEMBER FUNCTION OUTSIDE CLASS
DECLARATION
#include<iostream>
using namespace std;
class class_name
{
public;
Void function_name();
};
void class_name :: function_name()
{
statement1;
Statement 2;
…..
}
10. The operator :: is known as scope resolution operator.
This is used to access member functions to their corresponding
class in following form.
Type class_name::function_name(parameter list)
{
function body
}
where :
type : is the data type of value to be returned.
class_name : is the name of the class to which the
function belongs.
function_name : is the name of the function being
declared.
parameter list : list of formal arguments.
11. It is an activity of defining a new class in terms of an existing class. The
existing class is known as a base class.( i.e. super class) and the new class is
known as derived class ( ie. Sub class ) .
In c++, a derived class can be obtained by adding some new data structures and
functions to base class. The derived class inherits the members of its base class
without the need to redefine them. The format for defining a derived class is
given below:
Class <derived class> : Visibility mode < base class>
{
..
..
};
Where
class: is a reserved word
<derived class > : is the name of the subclass or new class being derived.
Visibility mode : is the mode of access to items from the base class.
The access mode is optional and can be either of the following type:
Private , public & protected
12. Accessible from /
Access mode
Base Class Derived class Outside
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
The action of visibility mode or access specifiers as follows:
13. The inheritances are classified in following categories:
Single inheritance
Hierarchical inheritance
Multilevel inheritance
Multiple inheritance
BASE
CLASS
SUB
CLASS
SINGLE INHERITANCE : In this the single derived class is present.
14. HIERARCHICAL INHERITANCE :
In hierarchical inheritance, using one super or
base class & multiple derived /sub classes.
SUPER
CLASS
OR BASE
CLASS
SUB
CLASS
SUB
CLASS
15. MULTILEVEL INHERITANCE
BASE
CLASS
SUB
CLASS/
BASE
CLASS
SUB
CLASS
We know that a derived class with a single base class is said to form
single inheritance. The derived class can also become a base class for
some other derived class.
This type of chain of deriving classes can go on as for as necessary.The
inheritance of this tupe is known as multilevel inheritance.
16. MULTIPLE INHERITANCE
A class can inherit properties from more than one base class. this type of
inheritance is called as multiple inheritance. Please notice that this inheritance is
different from hierarchical inheritance wherein subclasses share the same base
class. It is also different from multilevel inheritance wherein a subclass is derived
from a class which itself is derived from another class and so on .
X Y
Z Object
Base class Base class
Derived class
or Sub Class
17. Constructors:
A class constructor is a special member function of a class that is executed whenever
we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type
at all, not even void. Constructors can be very useful for setting initial values for certain
member variables.
The constructor function can also be used by a programmer to initialize the internal data
members of the object. In C++ , there are three types of constructors:
1. The default constructor
2. Parameterized constructors
3. The copy constructor
Destructors :
A destructor is a special member function of a class that is executed whenever an
object of it's class goes out of scope or whenever the delete expression is applied to a
pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it can
neither return a value nor can it take any parameters. Destructor can be very useful for
releasing resources before coming out of the program like closing files, releasing
memories etc.