2. Benefits of OOPs
1. Reduced complexity of a problem: The given problem can be viewed as a
collection of different objects. Each object is responsible for a specific task. The objects
of the system closely corresponds and relate in a one-to-one manner to the objects in
the real world.
2. Reusability: Once a class has been developed, implemented and tested, it can be
distributed to other programmers for use in their programs. Inheritance is considered
as an excellent way to organize abstraction and as a tool to support reuse.
3. Less Code redundancy: Through inheritance we can eliminate redundant code and
extend the use of existing classes.
4. Data Hiding: The programmer can hide the data and functions in a class from
other classes. It helps the programmer to build the secure programs. It is achieved by
restricting the members of class as private or protected.
5. Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify
existing code as new objects can be created with small differences to existing ones.
3. Disadvantages of OOPs
1. Steep learning curve: The thought process involved in object-oriented programming
may not be natural for some people, and it can take time to get used to it.
2. Larger program size: Object-oriented programs typically involve more lines of code
than procedural programs.
3. Slower programs: Object-oriented programs are typically slower than procedure
based programs, as they typically require more instructions to be executed.
4. Not suitable for all types of problems: There are problems that lend themselves well
to functional-programming style, logic-programming style, or procedure-based
programming style, and applying object-oriented programming in those situations will
not result in efficient programs.
4. Applications of OOPs
The Object oriented approach of software design has been very successful for
applications other than engineering. The areas in which OOPs concepts can be
applied
1. Object Oriented databases
2. CAD and CAM
3. Decision Support and Office automation Systems.
4. Simulation and modeling
5. Distributed Computing and Applications
6. Web based Applications
7. User interface design such as windows, menu ,…
5. Difference between C and C++
C C++
C is a structural programming language. C++ is an object oriented programming
language.
Emphasis is on procedure or steps to
solve any problem.
Emphasis is on objects rather than
procedure.
Functions are the fundamental building
blocks.
Objects are the fundamental building
blocks.
In C, the data is not secured. Data is hidden and can’t be accessed by
external functions.
C follows top down approach. C++ follows bottom up approach
Variables must be defined at the
beginning in the function.
Variables can be defined anywhere in the
function.
In C, namespace feature is absent. In C++, namespace feature is present.
C doesn’t support exception handling
directly. Can be done by using some
other functions.
C++ supports exception handling. Done
by using try and catch block.
Features like function overloading and
operator overloading is not
present.
C++ supports function overloading and
operator overloading.
6. C++ Development Environment
To compile a C++ program , you need a C++ compiler. The compiler is a software,
which translates the source code to binary code.
8. 1. Preprocessor : The C++ preprocessor copies the contents of the included header
files into the source code file, generates macro code, and replaces symbolic constants
defined using #define with their values. The preprocessor also removes the comments.
2. Assembly code: The expanded source code file produced by the C++ preprocessor
is compiled into the assembly language for the platform.
3. Object Code: The assembler code generated by the compiler is assembled into the
object code for the platform.
4. Linker : The object code file generated by the assembler is linked together with the
object code files for any library functions used to produce an executable file.
9.
10. C++ Language
C++ evolved from C. C++, an extension of C was developed by Bjarne Stroustrup
in the early 1980's at Bell Laboratories.
Earlier C++ was known as "C with Classes". The first complier for "C with Classes"
was called "Cfront".
In 1983 the name of the language was changed from "C with Classes" to "C++". The
++ operator in the C language is an operator for incrementing a variable.
In 1990 Borland's Turbo C++ compiler was released.
Currently there are two versions of C++ is available. The first is the traditional version
based on Stroustrup's originals designs. The second is Standard C++, which was
created by Stroustrup and the ANSI / ISO standardization committee.
The major difference between the two are : new style headers and namespace
International Organization for Standardization
American National Standards Institute
11. // Old Style
#include <iostream.h>
int main()
{
return 0;
}
// New Style
#include<iostream>
using namespace std;
int main()
{
return 0;
}
The new style headers do not specify the header filenames. Instead they simply
specify standard identifiers that may be mapped to files by the compiler
Namespace
Namespace is simply a declarative region. The purpose of a namespace is
to localize the names of identifiers to avoid name collisions.
12. Standardization of C++
From 1983 to 1998 C++ was developed by Bjarne Stroustrup in Bell Labs.
In 1998, the ISO working group standardized C++ for the first time and it is known
as C++98. After that four new standards were released in 2003, 2007, 2011
and 2014. The 2014 release is known as C++14. Currently, the work is going on
for the next release in 2017.
The C++ Programming Language was the first book to describe C++, it was
written by Bjarne Stroustrup and till the standardization of C++ in 1998. This
book remained the de facto of C++.
First edition published in 1985
Second edition published in 1991
Third edition published in 1997 and 2000
Fourth edition published in 2013, which incorporates the C++11.
13. Compilers for C++
1. Turbo C++ / Borland C++
Borland is a company that created the most successful compiler for C++ in late
90's. Turbo C++ was an 16 bit application which ran on DOS. Turbo C++ comes
with an IDE.
Four versions of Turbo C++ was introduced from 1990 to 1994. After the ver 3.0
Turbo C++, Borland segmented their C++ compiler into two product lines:
Turbo C++ and Borland C++. Turbo C++ was for hobbyist and Borland C++ was
for professionals.
The original Turbo C++ was put on hold after 1994. In 2006, again Turbo C++
was released in two variants "Explorer" and "Professional". In 2008 Borland sold
its compiler division to "Embarcadero Technologies" and in 2009 Embarcadero
discontinued the Turbo C++ 2006.
Borland C++ was continued till version 5.02 (1997). After 1997, the Borland
started work on C++ Builder Series. Today Borland C++ 5.5 (Command Line only)
is available for download from the Embarcadero website. The C++ Builder latest
version XE8 (22) is available for download from the Embarcadero Website.
14. 2. DOSBox
TurboC++ ver3.0 / BorlandC++ ver 3.0 are 16 bit compilers, which can run only
on legacy systems like windows 98 and windows XP. To run the ver3.0 compilers
on windows 7 and windows 8, we need to install the DosBox (emulator program
that emulates the environment of DOS windows 7 and 8).
First , install DosBox and then install Turbo C+9 or Borland C++ ver 3.0 on it.
You can also install other old Dos based software in DOSBox
15. 3. GNU Compiler Collection (GCC)
GNU (General Public License) Project is a free software, mass collaboration project
started in 1983 at MIT. GCC is a compiler system produced by the GNU project
supporting various programming languages.
GCC ver 1.0 was released in 1987, originally only for language C. Extended in same
for C++. Later the support for Java, Fortran, Ada and Go was added.
The standard compiler releases since 4.6 include front ends for C (gcc), C++ (g++)
and Java (gcj).
The latest version is 5.2.0 released on July 2015.
4. MinGw
It is an open source development environment targeting Microsoft windows application.
It includes a port of the GNU Compiler Collection (GCC). Using MinGw the
compiler for C, C++, Java and other languages can be downloaded and it uses
the GCC in background.
16. 5. CODEBLOCKS
Code Blocks is a free C, C++ and Fortran IDE built to meet the most demanding
needs of its users. It is designed to be very extensible and fully configurable. It can
use GCC, Borland C++ 5.5, MSVC ++ etc to compile the programs.
6. Dev C++
Bloodshed Dev-C++ is a full-featured Integrated Development Environment
(IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU
Compiler Collection) as it's compiler. Dev-C++ can also be used in combination
with Cygwin or any other GCC based compiler.
7. MS VC++
It is an C++ compiler available from Microsoft. It is available for both 32 bit and 64 bit.
It provides a very easy to use IDE on windows machine. The latest one is
VC ++ 2015.
17. S.N
o
Compiler Company Paid /
Free
Platfor
m
IDE C++11
1 C++
Builder
Embarcader
o
Paid Win, OS
X,
Yes No ( via
Clang)
2 Borland
C++ 5.5
Borland /
Embarcader
Free Win Command
Line
No
3 GCC GNU
Project
Free Win,
Unix
Code
Blocks, Net
Beans
Yes
4 Visual C++ Microsoft Paid Window
s
Yes Partial
5 MinGw Under GNU Free Window
s
No Yes
6 Dev C++ BloodShed /
Orwell
Free Window
s
Yes Yes
7 CodeBlock CodeBlock Free Win,
Linux
Yes Yes
18. C++ Standard Libraries
In the C++ programming language, the C++ Standard Library is a collection of
classes and functions, which are written in the core language and part of the C++
ISO Standard itself.
The C++ Standard Library provides several generic containers, functions to utilize
and manipulate these containers.
Example of Containers:
<array>
<list>
<map>
<queue>
The C++ Standard Library is based upon conventions introduced by the Standard
Template Library (STL), and has been influenced by research in generic
programming
19. C++ Standard Libraries
In old C++ style header files are included with the extension of .h , but in new
standard style .h extension is removed. Also the C header files like stdio.h , string.h
are written as cstdio and cstring.
The facilities of the standard library are defined in the std namespace and presented
as a set of headers. Below is the brief list of header files given on the basis of
functionality.
Container : <vector> <map> <set> <deque> <list> etc
General Utilities : <ctime> <iterator> <memory>
Diagnostics : <exception> <stdexcept> <cassert>
String and Character : <string> <cstring> <cstdlin> <cctype>
Input / Output : <iostream> <ostream> <iomanip>
Localization : <locale> <clocale>
Language Support : <limits> <new> <ctime>
20. Important Points about new C++ Standard
1. The main only returns int value.
2. Use "system("cls")" instead of clrscr() in Dev C++ and CodeBlock. In CodeBlock
include <cstdlib> to use system(cls).
3. Use "system("pause") instead of getch() in both Dev C++ and Code Block.
In Code Block we can also use to "cin.get()"
21. Moving from C to C++
#include <iostream.h>
#include <conio.h>
void main()
{
cout<<"Hello World";
}
22. 1. Output Streams
Output on the standard stream is performed using the cout object.
Cout<<"Hello World".
The word cout is followed by the symbol <<, called the insertion or put-to operator.
Cout << variablename << variablename;
24. Write a program to calculate simple interest and Total amount
25. #include<iostream.h>
void main()
{
int principle,time,rate,si,total;
cout<<"Enter prinicipal";
cin>>principle;
cout<<"Enter Time(in years)";
cin>>time;
cout<<"Enter rate of interest";
cin>>rate;
si = (principle * time * rate ) / 100;
cout<<"Simple Interest = ";
cout<<si;
total = principle + si;
cout<<"Total amount = ";
cout<<total;
}
26. 3. How to declare constants
const [DataType] variablename = constantvalue
e.g.
const float PI = 3.1452;
const int TRUE = 1;
const int FALSE = 0;
Write a program to calculate the area of circle
We cannot change the value of constant variable and the value of constant
value is treated as read only.
27. #inlcude<iostream.h>
const float PI = 3.1452;
void main()
{
float radius;
float area;
cout<<"Enter radius of circle";
cin>>radius;
area = PI * radius * radius;
cout<<"Area of circle = " <<area;
}
28. 4. Scope Resolution Operator ::
To access a global variable from a function in which a local variable is defined
with the same name as global variable. It it achieved using the scope resolution
operator.
#inlcude<iostream.h>
int num = 20;
void main()
{
int num = 10;
cout<<"Local = "<<num;
cout<<"n Global = "<< ::num;
}
29. 5. Variable definition at the point of use
In C local variable can only be defined at the top of a function. In C++ local variable
can be defined at any position in the code.
#include <iostream.h>
int main()
{
for(int i = 0; i<5;i++)
{
cout<<i<<endl;
}
cout<<i;
return 0;
}
30. #include <iostream.h>
int a = 10;
void main()
{
cout<<a <<endl;
int a = 20;
{
int a = 30;
cout<<a<<endl;
cout<<::a<<endl;
}
cout<<a<<endl;
cout<<::a<<endl;
}
31. 6. Variable Aliases - Reference variables
In addition to value variable and pointer variable of C. C++ also supports the
reference variable. It acts as an alias for the other value variable. It does not
provide the flexibility supported by the pointer variable. When a reference is
bound to a variable, then its binding cannot be changed.
Syntax:
DataType & ReferenceVariable = ValueVariable;
32. #include <iostream.h>
void main()
{
int a =1, b = 2;
int &z = a;
cout<<" a = " <<a << " b = " << b<< "z = " <<z <<endl;
z = b;
cout<<" a = " <<a << " b = " << b<< "z = " <<z <<endl;
cout<<" &a = " <<&a << " &b = " << &b<< "&z = " <<&z
<<endl;
}
33. 7. Strict Type Checking
C++ is a strongly typed language and it uses very strict type checking. A prototype
must be known for each function which is called and the call must match the
prototype. In C++ function prototyping is compulsory whereas in C it is optional.
#include <iostream.h>
int main()
{
int x,y;
cout<<"Enter two numbers";
cin>>x>>y;
cout<<"Maxium = " <<max(x,y); // Compile time Error
return o;
}
int max(int a, int b)
{
if (a >b)
return a;
else
return b;
}
34. 8. Passing of Parameters by Reference
#include <iostream.h>
void swap (int & x, int &y)
{
int t;
t = x;
x=y;
y=t;
}
void main()
{
int a,b;
cout<<"Enter two integers";
cin>>a>>b;
swap(a,b);
cout<<"On swapping the numbers " << a << b;
}
35. 9. Inline Functions
Function execution involves the overhead of jumping to and from the calling
statement. When functions are small in size this overhead is unnecessary. So to
avoid this overhead, we can use inline functions which are treated as macros.
The function body is substituted at the point of inline function call.
#inlcude <iostream.h>
inline float square(float x)
{
return x*x;
}
void main()
{
float no;
cout<<"Enter the number ";
cin>>no;
cout<<"Its square is "<<square(no);
}
36. 10. Function Overloading
In C++ two or more functions can be given the same name provided each has a
unique signature ( in either the number or data type of their arguments).
#include <iostream.h>
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(char*c) {
cout << "Printing character: " << c << endl;
}
void print () {
cout<<"Empty print method";
}
void main()
{
print(5);
print("Hello C++");
print();
}
37. 11. Default Argument
In C++, the function can take default values if the values are not provided during the
function call.
#inlcude <iostream.h>
void showstring(char *str = "Hello World")
{
cout<<str;
}
int main()
{
showstring("function call with arguments");
showstring();
retrun 0;
}
38. 12. Functions as a Part of Struct
Like C structures, C++ structures also provide a mechanism to group different data
types. In addition to it also allows to associate functions as a part of structure.
struct StructName
{
public :
// data and functions
private:
/ / data and functions
protected:
// data and functions
}
All the data and functions are public by default.
39. 13. Runtime Memory Management
The memory management functions such as malloc(), calloc() and free() in C, have
been improved and evolved in C++ with the new and delete operators.
int *a = new int [100]; // In C++
int *a = (int *) malloc (sizeof (int) * 100);