0% found this document useful (0 votes)
8 views

Notes_CSE2001_OOP_with_C++_Unit I

The document provides an overview of Object Oriented Programming (OOP) concepts, focusing on characteristics such as classes, objects, encapsulation, inheritance, and polymorphism, as well as the merits of C++ over C. It also discusses UML and class diagrams, detailing the relationships between objects, and explains function concepts including inline functions and default arguments. Additionally, it covers parameter passing methods in C++, specifically 'pass by value' and 'pass by reference'.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Notes_CSE2001_OOP_with_C++_Unit I

The document provides an overview of Object Oriented Programming (OOP) concepts, focusing on characteristics such as classes, objects, encapsulation, inheritance, and polymorphism, as well as the merits of C++ over C. It also discusses UML and class diagrams, detailing the relationships between objects, and explains function concepts including inline functions and default arguments. Additionally, it covers parameter passing methods in C++, specifically 'pass by value' and 'pass by reference'.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Class Notes UNIT – I

Syllabus:

Introduction to Object Oriented approach: Why object oriented programming? - Characteristics


of object oriented language: classes and objects - encapsulation-data abstraction- inheritance -
polymorphism - Merits and Demerits of object oriented programming. UML- class diagram of OOP -
Inline function – default argument function- Exception handling (Standard) - reference:
independent reference – function returning reference – pass by reference

Learning Objectives:

• To understand the relative merits of C++ as an Object Oriented Programming


(OOP) language
• To understand the features / characteristics of C++ that supports OOP approach
• To demonstrate the concepts of UML and class diagram in OOP
• To demonstrate the concept of functions (inline, returning reference)

1. Introduction to Object Oriented approach

An Object Oriented Programming (OOP) is a way of programming which enables


programmers to think like they are working with real-life entities (a thing with distinct
and independent existence) or objects. In real-life, people have knowledge and can do
various works. In OOP, objects have fields to store knowledge/state/data and can do
various works — methods.

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.

Fig. 1. OOP Features

Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Why OOP?

Characteristics / Features (Definitions of OOPS Concepts)

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

Exception handling is a feature of OOP, to handle unresolved exceptions or errors


produced at runtime.

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.

Merits of C++ (OOP) over C Language (Procedural)

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.

Following features of C++ makes it a stronger language than C,

1. There is Stronger Type Checking in C++.

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.

4. Exception Handling is there in C++.

5. The Concept of Virtual functions and also Constructors and Destructors for Objects.

6. Inline Functions in C++ instead of Macros in C language. Inline functions make


complete function body act like Macro, safely.

Variables can be declared anywhere in the program in C++, but must be declared before
they are used.

Unified Modeling Language (UML)

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:

o Provide users with a ready-to-use, expressive visual modeling language so they


can develop and exchange meaningful models.
o Provide extensibility and specialization mechanisms to extend the core concepts.
o Be independent of particular programming languages and development
processes.
o Provide a formal basis for understanding the modeling language.
o Encourage the growth of the OO tools market.
o Support higher-level development concepts such as collaborations, frameworks,
patterns and components.
o Integrate best practices.

What is a Class Diagram?

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

There are three principal kinds of relationships which are important:

Association - represent relationships between instances of types (a person works for a


company, a company has a number of offices.

Inheritance - the most obvious addition to ER diagrams for use in OO. It has an immediate
correspondence to inheritance in OO design.

Aggregation - Aggregation, a form of object composition in object-oriented design.

Class Diagram Example

Fig. 2. Class Diagram

How to draw Class diagram using Visual Paradigm software?

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

Fig. 3. Class diagram for Toy store software

Functions:

Parameters and Arguments

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

// Program to demonstrate the parameters and arguments.

# include < iostream.h >


# include < conio.h >
void demo ( int ) ; //Function declaration or
prototype
int main ( )
{
int x =101;
demo ( x ) ; // x is passed as argument to
function
cout << “ x = ” << x ;
getch ( ) ;
return 0;
}
void demo ( int p ) //Function definition
{
p = 0; // a is a value parameter and behaves as local
variable
cout << “ p = ” << p << ‘\n’ ;
}

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

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. Refer the demonstration of reference parameter
passing in Program 2. Example Program 2 is showing the example of Pass / Call
by reference.

Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Program 2: increment.cpp

// Program to demonstrate the pass by reference.

# include < iostream.h >


# include < conio.h >
void increment ( float&, int& ) ; //Function declaration
int main ( )
{
float y = 5.7 ;
int x =38 ;
increment ( y, x ); // y and x are passed by reference to function
cout << “y = ” << y ;
cout << “ x = ” << x ;
getch ( ) ;
return 0 ;
}
void increment ( float& a, int& b ) //Function definition
{
a =a+1;
b =b+2; // a and b are reference parameters
}

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.

As reference to the original variable is passed when function call is made,

increment ( y, x ) ; // y and x are passed by reference to function

any change by called function in reference parameters a and b, will appear in original
variable (x and y)in main program.

void increment ( float& a, int& b ) //Function definition


{
a =a+1;
b =b+2; // a and b are reference parameters
}

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.

Thus Pass by reference is useful in the situations:

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

To understand the inline function, we recall that function is called wherever it is


required by the calling program. While compilation if function call is found, the
jump instruction is generated to access the function from memory and after that
compiler jumps back to the instruction following the call. As function body
stored only once in memory it is accessed each time a call is made to function.
This frequent calling to function is more time consuming if it is used lot of times
in the program. So, the better solution is to replace the function calls with the
function code itself and we can achieve this by defining the function as inline.

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 but
generally the use should be restricted to small, frequently used 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

inline datatype function_name ( parameter1, parameter2, ………………………….)


{

Statements;
return statement;

The Program 3 demonstrates the functionality of inline function.

Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Program 3: inlinef.cpp

# include < iostream.h >


# include < conio.h >

inline float speed ( float d, float t ) // inline function declared and


defined
{
return ( d / t );
}

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

Enter distance in kms: 45


Enter time in hours: 1.5
Speed = 30 kms/h
Annotation
The Program 3 is to calculate the speed of object by giving distance travelled (in
kms.) and time (in hrs). The keyword inline is written before the declaration
only and not before call. In output we entered 45 as distance and 1.5 as time to
calculate the speed.

Default Arguments (Parameters)

Default arguments / parameters are used for programming convenience. It removes


the burden of specifying values for all of a function’s parameters that almost have
same value.
For example, consider a function

float multiply ( int x, float y = 2.5 )

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:

multiply ( x ) ; // here y is set to its default value 2.5

multiply ( x , 3.4) ; // here y is set to 3.4

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.

Following points should be remembered for default parameters

o Specify default arguments in the function declaration not in function definition.

o To avoid ambiguity, all default arguments must be placed at trailing positions.

o A default argument need not necessarily be a constant i.e. global variable can be used.

Program 4: multiply.cpp

# include < iostream.h >


# include < conio.h >
float multiply ( int x, float y = 2.5 ) //function declared and defined
{
float r ;
r = x* y;
return ( r ) ;
}
int main ( )
{
clrscr ( ) ;
cout << multiply ( 12 ) << ’ \n ’ ; // function call without y
cout << multiply ( 4, 3.4 ) ; //function call with other value than default
getch ( ) ;
return 0 ;
}

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

- Argument is the value or reference passed from a program to the function.

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

- Default parameters are used for programming convenience.

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

- Recursion is a general programming phenomenon where a function calls itself.

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.

List of Programs covered in class:

1. Write a program to demonstrate the concept of class and objects.

(Instantiate the objects of class Student.)

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.

5. Program to demonstrate the concept of inline functions.

6. Program to demonstrate the concept of default arguments.

7. Program to demonstrate the concept of Pass-by-value in functions.

8. Program to demonstrate the concept of Pass-by-reference in functions.

Additional Topics covered in Class

 Procedural Programming
 Class diagram.
 Analysis modelling with analogy of Toy Store software.

Prepared by: Prof. Anand Motwani, Faculty, SCSE, VIT Bhopal University

You might also like