Thursday 23 May 2013

Introduction of OOPs and Revision of basics


History of programming approaches
l  At each critical point in the evolution of programming, a new approach was created to help the programmer handle increasingly complex programs.
l  The first programs were created by toggling switches on the front panel of the computer.
l  This approach is suitable for only the  smallest programs
l  Next, assembly language was invented, which allowed longer programs to be written. 
l  The next advance happened in the 1950s when the first high level language (FORTRAN) was invented.
l  By using high level programming language, a programmer was able to write programs that were several thousand lines long. However the method of programming was ‘anything’ goes approach. It yields unreadable and unmanageable code.
l  Next invention is structured programming languages in the 1960s
l  C is a structured programming language as it contains control structures, code blocks and absence of goto (or at least minimal use)
l  To allow more complex programs to be written, a new approach of programming is needed. So the object oriented programming was invented.
l  OOP takes the best of the ideas embodied in structured programming and combines them with powerful new concepts that allow you to organize your program more effectively.

Inventions of programming languages
l  In 1940s, Assembly language was invented which is a machine level language; also it comes under low level language.
l  In 1950s, FORTRAN was invented. It is first high level programming language that is machine independent language.
l  In 1966, BCPL was invented by Martin Richards. BCPL stands for Basic Combined Programming Language.
l  In 1969, B language was developed by Ken Thompson. He is also know as developer of UNIX operating system.
l  In 1972, C language was developed by Dennis Ritchie at AT &T’s Bell Lab, New Jersy USA.
l  In 1979, C++ language was developed by Bjarne Stroustrup. Initially C++ was pronounced as C with classes.

Some Points about C++
l  C++ was developed by Bjarne stroustrup in the year 1979 at AT and T’s Bell Labs, New Jersey USA.
l  C++ is an enhancement in C, so it consists of all the features that C provides.
l  Initially C++ was known as ‘C with classes’.
l  In the year 1983, it is named as C++.
l  C++ is also a middle level language just like C.

Differences between C and C++

1) When a function takes no parameter, its prototype has the word void inside parenthesis.
int fun(void);
However, in C++ the void is optional.
int fun();
If this convention would use in C, it mean that nothing is said about the parameters.

2) In a C++ program, all functions must be prototyped. In C, prototypes are recommended but technically optional.

3) About keyword return:
In C++, if a function is declared as returning a value, that is return type other than void, any return statement within that function must contain a value.
In C, a non void function is not required to actually return a value. If it doesn’t, a garbage value is returned.

4) Comments:
In C, comments in the program can be specified by /*….*/
In C++, due to backward compatibility, /*….*/ is still used for multiple line comments. We can also make a single line as comment by prefix that line by //

5) Declaration of local variables
In C local variables can be declared only at the start of a block prior to any action statements.
In C++ local variables can be declared anywhere in the block.

6) Reference variable
In C++, reference variable are implicit pointers that acts as another variable name for some existing variable. There is no concept of reference variable in C.

7) Top Down and Bottom up approach
C programmer follows top down approach for programming
It is a program design technique that starts with the highest level of an idea and works its way down to the lowest level of detail.
C++ programmer adopts bottom up approach for programming.
In a bottom-up approach the individual base elements of the system are first specified in great detail. These elements are then linked together to form larger subsystems, which then in turn are linked, sometimes in many levels, until a complete top-level system is formed.

8) Procedure oriented Programming and Object Oriented Programming
Procedure oriented Programming
Main program is divided into small parts depending on the functions.
The Different part of the program connects with each other by parameter passing. Most of the functions use global data.
Object Oriented Programming
Main program is divided into small objects depending on the problem.
Data & functions of each individual object act like a single unit.
Each object controls its own data.
Data hiding is possible in OOP which prevent illegal access of function from outside of it. This is one of the best advantages of OOP
Input/Output Instruction
In C, standard input device is keyboard and scanf() is use to receive data from keyboard.
Also, standard output device is monitor and printf() is use to send data/message to monitor.
In C++, printf() can be replaced by ‘cout’:
l  cout<<”Hello ”;
l  This statement introduces two new C++ features, cout and <<.
l  cout is a predefined object.
l  The operator << is called the insertion or put to operator
l  It is used to display message on the screen.
Examples:
printf(“Hello ”);
cout<<“Hello ”;

printf(“sum of %d and %d is %d”, a, b, c);
cout<<“sum of ”<<a<<“ and “<<b<<“ is “<<c;

printf(“%d”,a+b);
cout<<a+b;

In C++, scanf() can be replaced by ‘cin’:
l  cin>>x;
l  cin>>y>>z;
l  The identifier cin is a predefined object in C++
l  The operator >> is known as extraction or get from operator
l  It is used to take input from keyboard.
Examples:
scanf(“%d”, &a);
cin>>a;
scanf(“%d%d”, &a, &b);
cin>>a>>b;

scanf(“%d%f”, &a, &c);
cin>>a>>c;

About header file iostream.h
We need to include header file iostream.h, as it contains declarations for the identifier cout and the operator <<.  Also it contains declarations for the identifier cin and operator >>.

Sample Program
#include<iostream.h>
void main()
{
            int x, s;
            cout<<”Enter a number”;
            cin>>x;
            s=x*x;
            cout<<”Square of “<<x<<” is “<<s;
}

1 comment:

  1. The header <iostream.h> is non-standard (it was used before the 1998 standardization of C++), so many if not most compilers will not accept the sample program code.

    Similarly, "void main" is non-standard – it should be "int main" – so many if not most compilers will not accept this code.

    Microsoft’s C++ compiler, Visual C++, does accept "void main" as a language extension. However, it accepts it silently, while the C++ standard requires a diagnostic. The Visual C++ compiler also fails to accept by default a standard "main" for a GUI subsystem application, i.e., it’s a bit non-standard in this department…

    ReplyDelete