0% found this document useful (0 votes)
19 views12 pages

CPP Unit 1

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)
19 views12 pages

CPP Unit 1

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/ 12

CM3104

UNIT 1. Introduction To Object Oriented Programming

1.1 Procedural programming: What is procedural programming?


Features of procedural programming. Drawbacks of procedural
programming.
1.2 Object Oriented Programming: Definition on Object Oriented
Programming, Object Oriented Programming paradigm, basic
concepts of Object-Oriented Programming, benefits of Object-
Oriented Programming, Object Oriented languages, applications
of Object-Oriented Programming.
1.3 Beginning with C++: What is C++, C++ program structure,
object, class, example of object and class, creating the source
file, compiling and linking.
**************************************************
What is procedural programming?
• Procedural Programming can be defined as a programming model which is
derived from structured programming, based upon the concept of calling
procedure.
• Procedures, also known as routines, subroutines or functions, simply
consist of a series of computational steps to be carried out.
• During a program’s execution, any given procedure might be called at any
point, including by other procedures or itself.

Difference between procedural and object-oriented programming


language.

Procedural Programming Object-Oriented Programming

The program is divided into small The program is divided into small
parts called functions. parts called objects.

1|Page 186_Shraddha Keshao Bonde_N2


CM3104

Follows a top-down approach. Follows a bottom-up approach.

It deals with algorithm. It deals with data.

It needs less memory. Needs more memory.

Does not have access specifier. Has access specifiers such as private,
public, protected, etc.

Overloading not possible. Overloading possible.

Less secure Highly secure

Based on unreal world Based on real world

There is no concept of data hiding There is concept of data hiding and


and inheritance. inheritance.

Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#,


Basic, etc. etc.

Features of procedural programming

Predefined functions
• A predefined function is a function available in a procedural
programming language from a library of available functions.
• Allow a programmer to complete common tasks without creating the
required code themselves.
2|Page 186_Shraddha Keshao Bonde_N2
CM3104

Local Variable
• A local variable is a programming variable that has a local scope of use.
• This means the variable only functions in the function in which the
developer defines it.

Global Variable
• Global variables increase functionality when local variables are
insufficient.
• Developers can use global variables in nearly all functions.
• A variable makes itself available to all methods and functions in the code,
allowing the developer to access key data throughout the program's many
procedures.

Modularity
• Modularity is a structure in which a developer divides the functionality of
its code into a series of smaller blocks.
• The programmer can then call these blocks, often called methods or
functions in procedural programming languages, in their code to access
them.

Parameter Passing
• Parameters are the data values that transfer from each function within a
code sequence.
• Parameter passing allows variable values to be passed through to the
program which will hadle it with a procedure.

Advantages of Procedural Programming


• It is good for general-purpose programming.
• We can access the same code at different points in the software program
without repeating it.
• The memory need is also reduced by using the Procedural Programming
technique.

3|Page 186_Shraddha Keshao Bonde_N2


CM3104

• An easier way to keep track of program flow.

Disadvantages of Procedural Programming


• The code reusability feature is not present in procedural-oriented
programming. We have to rewrite the same programming code many
times.
• Difficult to relate to real-world objects.
• We cannot perform operations like encapsulation, inheritance, etc.
• No security for data.
• Difficult to create new data types reduces extensibility.

Object Oriented Programming


• Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc in programming.
• The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function .

Basic concepts of Object-Oriented Programming

Object
• It is a basic unit of Object-Oriented Programming and represents the real-
life entities.

4|Page 186_Shraddha Keshao Bonde_N2


CM3104

• When a class is defined, no memory is allocated but when it is


instantiated (i.e. an object is created) memory is allocated.
• An object has an identity, state, and behaviour.
• Each object contains data and code to manipulate the data.
• For example “Dog” is a real-life Object, which has some characteristics
like color, Breed, Bark, Sleep, and Eats.

Encapsulation:
• Encapsulation is defined as the wrapping up of data under a single unit.
• It is the mechanism that binds together code and the data it manipulates.
• The variables or data of a class are hidden from any other class and can
be accessed only through any member function of their class in which
they are declared.
• The data in a class is hidden from other classes, so it is also known as
data-hiding.

Inheritance
• Inheritance is an important pillar of OOP(Object-Oriented Programming).
• The capability of a class to derive properties and characteristics from
another class is called Inheritance.
• Inheritance allows the user to reuse the code whenever possible and
reduce its redundancy.

5|Page 186_Shraddha Keshao Bonde_N2


CM3104

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.
• For example, A person at the same time can have different characteristics.
Like a man at the same time is a father, a husband, an employee.
• So the same person posses different behavior in different situations. This
is called polymorphism.

Abstraction
• Data abstraction is one of the most essential and important features of
object-oriented programming.
• Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details or
implementation.

Class
• A class is a user-defined data type.
• It consists of data members and member functions, which can be accessed
and used by creating an instance of that class.
• It represents the set of properties or methods that are common to all
objects of one type. A class is like a blueprint for an object.

6|Page 186_Shraddha Keshao Bonde_N2


CM3104

Benefits of Object-Oriented Programming


• Code reusability
o New objects can be derived from old objects, allowing for improvement and
refinement of the code at each stage and also preserving parts of the code for
other programs.
o This is used to develop many class libraries using class codes that have already
been written, for example, Microsoft Foundation Classes (MFC).

• Code Modularity
o Everything in OOP is an object
o These objects can be interchanged or removed to meet the users’ needs.

• Easier maintenance
o Inheritance usually reduces maintenance because of the ‘domino effect it has on
derived classes when a change is made in a base class.

• Design stability
o Once a stable base class has been developed, the new classes that are derived
may have fewer less errors and bugs.

• Improved communication between developers and users


o Objects can be broken down into real life entities, hence it is easier it
communicates ideas.

• Seamless transition from design to implementation


o This is mainly because communications are improved.

Object Oriented languages

Object Oriented languages

Java C# Python Ruby PHP TypeScript

7|Page 186_Shraddha Keshao Bonde_N2


CM3104

Applications of Object-Oriented Programming


✓ Client-Server Systems.
✓ Object-Oriented Databases.
✓ Object-Oriented Databases.
✓ Real-Time System Design.
✓ Simulation and Modelling System.
✓ Hypertext and Hypermedia.
✓ Neural Networking and Parallel Programming.
✓ Office Automation Systems.
✓ CIM/CAD/CAM Systems
✓ AI Expert Systems

What is C++?
➢ C++ is a cross-platform language that can be used to create high-
performance applications.
➢ C++ was developed by Bjarne Stroustrup, as an extension to the C
language.
➢ C++ gives programmers a high level of control over system resources and
memory.

Why Use C++?


➢ C++ is one of the world's most popular programming languages.
➢ C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
➢ C++ gives a clear structure to programs and allows code to be reused,
lowering development costs.
➢ C++ is portable and can be used to develop applications that can be
adapted to multiple platforms.
➢ C++ is fun and easy to learn!
➢ As C++ is close to C, C# and Java, it makes it easy for programmers to
switch to C++ or vice versa.

C++ program structure


In C++, a program is divided into the following three sections:

8|Page 186_Shraddha Keshao Bonde_N2


CM3104

Standard Libraries Section


Main Function Section
Function Body Section

Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}

Standard Libraries Section


#include <iostream>
using namespace std;

➢ #include is a specific preprocessor command that effectively copies and


pastes the entire text of the file, specified between the angle brackets, into
the source code.
➢ The file <iostream>, which is a standard file that should come with the
C++ compiler, is short for input-output streams. This command contains
code for displaying and getting an input from the user.
➢ namespace is a prefix that is applied to all the names in a certain set.
iostream file defines two names used in this program - cout and endl.
➢ This code is saying: Use the cout and endl tools from the std toolbox.

Main Function Section


int main() {}

➢ The starting point of all C++ programs is the main function.

9|Page 186_Shraddha Keshao Bonde_N2


CM3104

➢ This function is called by the operating system when your program is


executed by the computer.
➢ { signifies the start of a block of code, and } signifies the end.

Function Body Section


cout << "Hello World" << endl;
return 0;

➢ The name cout is short for character output and displays whatever is
between the << brackets.
➢ Symbols such as << can also behave like functions and are used with the
keyword cout.
➢ The return keyword tells the program to return a value to the function int
main
➢ After the return statement, execution control returns to the operating
system component that launched this program.
➢ Execution of the code terminates here.

C++ Classes/Objects
✓ 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.

Create a Class
To create a class, use the class keyword:
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
10 | P a g e 186_Shraddha Keshao Bonde_N2
CM3104

string myString; // Attribute (string variable)


};

Create an Object
To create an object of MyClass, specify the class name, followed by the
object name.

class MyClass
{ // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";

// Print attribute values


cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}

Creating a source file


1. In the Project Explorer view, right-click a project, and select New > File
> Other.

11 | P a g e 186_Shraddha Keshao Bonde_N2


CM3104

2. Select Source file under C++.


3. In the Source File box, type a name followed by the appropriate
extension. Select a template.
4. Click Finish.
5. Enter your code in the editor view.
6. Type CTRL+S to save the file.

Linking
✓ Refers to creation of a single executable file from multiple object files.
✓ The file created after linking is ready to be loaded into memory and
executed by the system.
✓ There is difference in linking and compilation when it comes to
understanding errors.

Compiling
✓ This phase translates the program into a low level assembly level code.
✓ The compiler takes the preprocessed file ( without any directives) and
generates an object file containing assembly level code.
✓ The object file created is in the binary form.
****************************************************************

12 | P a g e 186_Shraddha Keshao Bonde_N2

You might also like