Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Inheritance is the most promising concept of OOP, which helps realize the goal of constructing software

from reusable parts, rather than hand coding every system from scratch. Inheritance not only supports
reuse across systems, but also directly facilitates extensibility within a system. Inheritance coupled with
polymorphism and dynamic binding minimizes the amount of existing code to be modified while
enhancing a system.
When the class child, inherits the class parent, the class child is referred to as derived class (sub
class) and the class parent as a base class (super class). In this case, the class child has two parts: a derived
part and an incremental part. The derived part is inherited from the class parent. The incremental part is
the new code written specifically for the class child.
Dynamic binding:
Binding refers to linking of procedure call to the code to be executed in response to the call.
Dynamic binding(or late binding) means the code associated with a given procedure call in not known
until the time of call at run time.

Message passing:
An object oriented program consists of set of object that communicate with each other.
Objects communicates with each other by sending and receiving information .
A message for an object is a request for execution of a procedure and there fore invoke
the function that is called for an object and generates result

Benefits of object oriented programming (OOPs)

  Reusability: In OOP‟ s programs functions and modules that are written by a user can be reused by

 other users without any modification. 
  Inheritance: Through this we can eliminate redundant code and extend the use of existing classes.


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.

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 problem is solved by interfacing the objects. This technique reduces the
 
complexity of the program design.

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. Software complexity can be easily managed.
 
Message Passing: The technique of message communication between objects makes the interface

with external systems easier.


Modifiability: it is easy to make minor changes in the data representation or the procedures in an
OO program. Changes inside a class do not affect any other part of a program, since the only

public interface that the external world has to a class is through the use of methods.

BASIC STRUCTURE OF C++ LANGUAGE : The program written in C++ language follows this basic
structure. The sequence of sections should be as they are in the basic structure. A C program should have
one or more sections but the sequence of sections is to be followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section & class declarations
5.Member function definition
6. Main function
section main()
{
Declaration section
Executable section
}
1. DOCUMENTATION SECTION : comes first and is used to document the use of logic or
reasons in your program. It can be used to write the program's objective, developer and logic details. The
documentation is done in C language with /* and */ . Whatever is written between these two are called
comments.
2. LINKING SECTION : This section tells the compiler to link the certain occurrences
of keywords or functions in your program to the header files specified in this section.
e.g. #include<iostream>
using namespace std;

directive causes the preprocessor to add the contents of the iostream file to the program. It contains declarations

for cout and cin.

cout is a predefined object that represents the standard output stream. The operator << is an

insertion operator, causes the string in double quotes to be displayed on the screen.
screen

cout << “C++”


Object
Insertion Operator variable

The statement cin>>n; is an input statement and causes the program to wait for the user to type in a
number. The number keyed is placed on the variable “n”. The identifier cin is a predefined object in C++
that corresponds to the standard input stream. The operator >> is known as extraction operator. It extracts
the value from the keyboard and assigns it to the value variable on its right.

Object Extraction operator variable

cin >> 45.5

Keyboard

3. DEFINITION SECTION : It is used to declare some constants and assign them some
value. e.g. #define MAX 25
Here #define is a compiler directive which tells the compiler whenever MAX is found in
the program replace it with 25.
4. GLOBAL DECLARATION SECTION : Here the variables and class definations which are used
through out the program (including main and other functions) are declared so as to make them global(i.e
accessible to all parts of program). A CLASS is a collection of data and functions that act or manipulate
the data. The data components of a class are called data members and function components of a class are
called member functions
A class ca also termed as a blue print or prototype that defines the variable or functions common to
all objects of certain kind. It is a user defined data type

e.g.

int i; //this declaration is done outside and before main()

5. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or the functions which
our program needs.
void display()
{
cout<<”C++ is better that C”;
}
SIMPLE „C++‟ PROGRAM:
#include<iostream>
using namespace std;
void display()
{
cout<<”C++ is better that C”;
}
int main()
{
display()
return 0;
}

6. MAIN FUNCTION SECTION : It tells the compiler where to start the execution
from main()
{
point from execution starts
}
main function has two sections
1. declaration section : In this the variables and their data types are declared.
2. Executable section or instruction section : This has the part of program which actually performs
the task we need.

namespace:
namespace is used to define a scope that could hold global identifiers.
ex:-namespace scope for c++ standard library.
A classes ,functions and templates are declared within the namespace named
std using namespace std;-->directive can be used.

user defined name space:


syntax for defining name space is

namespace namespace_name
{
//declarations of variables.functions,classes etc...
}
ex:
#include<iostream>
using namespace std;
namespace sample
{`
int m;
void display(int n)
{
cout<<"in namespace N="<<n<<endl;
}
}

using namespace sample;


int main()
{
int a=5;
m=100;
display(200);
cout<<"M in sample name space:"<<sample::m;
return 0;}

#include<iostream>
This directive causes the preprocessor to add content of iostream file to the program.
some old versions of C++ used iostream.h .if complier does not support ANSI
(american nation standard institute) C++ then use header file iostream.h

DATA TYPES:
A data type is used to indicate the type of data value stored in a variable. All C compilers support a
variety of data types. This variety of data types allows the programmer to select the type appropriate
to the needs of the application as well as the machine. ANSI C supports the following classes of data
types: 1.Primary (fundamental) data types.
2.Derived data types.
3.User-defined data types C++ data types

Primary data types Derived data types User defined data types

int
Array
Pointer Structure
char Reference Union
Class
float enumeration

double

bool

void

Primary data types:


1.integer data type
2.character data type
3.float point data type
4.Boolean data type
5.void data type
integer data type:-
This data type is used to store whole numbers. These numbers do not contain the decimal part. The size of the
integer depends upon the world length of a machine (16-bit or 32-bit). On a 16-bit machine, the range of integer
values is - 32,768 to +32,767.integer variables are declared by keyword int. C provides control over range of integer
values and storage space occupied by these values through the data types: short int, int, long int in both signed and
unsigned forms.

Signed integers: (16-bit machine):


A signed integer uses 1 bit for sign and 15 bits for the magnitude of the number

MSB(most significant bit)


=
100(10) 00000000001100100(2)
Representation of negative number :

-100(10)=1111111110011100(2)

15 14 13 12 11 10 9 8 7 -1*2 +1*2 +1*2 +1*2 +1*2 +1*2 +1*2 +1*2


+1*2 +
6 5 4 3 2 1 0

0*2 +0*2 +1*2 +1*2 +1*2 +0*2 +0*2


= -32768+16384+8192+4096+2048+1024+512+256+128+0+0+26+8+4+0+0 =-
100(10)
NOTE: Signed bit (MSB BIT): 0 represents positive integer, 1 represents negative numbers

Unsigned integers: Unsigned integers use all 16 bits to store the magnitude. Stores numbers does not have any
sign & Size qualifier and range of integer data type on a 16-bit and machine are shown in the table:
MEMORY REQUIRED
RANGE
OR STORAGE SIZE IN BYTES FORMAT
DATA TYPE TURBO C GCC/ COMPILERS TURBO C GCC SPECIER
IN LINUX
( 16 BIT) ( 16 BIT) (32 BIT)
(32 BIT)
short int -32768 -32768
To To
or 2 2 32767 32767 %hd
15 15 15 15
signed short int (-2 to +2 -1) (-2 to +2 -1)
short int
0 to 65535 0 to 65535
or 2 2 (0 to +2 -1) (0 to +2 -1) %hu
signed short int
signed int -32768 -2,147,843,648 %d
To to
or 2 4 32767 2,147,843,647 or
15 15 31 31
int (-2 to +2 -1) (-2 to +2 -1) %i
unsigned int 0 to 65535 0 to 4,294,967,295
16 32
2 4 (0 to +2 -1) (0 to2 -1 ) %u
long int -2,147,843,648 -2,147,843,648
or to to
signed long int 4 4 2,147,843,647 2,147,843,647 %ld
31 31 31 31
(-2 to +2 -1) (-2 to +2 -1)
unsigned long int 0 to 4,294,967,295 0 to 4,294,967,295
32 32
4 4 (0 to2 -1 ) (0 to2 -1 ) %lu
long long int -9223372036854775808
or
Not To
signed long long supported 8 -------- 9223372036854775807 %Ld
63 63
int (-2 to +2 -1)

Character data type: (char)


A single character can be defined as a character data type. Character data type occupies one byte of
memory for storage of character. The qualifiers signed or unsigned can be applied on char data type.
char is the key word used for declaring variables
size and range of character data type on 16 bit or 32 bit machine can be shown below

Data type MEMORY REQUIRED RANGE FORMAT SPECIER


OR STORAGE SIZE (in bytes)
7
char or signed char 1 -128 to 127(-2 7 to 2 -1) %c
Unsigned signed char 1 0 to 256 (0 to 2 -1) %c

Floating Point Types:

Floating point number represents a real number with 6 digits precision occupies 4 bytes of memory.
Floating point variables are declared by the keyword float.
Double floating point data type occupies 8 bytes of memory giving 14 digits of precision. These are
also known as double precision numbers. Variables are declared by keyword double
long double refers to a floating point data type that is often more precise than double precision.

You might also like