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
}

   

C++ if statement

 

 

 

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
}
 

C++ if...else statement

 

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C++ switch statement

 

C++ Loop Types

A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages:

 

Loop Architecture

 

While loop

A while loop statement repeatedly executes a target statement as long as a given condition is true.

·         Syntax:

The syntax of a while loop in C++ is:

while(condition)
{
   statement(s);
}

 

 

C++ while loop

 

 

 

for loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

·         Syntax:

The syntax of a for loop in C++ is:

for ( init; condition; increment )
{
   statement(s);
}

C++ for loop

 

 

do...while loop

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

·         Syntax:

The syntax of a do...while loop in C++ is:

do
{
   statement(s);
}while( condition );

C++ do...while loop

 

 

 

 

 

 

 

 

 

 

 

 

 

Unit-05/Lecture-03

 

Functions

·         A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

·         A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

 

Defining a Function:

The general form of a C++ function definition is as follows:

return_type function_name( parameter list )
{
   body of the function
}

C++ function definition consists of a function header and a function body. Here are all the parts of a function:

·         Return Type.

·         Function Name

·         Parameters

·         Function Body

 

C++ Friend Functions

 

·         A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class.

·         A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

 

class Box
{
   double width;
public:
   double length;
   friend void printWidth( Box box );
   void setWidth( double wid );
};

 

 

C++ Inline Functions

 

·         C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

·         Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality.

·         To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.

 

·         A function definition in a class definition is an inline function definition, even without the use of the inline specifier.

 

 

 

 

 

 

 

Unit-05/Lecture-04

Exception Handling

 

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

 

·         throw: A program throws an exception when a problem shows up. This is done using a throwkeyword.

 

·         catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

 

·         try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

·          

Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

 

 

try
{
   // protected code
}catch( ExceptionName e1 )
{
   // catch block
}catch( ExceptionName e2 )
{
   // catch block
}catch( ExceptionName eN )
{
   // catch block
}

 

 

 

C++ Templates

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

·         Function Template:

The general form of a template function definition is shown here:

template <class type> ret-type func-name(parameter list)
{
   // body of function
} 

 

 

·         Generic functions

·         Generic classes

 

Generic Functions

 

¢  A generic function defines a general set of operations that will be applied to various types of data

¢  Allows to create a function that that can automatically overload itself !!!

¢  Allows to make the data type, on which to work, a parameter to the function

 

 

¢  General form

 

  template <class Ttype1, class Ttype2, …, class TtypeN>

  ret-type func-name(param list)

  {

     // body of function

  }

 

 

          Here,

·         template is a keyword

·         We can use keyword “typename” in place of keyword “class”

·         “TtypeN” is the placeholder for data types used by the function

 

 

¢  The compiler generates as many different versions of a template function as required

¢  Generic functions are more restricted than overloaded functions

  Overloaded functions can alter their processing logic

  But, a generic function has only a single processing logic for all data types

 

Generic Classes

 

¢  Makes a class data-type independent

¢  Useful when a class contains generalizable logic

  A generic stack

  A generic queue

  A generic linked list etc. etc. etc.

 

 

¢  The actual data type is specified while declaring an object of the class.

 

¢  General form

 

 

  template <class Ttype1, class Ttype2, …, class TtypeN>

  class class-name

  {

     // body of class

  };

 

 

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)

1.       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.

 

 

 

 

 

S.NO

RGPV QUESTIONS

Year

Marks

Q.1

Explain about JVM?

JUNE 2014

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Unit-05/Lecture-06

 

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

 

 

 

 

 

 

Java Exceptions

 

 

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.

 

 

S.NO

RGPV QUESTIONS

Year

Marks

Q.1

Define interface in java? How it is implemented? How can they be accessed?

Dec , 2003

10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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 (publicprivate) 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

  1. Java has no explicit memory reclamation operators--garbage collection used instead.
  2. Java has no explicit pointers
  3. Java eliminates value (i.e., stack-allocated) objects--all objects are allocated off the heap.

 

 

 

 

 

S.NO

RGPV QUESTIONS

Year

Marks

Q.1

Compare c++with java

June 2014

3

 

Q.2

What is the purpose of package?

June 2014

2

 

 

 

 

 

 

 

 

 

Back To Home