0% found this document useful (0 votes)
31 views10 pages

Ctuc103 Unit 01

The document provides an introduction to Object-Oriented Programming (OOP) and C++, detailing its concepts, structure, and benefits over procedural programming. It covers key OOP features such as classes, objects, inheritance, polymorphism, data abstraction, and encapsulation, as well as C++ specific functionalities like inline functions, default arguments, and call by reference. The document emphasizes the modular approach of OOP and the advantages it offers in program design and data management.

Uploaded by

bnfx4498jf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Ctuc103 Unit 01

The document provides an introduction to Object-Oriented Programming (OOP) and C++, detailing its concepts, structure, and benefits over procedural programming. It covers key OOP features such as classes, objects, inheritance, polymorphism, data abstraction, and encapsulation, as well as C++ specific functionalities like inline functions, default arguments, and call by reference. The document emphasizes the modular approach of OOP and the advantages it offers in program design and data management.

Uploaded by

bnfx4498jf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Smt.

Chandaben Mohanbhai Patel Institute of


Computer Applications
CHAROTAR UNIVERSITY OF SCIENCE AND TECHNOLOGY

B.Sc. (IT) (Semester-2)


CTUC103 || Introduction to Object-Oriented Programming

Unit-1: OOP Concept and Introduction to C++


 Introduction to C++
 Structure of C++ program
 Fundamental of OOP
 Characteristics of OOP
 Inline Function
 Pass by Reference
 Default Arguments
 Creating Namespace
Introduction to C++:
 C++, as we all know is an extension to C language and was developed by Bjarne
Stroustrup at Bell Labs in Murray Hill, New Jersey.
 Bjarne Stroustrup initially called the new language "C with Classes." However, in
1983 the name was changed to C++.
 C++ is a middle-level programming language.
 C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form
programming language that supports procedural, object-oriented, and generic
programming.
Benefits of C++ over C Language:
The major difference being OOPS concept, C++ is an object-oriented language
whereas C is a procedural language. Apart from this, many other features of C++ give this
language an upper hand on C language.
The following features of C++ make it a stronger language than C,
 There is Stronger Type Checking in C++.
 All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance, etc.
make it more worthy and useful for programmers.
 C++ supports and allows user-defined operators (i.e. Operator Overloading) and
function overloading is also supported in it.
 Exception Handling is there in C++.
 Variables can be declared anywhere in the program in C++ but must be declared
before they are used.

Structure of C++ Program:

Include Files

Class Declaration

Member Function
Definitions
Main Function Program

1|Page
 A typical C++ program contains four sections. The sections may be placed in
separate code files and then complied jointly or independently.
 It is common to organize a program into three separate files.
 The class declarations are placed in a header file and the definitions of member
functions are placed in another file. This approach enables the programmers to
separate the abstract specification (class definition) from the implementation
details.
 Finally, the main program that uses the class is placed in a third file which includes
the previous two files as well as any other files required.

Fundamental of OOP:
 Emphasis is on doing algorithms.
 Large programs are divided into functions.
 Most of the functions share the global data.
 Data move freely around the system from function to function.
 Functions transform data from one form to another.
 Follows top–down approach in program design.

Object Oriented Programming:


It is an approach that provides a way of modularizing programs by creating a partitioned
memory area for both the data and functions that can be used as templates for creating
copies of such modules on demand.
An object is considered to be a partitioned area of computer memory that stores data and
functions that can access the data.

Features of Object–Oriented Programming:


 Emphasis on data rather than procedure.
 Programs are divided into objects.
 Functions that operate on the data of an object are tied together in the data
structure.
 Data is hidden and cannot be accessed by external functions.
 Objects may communicate with each other through functions.

2|Page
 New data and functions can be easily added whenever needed.
 Follows bottom–up approach in the program design.

Difference between Structured Programming and Object Oriented Programming:

Structured Programming Object-Oriented Programming


1 Emphasis is on an algorithm. Emphasis is on data rather than
algorithms.
2 Large programs are divided into Large programs are divided into objects.
functions.
3 Data moves openly around the system Data is hidden and cannot be accessed by
from function to function. external functions.
4 Employs a top-down approach in the Follows bottom-up approach in the
program design. program design.
5 It is difficult to add new data and New data and functions can be easily
functions compared to OOP. added.
6 Eg. FORTRAN, COBOL Eg. JAVA, C++,SIMULA67,SmallTalk

Characteristics of OOP:
 Classes
 Objects
 Data Abstraction and Encapsulation
 Inheritance
 Polymorphism
 Dynamic Binding
 Message Passing
Classes
A class is a way to bind the data and its associated functions together. It allows the data
and functions to be hidden from external use. It is a user-defined data type and behaves
like a built–in type. It is also called the Abstract Data Types(ADT) because the classes use
the concept of data abstraction.

3|Page
Once a class has been defined, we can create any number of objects of that class. Each
object is associated with the data of the type class with which they are created. Thus a
class is a collection of objects of similar type.
Eg. roll_no and name are the members of the class STUDENT. It can be declared as follows:
class Student
{
private:
int roll_no;
char name[10];
public:
void getdata();
void putdata();
}

Objects:
Objects are the basic run-time entities in an Object–oriented system. The programming
problem is analysed in terms of objects and the nature of communication between them.
When the program is executed, the objects interact by sending messages to one another.
Example: If “customer” and “account” are two objects in a program, then the customer
object may send a message to the account object requesting the bank balance.
Each object contains data and code to manipulate the data. Objects can interact without
having to know the details of each other’s data or code.
Example: If Student is the class then we can create an object of type student as:
void main()
{
Student s1;
}

Data Abstraction and Encapsulation:


The wrapping up of data and functions into a single unit (called class) is known as
Encapsulation. Data Encapsulation is the most striking feature of a class. The data is not
accessible to the outside world and only those functions which are wrapped in the class
can access it. These functions provide the interface between the object’s data and the
program. This insulation of the data from direct access by the program is called Data
Hiding or Information Hiding.

4|Page
Abstraction means representing the essential features without including background
details. Classes use the concept of data abstraction and are defined as a list of attributes.
They encapsulate all the essential properties of the objects that are to be created. The
attributes are called data members because they hold the information. The functions that
operate on these data are called member functions.

Inheritance:
The mechanism of deriving a new class from an old class is called Inheritance or
Derivation. In OOP, the concept of inheritance provides the idea of reusability. This
means that we can add additional features to an existing class without modifying it. This
is possible by deriving a new class from the existing one. The new class will have the
combined features of both classes. The old class is called the base class and the new class
is called the derived class. Each derived class defines only those features that are unique
to it.

Polymorphism:
It is a Greek term, that means the ability to take more than one form. An operation may
provide different behaviors in different situations. The behavior depends upon the types
of data used in the operation.
Eg. Consider the operation of addition. For two numbers, the operation will generate a
sum. If the operands are strings, then the operation would produce a third string by
concatenation. The process of making an operator provide different behavior in different
situations is called operator overloading. Polymorphism is extensively used in
implementing inheritance.

Inline function:
One of the objectives of using functions in a program is to save memory space which
provides an advantage when a function is likely to be called many times.
However, every time a function is called, it takes a lot of extra time to execute a series of
instructions for tasks such as jumping to the function, saving registers, pushing
arguments into a stack, and returning to the calling function.
When a function is small, then a considerable percentage of execution time may be spent.

5|Page
C++ provides a solution for this. To eliminate the cost of calls to small functions, C++
provides a new feature called Inline Function.
An inline function is a function that is expanded in line when it is invoked. That is compiler
replaces the function call with the corresponding function code.
Syntax: Example:
inline function – header inline int test(int a)
{ {
Function body return (a * a);
} }

The above inline function can be called by statements like


c = test (2);
d = test (3+5);
On the execution of these statements, the values of c and d will be 4 and 64 respectively.
All inline functions must be defined before they are called. Usually, the functions are made
inline when they are small enough to be defined in one or two lines.
The inline keyword sends a request, not a command, to the compiler. The compiler may
ignore a request if the function definition is too large or too complicated and compile the
function as a normal function.

Some of the situations where inline expansion may not work are:
1. For functions returning values, if a loop, a switch, or a goto exists.
2. For functions not returning values, if a return statement exists.
3. If the function contains static variables.
4. If inline functions are recursive.
Example:
inline int mul (int x, int y)
{
return x*y;
}
inline int div (int x, int y)
{
return x/y;
}

6|Page
int main ()
{
int a, b;
a=50; b=25;
cout<<"\nMultiplication : "<<mul (a, b);
cout<<"\nDivision : "<<div (a, b);
return 0;
}
Output would be as follows:
Multiplication : 1250
Division : 2

Making an Outside Function Inline:


We can define a member function outside the class definition and still make it inline by
just using the qualifier inline in the header line of the function definition. For example,

inline void simple :: getdata()


{
cin>>no;
cin>>name;
}

Call by Value:
void swap(int , int);
int main()
{
int a, b;
cout<<”Enter the value for A : “;
cin>>a;
cout<<”Enter the value for B : “;
cin>>b;
swap(a,b);
cout<<”\nAfter interchange in main program”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
return 0;
}
void swap(int a,int b)
{
int c;
c=a;
7|Page
a=b;
b=c;
cout<<”\nIn subprogram.”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
}

Call by Reference:
void swap(int * ,int *);
int main()
{
int a,b;
cout<<”Enter the value for A : “;
cin>>a;
cout<<”Enter the value for B : “;
cin>>b;
swap(&a,&b);
cout<<”\nAfter interchange in main program”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
return 0;
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
cout<<”\nIn subprogram.”;
cout<<”\nValue of a is : “<<*a;
cout<<”\nValue of b is : “<<*b;
}

Default Arguments:
 C++ allows us to call a function without specifying its arguments. In such cases, the
function assigns a default value to the parameter that does not have a matching
argument in the function call.
 Default values are specified when the function is declared. The compiler looks at
the prototype to see how many arguments a function uses and alerts the program
for possible default values.

8|Page
Example:
float amount( float pamt, int year, float rate = 2.5);
The above prototype declares a default value of 2.5 to the argument rate. Then a function
call like
value = amount (1000,2); // one argument missing
passes the value 1000 to pamt and 2 to year and then lets the function use a default value
of 2.5 for rate.

The function call


value = amount (4000, 2, 3.5); // no arguments missing
passes an explicit value of 3.5 to rate.
 Only the trailing arguments can have default values and therefore we must add
default values from right to left. We cannot provide a default value to a particular
argument in the middle of an argument list.
 int test(int i, int j=5, int k=10); // legal
 int test(int i=5, int j); // illegal
 int test(int i=0, int j, int k=10); // illegal
 int test(int i=2, int j=5, int k=10); // legal
Advantages of Default Arguments:
 We can use default arguments to add new parameters to the existing functions.
 Default arguments are used to combine similar functions into one.

Const Arguments:
An argument to the function can be declared as constant as follows:
int strlen(const char *p);
int length(const string &s);
The qualifier tells the compiler that the function should not modify the argument. The
compiler will generate an error when this condition is violated. This type of declaration
is significant only when we pass the arguments by reference or pointer.

9|Page

You might also like