|
UNIT – 5 |
|
INTRODUCTION |
|
Unit-05/Lecture-01 |
|
·
What is C++? ·
Structure of C++ ·
Similarity and differences between structure of C
and C++. ·
What is Java? ·
Pakages on Java ·
Interface in Java ·
Differences between structure of Java and C++. What is C++? ·
C++ is a statically typed, compiled,
general-purpose, case-sensitive, free-form programming language that supports
procedural, object-oriented, and generic programming. ·
C++ is regarded as a middle-level language, as it comprises a
combination of both high-level and low-level language features. ·
C++ was developed by Bjarne Stroustrup starting in
1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C
language and originally named C with Classes but later it was renamed C++ in
1983. ·
C++ is a superset of C, and that virtually any legal
C program is a legal C++ program. Object-Oriented Programming Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their
interactions to design applications and computer programs. Programming
techniques may include features such as encapsulation, modularity,
polymorphism, and inheritance. C++
fully supports object-oriented programming, including the four pillars of
object-oriented development: ·
Encapsulation ·
Data hiding ·
Inheritance ·
Polymorphism Standard
Libraries
Standard C++ consists of
three important parts: ·
The core language giving all the building blocks
including variables, data types and literals, etc. ·
The C++ Standard Library giving a rich set of
functions manipulating files, strings, etc. ·
The Standard Template Library (STL) giving a rich
set of methods manipulating data structures, etc. Use
of C++
·
C++ is used by hundreds of thousands of programmers
in essentially every application domain. ·
C++ is being highly used to write device drivers and
other softwares that rely on direct manipulation of hardware under realtime
constraints. ·
C++ is widely used for teaching and research because
it is clean enough for successful teaching of basic concepts. ·
Anyone who has used either an Apple Macintosh or a PC running Windows
has indirectly used C++ because the primary user interfaces of these systems
are written in C++. Structure of c++ A typical c++ program
contains four sections: ·
Include files ·
Class declaration ·
Member function definition ·
Main function program. Similarity and differences between structure of c
and c++ ·
C follows the procedural programming paradigm while
C++ is a multi-paradigm language(procedural
as well as object oriented) ·
In case of C, the data is not secured while the data
is secured(hidden) in C++ ·
C is a low-level language while C++ is a
middle-level language ·
C uses the top-down approach while C++ uses the
bottom-up approach ·
C is function-driven while C++ is object-driven ·
We can use functions inside structures in C++ but
not in C. ·
C++ supports function overloading while C does not ·
C++ supports Exception Handling while C does not. ·
C++ allows the use of reference variables while C
does not |
|
Unit-05/Lecture-02
Decision making structures require that the programmer specify one or more conditions
to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to
be false. if
statement.
An if statement consists of a boolean expression
followed by one or more statements.
·
Syntax:
The syntax of an if statement in C++ is: if(boolean_expression) { // statement(s) will execute if the boolean expression is true }
if...else
statement.
An if statement can be followed by an optional
else statement, which executes when the boolean expression is false.
·
Syntax:
The syntax of an if...else statement in C++ is: if(boolean_expression) { // statement(s) will execute if the boolean expression is true } else { // statement(s) will execute if the boolean expression is false }
Switch
Statement
A switch statement allows a variable to be tested for
equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each case.
|
|
|
|
S.NO |
RGPV QUESTIONS |
Year |
Marks |
|
Q.1 |
What are the various
ways of handling exception?Which one is best? |
Dec, 2009 |
8 |
|
Unit-05/Lecture-05
Java is a high-level
programming language originally developed by Sun Microsystems and released in
1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX. Java is: ·
Object Oriented: ·
Platform independent: ·
Simple ·
Secure ·
Architectural-neutral ·
Portable ·
Robust ·
Multithreaded ·
Interpreted ·
High Performance ·
Dynamic Basic
Syntax:
·
Case Sensitivity ·
Class Names ·
Method Names ·
Program File Name - ·
public static void
main(String args[]) JVM (Java Virtual Machine)
2.
Internal Architecture of JVM JVM (Java Virtual
Machine) is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed. JVMs are available
for many hardware and software platforms (i.e.JVM is plateform dependent).
JVM It is: 1. A
specification where working of Java Virtual Machine is
specified. But implementation provider is independent to choose the
algorithm. Its implementation has been provided by Sun and other companies. 2. An
implementation Its implementation is known as JRE (Java
Runtime Environment). 3. Runtime
Instance Whenever you write java command on the
command prompt to run the java class, and instance of JVM is created. The JVM performs following operation: ·
Loads
code ·
Verifies
code ·
Executes
code ·
Provides
runtime environment JVM provides definitions for the: ·
Memory
area ·
Class
file format ·
Register
set ·
Garbage-collected
heap ·
Fatal
error reporting etc. |
||||||||
|
|
Java
Decision Making
There are two types of decision making statements in
Java. They are: ·
if statements ·
switch statements The
if Statement:
An if statement consists of
a Boolean expression followed by one or more statements. ·
Syntax:
The syntax of an if
statement is: if(Boolean_expression) { //Statements will execute if the Boolean expression is true } The
if...else Statement:
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false. ·
Syntax:
The syntax of an if...else
is: if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false } Nested
if...else Statement:
It is always legal to nest
if-else statements which means you can use one if or else if statement inside
another if or else if statement. ·
Syntax:
The syntax for a nested
if...else is as follows: if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true } } The
switch Statement:
A switch statement allows a variable
to be tested for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each case. ·
Syntax:
The syntax of enhanced for
loop is: switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements } |
|
|
|
Unit-05/Lecture-07 –
Exceptions
Handling
An exception is a problem
that arises during the execution of a program. An exception can occur for
many different reasons, including the following: ·
A user has entered invalid data. ·
A file that needs to be opened cannot be found. ·
A network connection has been lost in the middle of
communications or the JVM has run out of memory. How exception handling works in Java, you
need to understand the three categories of exceptions:
·
Checked
exceptions
·
Runtime
exceptions:
·
Errors:
Exception
Hierarchy:
The Exception class has
two main subclasses: IOException class and RuntimeException Class
Interfaces
·
An interface is a collection of abstract methods. A
class implements an interface, thereby inheriting the abstract methods of the
interface. ·
An interface is not a class. Writing an interface is
similar to writing a class, but they are two different concepts. A class
describes the attributes and behaviors of an object. An interface contains
behaviors that a class implements. ·
Unless the class that implements the interface is
abstract, all the methods of the interface need to be defined in the class. An interface is similar to a class in the following
ways: ·
An interface can contain any number of methods. ·
An interface is written in a file with a .java extension,
with the name of the interface matching the name of the file. ·
The bytecode of an interface appears in a .class file. ·
Interfaces appear in packages, and their
corresponding bytecode file must be in a directory structure that matches the
package name. However, an interface is different from a class in
several ways, including: ·
You cannot instantiate an interface. ·
An interface does not contain any constructors. ·
All of the methods in an interface are abstract. ·
An interface cannot contain instance fields. The
only fields that can appear in an interface must be declared both static and
final. ·
An interface is not extended by a class; it is
implemented by a class. ·
An interface can extend multiple interfaces. Declaring Interfaces:
The interface keyword is used to declare
an interface. Here is a simple example to declare an interface: Example:
Let us look at an example
that depicts encapsulation: /* File name : NameOfInterface.java */ import java.lang.*; //Any number of import statements public interface NameOfInterface { //Any number of final, static fields //Any number of abstract method declarations\ } Interfaces have the following properties: ·
An interface is implicitly abstract. You do not need
to use the abstract keyword when declaring an interface. ·
Each method in an interface is also implicitly
abstract, so the abstract keyword is not needed. ·
Methods in an interface are implicitly public.
|
|
Unit-05/Lecture-08 –
Packages
·
A java package is a group of similar types of
classes, interfaces and sub-packages. ·
Package in java can be categorized in two form, built-in package and
user-defined package. ·
There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc. Advantage of Java
Package
1) Java package is used to categorize the classes
and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision. Simple example of java package
The package keyword is used to create a package in java. 1. //save as Simple.java 2. package mypack; 3. public class Simple{ 4. public static void main(String args[]){ 5. System.out.println("Welcome to package"); 6. } 7. } Differences
between C++ and Java are: ·
C++ parsing is somewhat more complicated than with
Java; for example, Foo<1>(3); is a sequence of comparisons if
Foo is a variable, but it creates an object if Foo is the name of a class
template. ·
C++ allows namespace level constants, variables, and
functions. All such Java declarations must be inside a class or interface. ·
const in C++ indicates data to be 'read-only,'
and is applied to types. final in Java indicates that the
variable is not to be reassigned. For basic types such as const
int vs final
int these
are identical, but for complex classes, they are different. ·
C++ didn't support constructor delegation until the
C++11 standard, and only very recent compilers support this. ·
C++ generates machine code that runs on the
hardware, Java generates bytecode that runs on a virtual machine so with C++
you have greater power at the cost of portability. ·
C++, int main() is a function by itself,
without a class. ·
C++ access specification (public, private)
is done with labels and in groups. ·
C++ access to class members default to private,
in Java it is package access. ·
C++ classes declarations end in a semicolon. Memory Management
|