CPP-Unit 1-A4
CPP-Unit 1-A4
In 1970 and 1980, popular programming methodology was procedural programming. Procedural
programming methodology is based on following process –
Procedural programming is based around data structures and subroutines. The functions are where stuff
actually "happens", and the data structures are simply containers for the information needed by those
functions.
While we concentrate on functions, very less importance is given to the data which are being used various
functions. What happens to the data? How are they affected by functions which operate on them?
Further, in a multi-function program, many important data are stored as global variables so that they may
be accessed by all the functions. Global variables can be accidentally changed by functions. In a large
program it is difficult to identify & understand which functions use which data. This may result into bugs (i.e.
errors)
Another serious problem with procedural program is that it does not represent real-world problem. This is
so because procedural programs do not focus on real-world entities in the problem.
Object oriented programming, on the other hand, shifts your primary focus to the data itself. Instead of
asking "what do I want to do and what will I need to know to do it", you ask "what kind of things do I want to
have and what can those things do for me".
Instead of designing your functions first and then coming up with data structures to support them, you
design types first and then come up with the operations needed to work with them.
OOP treats data as the most important element in the program and does not allow them to flow freely
around the system. It ties data more closely to the functions that operate on it, and protects it from
accidental modifications outside the functions.
OOP allows decomposition of a problem into a number of entities called objects and build data and
functions around these objects. The data of an object can be accessed only by functions associated with
that object.
Some of the important characteristics of OOP are:
Objects
o are basic runtime entities in OOP.
o may represent a student, an employee, a vehicle, a bank account etc.
o are variables of user-defined datatype-class.
o occupy space in memory, have life-time & scope.
o interact with each other through functions.
Classes
o are user-defined datatype.
o define attributes & behavior of objects.
o contain variable that may store value of attributes.
o also contain code to assign, modify or access the value of attributes.
Data Abstraction
o Data Abstraction refers to the act of representing main features without showing details.
o To work with objects, only object name & function name are required. It is not necessary to know &
understand how functions operate on data.
Encapsulation
o The wrapping up of data and functions together inside a class, is known as Encapsulation.
o Encapsulation ties data to functions which operate on the data.
o The data can not be directly accessed/modified by outside world and only those functions which are
wrapped inside a class can do so.
Inheritance
o Inheritance is the process by which objects of one class acquire the properties of another class.
o It supports hierarchical classification.
o In OOP, a new class is defined with the help of one or more existing classes.
o The new class will have its own features as well as some or all features of other classes.
o Features implemented by one class are re-used in other classes. Thus, inheritance supports
reusability.
Polymorphism
o Polymorphism is a Greek term meaning the ability to take more than one form.
o An operation may behave in different ways depending on the type of data used in the operation.
o Two types: Compile-time polymorphism and Run-time polymorphism
o Function overloading and operator overloading are example of compile-time polymorphism.
o Use of virtual function is an example of run-time polymorphism.
Dynamic Binding
o Binding means linking the function call to the code to be executed.
o Dynamic binding (also known as late binding) means the code to be executed is not known until the
execution time.
o It is associated with inheritance & polymorphism.
o Virtual function is used for implementing dynamic binding.
Introduction to C++
o C++ is an object-oriented programming language.
o It was developed by BjarneStroustrup at AT&T Bell Labs (USA) in early 1980s.
o Initial name was C with classe, later renamed to C++.
o Combines features of C and Simlula67.
o C++ is a superset of C.
// main()
As shown above a typical C++ program starts with inclusion of required header files. iostream.h and
conio.h are most frequently used.
Next, constant is declared using either #define pre-processor directive or using const keyword.
A class is defined just below constant declaration. Member functions of a class may be defined outside the
class. If a programmer decides to do so, member functions are defined after the class definition and before
main().
At last, main(), which is the starting point of the program execution, is defined.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
cout<<”Hello World”;
getch();
}
A C++ program must be stored in a file with .CPP extension. Also, note that the default return-type of
main() in C++ is int, so either a return 0; statement must be present in main() or void should be used as the
return-type.
Basic datatypes
User-defined Datatypes
o struct and union are valid user-defined datatypes in C++ also.
o C++ allows creation of a class- which is also a user-defined datatype.
o More about classes is discussed later.
Derived Datatypes
Arrays
o Arrays are also supported in C++.
o The only exception is the way a character array is declared in C++; C++ requires that the size of the
array must be one more than the string assigned. e.g. char str[6] = “hello” ; is correct whereas char
str[5] = “hello” ; is incorrect.
Functions
o Functions in C++ are different than in C.
o You can define more than one functions with same name but different type and no. of arguments.
This concept is known as Function Overloading and is discussed later.
o It is optional to pass one or more arguments when calling a function.
o Functions can return address.
Pointers
o C++ introduces two types of pointers: (1) Constant pointer (2) Pointer to a constant
Symbolic Constants
o An enumeration can be defined as: enum {X, Y, Z} ;This defines X, Y & Z as integer constants with
value 0, 1 & 2 respectively..
o We can assign values to X, Y & Z explicitly. e.g. enum {X=100, Y=200, Z=300} ;
Variables
o In C all variables must be declared in the beginning of the scope. C++ allows declaration of variable
at the place of use.
o In C, a variable must be initialized using a constant value. However, C++ allows initialization of
variable at runtime. e.g the following statements are valid in C++:
int n = strlen(s1) ;
float area = 3.14 * radius * radius ;
o Thus, both declaration & initialization can be done simultaneously at the place where a variable is
used for the first time.
Reference Variables
o A reference variable provides an alias i.e. an alternate name for an existing variable.
o It can be declared as follows: <datatype>&<ref. variable name> = <variable name> ;
o e.g. float total = 100;
float&sum = total;
Here, total is a float variable that already exists.sum is an alternate name for total.
Both total and sum refer to the same variable in the memory.
total = 200;
sum = 300;
Operators in C++
o All C operators are valid in C++.
o In addition, C++ introduces following new operators:
1) :: scope resolution operator
2) new memory allocation operator
3) delete memory release operator
4) ->* pointer to member operator
5) .* pointer to member operator
6) << insertion operator
7) >> extraction operator
#include <iostream.h>
#include <conio.h>
void main(void)
{
cout<<"\n x = "<<x; //Refers to global x , x=1000
int x=100;
{
cout<<"\n\n Inside scope...";
cout<<"\n x = "<<x; //Refers to x declared latest i.e. in main(), x=100
int x = 10;
o Scope resolution operator has two more uses: To define member function outside the class and to
access static members of the class.
o Many times you are not aware in advance how much memory you will need to store particular
information in a defined variable and the size of required memory can be determined at run time.
o You can allocate memory at runtime for the variable of a given type using a special operator in C++
which returns the address of the space allocated. This operator is called new operator.
o If you are not in need of dynamically allocated memory anymore, you can use delete operator,
which de-allocates memory previously allocated by new operator.
Manipulators
o Manipulators are operators that are used to format the data display.
o The most commonly used manipulators are endl and setw.
o The endl is used to insert a newline in the output.
o It can be used as: cout<<”some string<<endl;
o By default, << operator displays left-justified values.
o If you want to display values using some specific space and left-justification, setw() operator can be
used.
o e.g. cout<<setw(5)<<345;
It will display : 345 i.e. left-justified.
The following table lists most of the frequently used header files: