Notes_CSE2001_OOP_with_C++_Unit I
Notes_CSE2001_OOP_with_C++_Unit I
Syllabus:
Learning Objectives:
Object Oriented programming is a programming style that is associated with the concept
of Class, Objects and various other concepts revolving around these two, like
Inheritance, Polymorphism, Abstraction, Encapsulation etc.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Why OOP?
Objects
Objects are the basic unit of OOP. They are instances of class, and have data members
and various member functions to perform tasks.
Class
It is similar to structures in C language. Class can also be defined as user defined data
type but it also contains functions in it. So, class is basically a blueprint for object. It
declare & defines what data variables the object will have and what operations can be
performed on the class's object.
Abstraction
Abstraction refers to showing only the essential features of the application and hiding
the details. In C++, classes provide methods to the outside world to access & use the data
variables, but the variables are hidden from direct access. This can be done access
specifiers.
Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables and
functions together in class.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is
inherited is called base calls & the class which inherits is called derived 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.
Polymorphism
It is a feature, which lets us create functions with same name but different arguments,
which will perform differently. That is function with same name, functioning in different
way. Or, it also allows us to redefine a function to provide its new definition. You will
learn how to do this in details soon in coming lessons.
Exception Handling
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Instances
A blueprint for a house design is like a class description. All the houses built from that
blueprint are objects of that class. A given house is an instance.
The major difference being OOPS concept, C++ is an object oriented language whereas C
language is a procedural language. There are many other features of C++ which gives
this language an upper hand on C language.
2. All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes
it more worthy and useful for programmers.
3. C++ supports and allows user defined operators (i.e Operator Overloading) and
function overloading is also supported in it.
5. The Concept of Virtual functions and also Constructors and Destructors for Objects.
Variables can be declared anywhere in the program in C++, but must be declared before
they are used.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
UML, short for Unified Modeling Language, is a standardized Modeling language
consisting of an integrated set of diagrams, developed to help system and software
developers for specifying, visualizing, constructing, and documenting the artifacts of
software systems, as well as for business Modeling and other non-software systems. The
UML represents a collection of best engineering practices that have proven successful in
the modeling of large and complex systems. The UML is a very important part of
developing object oriented software and the software development process. The UML
uses mostly graphical notations to express the design of software projects. Using the
UML helps project teams communicate, explore potential designs, and validate the
architectural design of the software. In this article, we will give you detailed ideas about
what is UML, the history of UML and a description of each UML diagram type, along with
UML examples.
The primary goals in the design of the UML in Fundamental Object-Oriented Design in
UML as follows:
The class diagram is a central modeling technique that runs through nearly all object-oriented
methods. This diagram describes the types of objects in the system and various kinds of static
relationships which exist between them.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Relationships
Inheritance - the most obvious addition to ER diagrams for use in OO. It has an immediate
correspondence to inheritance in OO design.
https://www.visual-
paradigm.com/support/documents/vpuserguide/94/2576/7190_drawingclass.html
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Class Diagram for proposed program for Toy store reception
Functions:
C++ supports two styles of passing arguments: “pass by value” and “pass by
reference”. They are also called as “call by value” and “call by reference”
respectively.
Argument is the value or reference passed from a program to the function. The calling
program gives arguments to the function. On the contrary the local variables used
within the function to hold the argument values are called parameters. Sometimes
these two terms are used interchangeably.
If the argument is passed as value to the function, the parameter receives a copy of
argument value. Any changes made by function to parameter value will not affect the
argument (shown by Program 1). Example Program 1 is showing the example of
Pass / Call by value.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Program 1: args.cpp
Output
p = 0
x = 101
Annotation
In the Program 1 above, the demo ( ) is the function and the single parameter p is
value parameter. When the function call is made the argument X is passed to it, p
receives the copy of the value of x. Here p is set to 0 by the function but it does
not affect x (see Output).
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Program 2: increment.cpp
Output
y = 6.7 x = 40
In the Program 2 above, the type of each parameter in declaration was followed by an
ampersand sign (&). This ampersand specifies that their corresponding arguments are to
be passed by reference.
any change by called function in reference parameters a and b, will appear in original
variable (x and y)in main program.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Our program’s output shows that the values in x and y are increased by 1 and 2
respectively after calling increment function.
To explain it in another way, we associate a and b with the arguments passed on the
function call (y and x) and any change that we do on a and b within the function will
affect the value of y and x outside it.
o where we want to return more than one value from called function.
o when we want access to the actual variables in the calling program (for
manipulation).
Inline Functions
The inline keyword is used in declaration before the return type and it should
not be included when calling the function. Following is the syntax
Statements;
return statement;
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Program 3: inlinef.cpp
int main ( )
{
float dist, time ;
cout << " Enter distance in kms: " ;
cin >> dist ;
cout << "\n";
cout << "Enter time in hours: ";
cin >> time;
cout << ” Speed= ” << speed ( dist, time ) << " kms/h " ; //function call
getch ( ) ;
return 0 ;
}
Output
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Here y has a default argument of 2.5; the following calls to function are therefore
valid:
The first call illustrates that the value for the second parameter is not passed, so
default value is used. In second call the value specified can take priority over the
default argument. The Program 4 demonstrates the use of default parameters.
o A default argument need not necessarily be a constant i.e. global variable can be used.
Program 4: multiply.cpp
Output
30
13.6
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Important Points about functions:
- Function is a group of instructions to carry out specific task, at the point of program
where it is called.
- The function definition consists of two parts: prototype and body. The prototype (also
called interface) of a function specifies how it may be used.
- User defined functions can be stored as library functions of C++ and used when
required by program.
- Return data type is the type specifier of the data returned by the function.
- C++ supports two styles of passing arguments: “pass by value” and “pass by reference”.
- The calling program gives arguments to the function and the local variables used
within the function to hold the argument values are called parameters.
- If the argument is passed as value to the function, the parameter receives a copy of
argument value. In this case any changes made by function to parameter value will not
affect the argument
- A reference parameter receives the argument passed to it and works on it directly. Any
changes made by the function to any reference parameter will directly apply to the
argument.
- C++ programs start their execution from main function. As the main function is the
entry point for the program, so it is essential that all C++ programs must have
a main function. It returns an integer exit code generally to operating system.
- The void specifier confirms the absence of type. If we want to return no value,
the void type specifier for the function is used.
- Function Overloading simply defined as: different functions with the same name but
with varying number of different arguments.
- By defining a function as inline the actual function code is inserted, instead of a jump
to the function. Inline functions can increase the performances.
- Functions for basic mathematical operations are kept in standard library under the
math.h header file.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
- The function which calls itself is called as recursive function. Recursive function should
have at least one termination condition which can be satisfied, otherwise, the function
will call itself indefinitely.
2. Write a program that defines a class Box and instantiate the objects of class Box with
different dimensions (volume etc.).
3. Write a program to demonstrate the concept of public access specifier and access the
public member of class.
4. Write a program for Toy store reception using Class Diagram shown in Fig. 4.
Procedural Programming
Class diagram.
Analysis modelling with analogy of Toy Store software.
Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University