20CS204- PROGRAMMING
PARADIGMS-II
COURSE OUTCOMES
On successful completion of the course, the students will be able to
CO1: Relate the basic constructs in C++ and Java programming. -
CO2: Demonstrate OOP concepts with simple programs.
CO3: Develop simple applications in Java.
CO4: Analyze the real-life problem scenarios with object-oriented concepts.
Syllabus
Overview of OOPS Concepts 7
Structure of C++ program –Class and Object- Constructor- Inheritance- Functions: Virtual
Function, Friend function, Static function - Operator overloading – Templates
Java 8
Features of Java – Comparison of C, C++ with Java - Java Architecture- JDK, JRE, JVM -
Keywords and Operators – Control statements - Java Array – Unicode System – String and
StringBuffer.
OOP concepts in Java 10
OOP concepts - Class and Objects - Method, Constructors - this, static - instanceof Operator -
Inheritance vs Aggregation - super – final - Polymorphism: Method overloading, overriding –
Abstraction: Abstract class, Interface – Encapsulation: Package, Access modifiers.
Cont..
Exception Handling, Multithreading, File handling in Java 10
Exception Handling: Exception types - Try and Catch Block - Finally block –Throw, Throws, finally
clause – User defined Exception – Multithreading: Basics of a thread - Life cycle of a thread–
Creating and Running a Thread –– Thread control and priority – Synchronization and Inter-Thread
Communication - File handling and IO Streams.
GUI programming, JDBC, Collections framework, Wrapper class 10
GUI: Overview of Applet, Swing, AWT, Event Handling - JDBC: Introduction - JDBC Drivers –
Establishing connection – Statements - Collection Framework: Introduction – List: Types, Iterators –
Generics – Set – Map: Types - Wrapper classes.
TEXT BOOKS
1.Herbert Schildt, “C++: The Complete Reference”, 5th Edition, Tata McGraw- Hill, 2017.
2.Herbert Schildt, “The Complete Reference Java”, 11th Edition, McGraw- Hill Education,
2019.
REFERENCES
1. Lafore R,”Object Oriented Programming in C++”, Fourth Edition, SAMS, 2002.
2. Deitel H M and Deitel P J,”C++ How to Program”, Pearson Education, Fourth Edition,
2002.
3. Arnold, Gosling and Holmes, “The Java Programming Language”, Third Edition,
AddisonWesley, 2000.
4.Stroustrup B,”C++ Programming Language”, Special Third Edition, Addison Wesley
Professional, 2000.
WEB REFERENCES
1.https://nptel.ac.in/courses/106/105/106105191/
2.https://academy.oracle.com/pages/java_programming_course.pdf
3.https://www.coursera.org/specializations/core-java
What is the Importance of Programming
Languages?
It is important to automate, collect, manage, calculate, and analyze the processing of data and
information accurately.
Programming helps create software and applications that help computer and mobile users in
daily life
Difference
It is programmer friendly
It is a machine friendly language.
language.
High level language is less memory
Low level language is high memory efficient.
efficient.
It is easy to understand. It is tough to understand.
Debugging is easy. Debugging is complex comparatively.
It is simple to maintain. It is complex to maintain comparatively.
It is portable. It is non-portable.
It can run on any platform. It is machine-dependent.
It needs compiler or interpreter for
It needs assembler for translation.
translation.
It is used widely for programming. It is not commonly used now-a-days in programming.
OOPS CONCEPTS-An Overview of
C++
C++ was developed by Bjarne Stroustrup in the 1980 at Bell Labs.
The name C++ comes from the C increment operator ++, which adds 1 to the value of a
variable.
The C++ is an extensive version of language C.
Every C program can be consider as C++ Program but reverse is not true.
C++ supports for the Structured programming and also it fully supports OOP
C++ is also called as “C with classes“.
C++ is first popular object oriented programming language.
C++ runs on a variety of platforms, such as Windows,
Mac OS, and the various versions of UNIX.
Why C++?
•C++ is a general-purpose programming language that is a better C ,supports
data abstraction and supports object-oriented programming
Differences between C and C++ are:
C C++
C was developed by Dennis Ritchie between the year 1969
C++ was developed by Bjarne Stroustrup in 1979.
and 1973 at AT&T Bell Labs.
C does no support polymorphism, encapsulation, and C++ supports polymorphism, encapsulation, and
inheritance which means that C does not support object inheritance because it is an object oriented programming
oriented programming. language.
C is a subset of C++. C++ is a superset of C.
C contains 32 keywords. C++ contains 52 keywords.
C++ is known as hybrid language because C++ supports
For the development of code, C supports procedural
both procedural and object oriented programming
programming.
paradigms.
Data and functions are separated in C because it is a Data and functions are encapsulated together in form of an
procedural programming language. object in C++.
Data is hidden by the Encapsulation to ensure that data
C does not support information hiding.
structures and operators are used as intended.
Cont..
Built-in data types is supported in C. Built-in & user-defined data types is supported in C++.
Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.
C is a function-driven language. C++ is an object-driven language
Header file used by C is stdio.h. Header file used by C++ is iostream.h.
Reference variables are not supported by C. Reference variables are supported by C++.
Virtual and friend functions are not supported by C. Virtual and friend functions are supported by C++.
C does not support inheritance. C++ supports inheritance.
C provides malloc() and calloc() functions for dynamic C++ provides new operator for memory allocation and
memory allocation, and free() for memory de-allocation. delete operator for memory de-allocation.
Direct support for exception handling is not supported by
Exception handling is supported by C++.
C.
scanf() and printf() functions are used for input/output in C. cin and cout are used for input/output in C++.
Advantages of C++
❖C++ is a highly portable language
❖C++ is an object-oriented programming language
❖C++ provide performance and memory efficiency.
❖It provides a high-level abstraction.
❖C++ used reusability of code.
❖C++ used inheritance, polymorphism.
Example Program
#include <iostream.h>
int main() {
cout << "Hello, World!" << endl; // This prints Hello, World!
return 0;
}
Basic Concepts of OOPs
Class & Object
Class:
❖A class in C++ is the building block
❖It is a user-defined data type,
❖Which holds its own data members and member functions,
❖Which can be accessed and used by creating an instance of that class.
❖A C++ class is like a blueprint for an object.
Example
❖ Consider the Class of Cars.
❖There may be many cars with different names and brand but all of them will share some common
properties
❖ Like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.
Object
❖Any entity that has state and behavior is known as an object
❖An Object is an identifiable entity with some characteristics and behaviour.
❖An Object is an instance of a Class.
❖When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is
created) memory is allocated.
Syntax:
ClassName ObjectName;
Encapsulation
❖Encapsulation is defined as wrapping up of data and information under a single unit.
❖Encapsulation also leads to data abstraction or hiding. As using encapsulation also hides the data
For example: capsule, it is wrapped with different medicines.
Abstraction
❖Hiding internal details and showing functionality is known as abstraction.
❖Data abstraction refers to providing only essential information about the data to the outside world,
hiding the background details or implementation.
❖ For example: phone call, we don't know the internal processing.
Inheritance
❖Inheritance is a way to reuse once written code again and again.
❖The class which is inherited is called the Base class & the class which inherits is called the
Derived class. They are also called parent and child class.
❖So when, a derived class inherits a base class, the derived class can use all the functions which
are defined in base class, hence making code reusable.
Cont..
Sub Class: The class that inherits properties from another class is called Sub class or Derived
Class.
Super Class: The class whose properties are inherited by sub class is called Base Class or Super
class.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new
class and there is already a class that includes some of the code that we want, we can derive our
new class from the existing class. By doing this, we are reusing the fields and methods of the
existing class.
Example
Types of Inheritance
Polymorphism
❖The word polymorphism means having many forms.
❖ In simple words, we can define polymorphism as the ability of a message to be displayed in more
than one form.
Example
❖A person at the same time can have different characteristic
Cont..
C++ supports operator overloading and function overloading.
❑ Operator Overloading: The process of making an operator to exhibit different behaviors in
different instances is known as operator overloading.
❑ Function Overloading: Function overloading is using a single function name to perform
different types of tasks. Polymorphism is extensively used in implementing inheritance.
Dynamic Binding:
❖In dynamic binding, the code to be executed in response to function call is decided at runtime.
C++ has virtual functions.
Message Passing
❖Objects communicate with one another by sending and receiving information to each other.
❖ A message for an object is a request for execution of a procedure and therefore will invoke a
function in the receiving object that generates the desired results.
❖Message passing involves specifying the name of the object, the name of the function and the
information to be sent.
What is the advantage of OOP?
❖Simplicity: software objects model real world objects, so the complexity is reduced and the
program structure is very clear;
Modularity: each object forms a separate entity whose internal workings are decoupled from
other parts of the system;
❖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;
❖ Extensibility: adding new features or responding to changing operating environments can be
solved by introducing a few new objects and modifying some existing ones;
❖Maintainability: objects can be maintained separately, making locating and fixing problems
easier;
❖ Re-usability: objects can be reused in different programs
C++ Classes/Objects
❖C++ is a multi-paradigm programming language. Meaning, it supports different programming
styles.
❖One of the popular ways to solve a programming problem is by creating objects, known as
object-oriented style of programming.
❖C++ supports object-oriented (OO) style of programming which allows you to divide complex
problems into smaller sets by creating objects.
❖Object is simply a collection of data and functions that act on those data.
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and color, and
methods, such as drive and brake.
Attributes and methods are basically variables and functions that belongs to the class. These are
often referred to as "class members".
A class is a user-defined data type that we can use in our program, and it works as an object
constructor, or a "blueprint" for creating objects.
Structure of C++ Program
Class and Object-Syntax
The general syntax for declaration of a class as follows:-
Syntax
class classname
{
private:
data members;
member functions;
public:
data members;
member functions;
protected:
data members;
member functions;
};
Access Modifiers/Specifiers in C++
Access modifiers are used to implement an important feature of Object-Oriented Programming
known as Data Hiding.
Cont..
❖public - members are accessible from outside the class.
❖ private - members cannot be accessed (or viewed) from outside the class.
❖ protected - members cannot be accessed from outside the class, however, they can be accessed in
inherited classes.
Example
Create a class called person :
class person // The class
{ public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Public
❖Public class members and functions can be used from outside of a class by any function or other
classes.
❖ You can access public data members or function directly by using dot operator (.) or (arrow
operator-> with pointers).
Private
Private class members and functions can be used only inside of class and by friend functions and
classes.
Example
Member Functions in C++:
❖Driving programs into a function are one of the major principles of programming. These are
known as “Member functions” in C++.
❖They have their deceleration’s inside the class definitions. And these member functions work on
the data member of the class.
❖ While defining if the member function is inside the class definition, then it can be defined
directly.
❖In case if it is defined outside of the class, then we should use the special operator known as
“scope resolution operator (::)” will be used along with class name and the function name.
Inside the Member Function: Example
Syntax
class Sq {
public:
int a;
int square()
{
return a*a;//returns square of a number
}
};
Example
Defining member function outside of the
class definition
A public member function can also be defined outside of the class with a special type of
operator known as Scope Resolution Operator (SRO); SRO represents by :: (double colon)
return_type class_name::function_name(parameters)
{
function_body;
}
Comparisons of class and object
Assignment
Create a class student with data members like roll no and mark percentage and also declare two
member functions inputData( ) and displayData( ) which operate on data members. Display two
student’s of roll no. and mark percentage.