C++ Note1
C++ Note1
Machine language:
It is a set of instructions executed directly by a computer’s central processing unit CPU. Each
instruction preforms a very specific task, such as a load, a jump, or an arithmetic logic unit
operation on a unit of data in a CPU. Every program directly executed by a CPU is made up
of a series of such instructions. It is represented by a string of binary digits 0 and 1; 0 stands
for the absence of electric pulse and 1 stands for the presence of electric pulse.
Assembly language:
Global data are more vulnerable to inadvertent change by a function. In a large program it is
very difficult to identify what data is used by which function. In case we need to revise an
external data structure, we also need to revise all functions that access the data. This provides
opportunity for bugs to creep in. Another drawback is that it does not model real world
problems very well. This is because functions are action oriented and do not really
correspond to the elements of the problem.
OOP treats data as a critical element in the program development and does not allow it to flow
freely around the system. It ties the data more closely to the functions that operate on it, and
protects it from accidental modification from outside functions. OOP allows decomposition of
a problem into a number of entities called objects and then builds data and function around
these objects. The data of an object can be accessed only by the functions associated with that
object. However, functions of one object can access the functions of other objects.
1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
OBJECTS:
Objects are the basic run-time entities in an OOP. They may represent a person, a place, a
bank account, a table of data or any item that the program has to handle. Program objects
must be chosen such that they match with the real-world objects. When a program is
executed, the objects interact by sending messages to one another. For example, if
“customer” and “account” are two objects in a program, then the customer object may send a
message to the account object requesting for the bank balance. Each object contains data, and
code to manipulate data. Objects can interact without having to know details of each other’s
data or code.
CLASS:
A class is collection of objects of similar type. For example, apple, orange, mango are
members of class fruit. Classes are user defined data types and behave like built in types of
programming language.
DATA ABSTRACTION and ENCAPSULATION:
The wrapping up of data and function into a single unit (called class) is known as encapsulation.
The data is not accessible to the outside world and only those functions which are wrapped in
the class can access it. These functions provide the interface between the object’s data and the
program.
Abstraction refers to the act of representing essential features without including the background
details or explanations. Classes use the concept of abstraction and are defined as a list of
abstracts attributes such as size, weight and cost and functions to operate on the attributes.
INHERITANCE:
Inheritance is the process by which objects of one class acquire the properties of another
class. In OOP, the concept of inheritance provides the idea of reusability. This mean that we
can add additional features to an existing class without modifying it. This is possible by
designing a new class will have the combined features of both the classes. For example, bird
‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’.
POLYMORPHISM:
Polymorphism means the ability to take more than one form. An operation may exhibit
different behaviours in different instances. The behaviour depends upon the type of data used
in the operation. The process of making an operator to exhibit different behaviours in
different instances is known as operator overloading.
DYNAMIC BINDING:
Binding refers to the linking of a procedure call to the code to be executed in response to the
call. Dynamic binding also known as late binding means that the code associated with a given
procedure call is not known until the time of the call at run time. It is associated with the
polymorphism and inheritance. A function call associated with a polymorphic reference
depends on the dynamic type of the reference.
MESSAGE PASSING:
An OOP consists of a set of objects that communicate with each other. A message for an
object is a request for execution of a procedure, and therefore will invoke a function
(procedure) in the receiving object that generates the desired result. Message passing involves
specifying the name of the object, the name of the function(message) and the information to
be sent. Example
BENEFITS OF OOP:
OOP offers several benefits to both the program designer and the user. Object-oriented
contributes to the solution of many problems associated with the development and quality of
software products. The principal advantages are:
1. Through inheritance we can eliminate redundant code and extend the use of existing
classes.
2. We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
3. This principle of data hiding helps the programmer to build secure programs that can’t be
invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist without any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large systems.
7. Message passing techniques for communication between objects makes the interface
description with external systems much simpler.
8. Software complexity can be easily managed.
What is C++?
Comments
C++ introduces a new comment symbol // (double slash). Comments start with a double slash
symbol and terminate at the end of line. A comment may start anywhere in the line and
whatever follows till the end of line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multi line comments can be
written as follows:
// This is an example of
// C++ program to illustrate
// Some of its features
Output Operator
The statement cout << “C++ is better than C“ displays the string with in quotes on the screen.
The identifier cout (pronounced as ‘C out’) is a predefined object that represents the standard
output stream in C++. The operator << is called insertion or put to operator.
Return Statement
In C++ main ( ) returns an integer type value to the operating system. Therefore, every main
( ) in C++ should end with a return (0) statement, otherwise a warning or an error might occur
Input operator
The statement
cin >> number 1;
is an input statement and causes the program to wait for the user to type in a number. The
number keyed in is placed in the variable number 1. The identifier cin (pronounced ‘C in’)
is a predefined object in C++ that corresponds to the standard input stream. The operator >>
is known as extraction or get from operator.
Multiple use of insertion operator << and extraction operator in one statement is known as
cascading. The statement
cout << “Sum = “ << sum << “ \ n ”;
first sends the string “Sum=” to cout and then sends the value of sum. Finally, it sends the
newline character so that the next output will be in the new line. When cascading an output
operator, we should ensure necessary blank spaces between different items.
This is one statement but provides two lines of output. If you want only one line of output,
the statement will be:
cout << “Sum = “ << sum << “ ,”
• Include files
• Class declaration
• Member functions definitions
• Main function program
It is common practice to organize a program into three separate files. The class declarations
are placed in a header file and the definitions of member functions go into another file. This
approach enables the programmer to separate the abstract specification of the interface (class
definition) from the implementation details (member functions definitions). Finally, the main
program that uses the class is placed in a third file which “includes” the previous two files as
well as any other file required.