0% found this document useful (0 votes)
8 views22 pages

Module-1 Introduction of C++

Uploaded by

anamijames03
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)
8 views22 pages

Module-1 Introduction of C++

Uploaded by

anamijames03
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/ 22

Introduction of C++ Programming 2022

Module – 1
Introduction to Object Oriented Programming

Computer Programming background:


What is computer Program?
A computer program is a sequence or set of instructions in a computer
programming language to perform specific task by the computer to execute.
A computer program in its human-readable form is called source code.
Source code needs another computer program to execute because computers can
only execute their native machine instructions. Therefore, source code may be
translated to machine instructions using the language's compiler.
• A computer program is also called computer software, which can range
from two lines to millions of lines of instructions.
• Computer program instructions are also called program source code
and computer programming is also called program coding.
• A computer without a computer program is just a dump box; it is
programs that make computers active.

Uses of Computer Programs

Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defense, communication, etc. Listed below
are a few applications of computer programs −
• MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc.,
are examples of computer programs.
• Computer programs are being used to develop graphics and special effects
in movie making.
• Computer programs are being used to perform Ultrasounds, X-Rays, and
other medical examinations.
• Computer programs are being used in our mobile phones for SMS, Chat,
and voice communication.

Computer programming Languages:

A programming language is a computer language that is used


by programmers (developers) to communicate with computers. It is a set of
instructions written in any specific language (C, C++, Java, Python) to perform
a specific task.
1
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

A programming language is mainly used to develop desktop applications,


websites, and mobile applications.
Programmers write their instructions in code, which is text written using the
rules of a particular computer language. There are numerous programming
languages out there.

Some of the most popular programming languages include:

• Java
• JavaScript
• Python
• C++/C
• C#
• CSS
• HTML
• SQL

Types of programming language


Each programming language contains a unique set of keywords and syntax,
which are used to create a set of instructions. Thousands of programming
languages have been developed till now, but each language has its specific
purpose. These languages vary in the level of abstraction they provide from the
hardware. Some programming languages provide less or no abstraction while
some provide higher abstraction. Based on the levels of abstraction, they can be
classified into two categories:
o Low-level language
o High-level language

1. Low-level programming language


Low-level language is machine-dependent (0s and 1s) programming language.
The processor runs low- level programs directly without the need of a compiler
or interpreter, so the programs written in low-level language can be run very
fast and it is not portable.
Low-level language is further divided into two parts -
i. Machine Language
The machine-level language is a language that consists of a set of instructions
that are in the binary form 0 or 1. As we know that computers can understand
2
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

only machine instructions, which are in binary digits, i.e., 0 and 1, so the
instructions given to the computer can be only in binary codes. Creating a
program in a machine-level language is a very difficult task as it is not easy for
the programmers to write the program in machine instructions. It is error-prone
as it is not easy to understand, and its maintenance is also very high. A
machine-level language is not portable as each computer has its machine
instructions, so if we write a program in one computer will no longer be valid in
another computer.
The advantage of machine language is that it helps the programmer to execute
the programs faster than the high-level programming language.
ii. Assembly Language
Assembly language (ASM) is also a type of low-level programming language
that is designed for specific processors. It represents the set of instructions in
a symbolic and human-understandable form. It uses an assembler to convert the
assembly language to machine language.
The advantage of assembly language is that it requires less memory and less
execution time to execute a program.

Differences between Machine-Level language and Assembly language


The following are the differences between machine-level language and
assembly language:

Machine-level language Assembly language

The machine-level language comes The assembly language comes above the
at the lowest level in the hierarchy, machine language means that it has less
so it has zero abstraction level from abstraction level from the hardware.
the hardware.

It cannot be easily understood by It is easy to read, write, and maintain.


humans.

The machine-level language is The assembly language is written in


written in binary digits, i.e., 0 and 1. simple English language, so it is easily

3
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

understandable by the users.

It does not require any translator as In assembly language, the assembler is


the machine code is directly used to convert the assembly code into
executed by the computer. machine code.

It is a first-generation programming It is a second-generation programming


language. language.

2. High-level programming language


High-level programming language (HLL) is designed for developing user-
friendly software programs and websites. This programming language requires
a compiler or interpreter to translate the program into machine language
(execute the program).
Advantages of a high-level language
o The high-level language is easy to read, write, and maintain as it is
written in English like words.
o The high-level languages are designed to overcome the limitation of low-
level language, i.e., portability. The high-level language is portable; i.e.,
these languages are machine-independent.
High-level programming language includes Python, Java, JavaScript, PHP, C#,
C++, Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift
programming language.
A high-level language is further divided into three parts -
i. Procedural Oriented programming language
Procedural Oriented Programming (POP) language is derived from structured
programming and based upon the procedure call concept. It divides a program
into small procedures called routines or functions.
Procedural Oriented programming language is used by a software programmer
to create a program that can be accomplished by using a programming editor
like IDE, Adobe Dreamweaver, or Microsoft Visual Studio.
The advantage of POP language is that it helps programmers to easily track the
program flow and code can be reused in different parts of the program.
4
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

Example: C, FORTRAN, Basic, Pascal, etc.

ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In


this programming language, programs are divided into small parts called
objects. It is used to implement real-world entities like inheritance,
polymorphism, abstraction, etc in the program to makes the program reusable,
efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and


easier to execute, maintain, modify, as well as debug.

Example: C++, Java, Python, C#, etc.

iii. Natural language

Natural language is a part of human languages such as English, Russian,


German, and Japanese. It is used by machines to understand, manipulate, and
interpret human's language. It is used by developers to perform tasks such as
translation, automatic summarization, Named Entity Recognition (NER),
relationship extraction, and topic segmentation.

The main advantage of natural language is that it helps users to ask questions in
any subject and directly respond within seconds.

Differences between Low-Level language and High-Level language

The following are the differences between low-level language and high-level
language:

Low-level language High-level language

It is a machine-friendly language, It is a user-friendly language as this


i.e., the computer understands the language is written in simple English
machine language, which is words, which can be easily understood by
represented in 0 or 1. humans.

The low-level language takes more It executes at a faster pace.


time to execute.

It requires the assembler to convert It requires the compiler to convert the


the assembly code into machine high-level language instructions into

5
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

code. machine code.

The machine code cannot run on The high-level code can run all the
all machines, so it is not a portable platforms, so it is a portable language.
language.

It is memory efficient. It is less memory efficient.

Debugging and maintenance are Debugging and maintenance are easier in


not easier in a low-level language. a high-level language.

Difference between compiler and interpreter:

Compliers and interpreters are programs that help convert the high level
language (Source Code) into machine codes to be understood by the computers.
Compiler Interpreter
A compiler translates the entire source An interpreter translates the entire
code in a single run. source code line by line.
It consumes less time i.e., it is faster It consumes much more time than the
than an interpreter. compiler i.e., it is slower than the
compiler.
The localization of errors is difficult. The localization of error is easier than
the compiler.
It is more efficient. It is less efficient.
CPU utilization is more CPU utilization is less.

The compiler is used by the language An interpreter is used by languages


such as C, C++. such as Java, Python.

6
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

C++ Overview:
C++ is an object-oriented programming language. It is an extension to C
programming.
C++ was developed by Bjarne Stroustrup starting in 1980 at Bell Labs in
Murray Hill, New Jersey, as an enhancement to the C language and originally
named C with Classes but later it was renamed C++ in 1983.
C++ is a general purpose, case-sensitive, free-form programming
language that supports object-oriented, procedural and generic programming.
C++ supports the most of the major platforms that we have like windows,
Mac OS and Unix, so we can use it for a wide variety of applications for writing
different software’s that can be used in different domains.
One of the major features that are there in C++ and not in C is the
concept of classes and objects. The main difference between C++ and C
programming language is C++ supports classes and objects but C doesn’t
support it.
C++ fully supports object-oriented programming because it contains the
structure of object oriented program, including the four pillars of object-
oriented development:
➢ Encapsulation
➢ Data hiding
➢ Inheritance
➢ Polymorphism
The object oriented features in C++ allow programmers to build large
programs with clarity, extensibility and ease of maintenance, incorporating the
spirit and efficiency of C.
Applications of C++:
C++ is a versatile language for handling very large programs. It is
suitable for virtually any programming task including development of editors,
compilers, databases, communication systems and any complex real life
application systems.
➢ Since C++ allows us to create hierarchy-related objects, we can build
special object oriented libraries which can be used later by many
programmers.

7
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

➢ While C++ is able to map the real-world problem properly, the C part of
C++ gives the language the ability to get close to the machine-level
details.
➢ C++ programs are easily maintainable and expandable. When a new
feature needs to be implemented, it is very easy to add to the existing
structure of an object.
➢ It is expected that C++ will replace C as a general-purpose language in
the near future.
Advantages of C++
➢ C++ is a simple and portable structured programming language.
➢ It supports OOPs features
➢ It Provides easy and expandable
➢ C++ is more compatible than C
Disadvantages of C++
➢ C++ programming languages is not secured as compared to other
programming languages like Java or Python.
➢ C++ cannot support garbage collection.
➢ It is difficult to debug large as well as complex web applications.

Structure of C++ Program


C++ program would contain four sections as shown in below figure.
These sections may be placed in separate code files and then compiled
independently or jointly.

Include files

Class declaration

Member function definitions

Main function program

Structure of C++ program

8
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

Include Files/Header section

Usually, a program includes different programming elements such as built-


in functions, classes, keywords, constants, operators and more which are already
defined in the standard C++ library.

For using such pre-defined elements in a program, an appropriate header


must be included in the program. Moreover, the standard headers comprise
information such as prototype, definition and return type of library functions, the
data type of constants and more.

There are two types of header files defined in a program:

o Pre-defined header file: The header file which is predefined is known as


a pre-defined header file.
o User-defined header file: The header file which is defined by the user is
known as a user-defined header file.

Consequently, programmers do not have to declare (or define) the


predefined programming elements explicitly. Further, standard headers specify in
the program through the preprocessor directive” #include.

Next, the file <iostream>, which is a standard file that should come with the
C++ compiler, is short for input-output streams. This command comprises code
for displaying and getting input from the user.

Class Declaration or Definition

A class is an organization of data and functions which operate on them. Data


types are called data members and the functions are called member functions.

9
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

The combination of data members and member functions constitute a data


object or simply an object.

The general syntax or general form of the class declaration is as follows:

class class_name
{
private:
data members;
member functions ;
public:
data members;
member functions ;
protected:
data members;
member functions ;
};

For example
class example
{
private:
int a,b;
public:
void input();
void displaySum();
};

(i) Class name or name of class

It is mainly the name given to a particular class. It serves as a name specifier


for the class, using which we can create objects. The class is specified by
keyword “class”.

For example: class Circle

Here Circle is the class name.

10
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

(ii) Data Members

These are the data-type properties that describe the characteristics of a class.
We can declare any number of data members of any type in a class. We can say
that the variables in C and data members in C++.

For example: float area;

(iii) Member Functions

These are the various operations that can be performed to data members of that
class. We can declare any number of member functions of any type in a class.
Member functions are access using object and dot operator.

For example: void read();

void display();

How member function access: c1.read();

Here c1 is object of class circle and operator dot (.) is used to access member
functions.

(iv) Access Specifiers

Access Specifiers are used to identify access rights for the data members and
member functions of the class. Depending upon the access level of a class
member, access to it is allowed or denied.

There are three main types of access Specifiers in C++ programming language:

1. Private: A private member within a class denotes that only members of the
same class have accessibility. The private member is not accessible from
outside the class.

2. Public: Public members are accessible from outside the class.

11
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

3. Protected: A protected access specifier is a stage between private and public


access. If member functions defined in a class are protected, they cannot be
accessed from outside the class but can be accessed from the derived class
(inheritance).

Member function definition

Member functions can be defined in two places:

➢ Inside class definition


➢ Outside class definition

Inside class definition

To define member function inside a class the function declaration within


the class is replaced by actual function definition inside the class. Only small
functions are defined inside class definition.
Syntax: return_type classname(argument)
For example: class rectangle
{

int length;
int breadth;
public:
void getdata()
{
cin>>length;
cin>>breadth;
}
void putdata()
{
cout<<length;
cout<<breadth;
}
};

Outside class definition

12
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

A function declared as a member of a class is known as member


function. Member functions declared within a class must be defined separately
outside the class. The scope of the member function is limited to the class
mentioned in the header .Scope resolution operator :: is used to define the
member function.

Syntax: For member function outside a class


return_type classname::memberfunction(arg1,arg2,…,argn)
{
Function body;
}

The member functions have following characteristics


➢ Type and number of arguments in member function must be same as the
types and the number of data declared in the class definition.
➢ The symbol :: is known as scope resolution operator. Usage of :: along with
the class name is the header of function definition. This operator :: identifies
the function as the function of the particular class.
➢ Several classes can use same function name. Membership label will resolve
their scope.
➢ Member function can access private data of a class. A non member function
cannot.

For example,
void item :: getdata(int a, float b)
{
Number=a;
Cost=b;
}

Main Function

The main () is a startup function that starts the execution of a C++ program.
All C++ statements which are to be executed are written within main ( ). The
compiler executes all the instructions written within the opening and closing curly
braces’ {}’ that enclose the body of the main ( ).

13
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

General form of main function:

int main() void main()


{ {
Executable statements; or Executable statements;
return 0; }
}
A function definition has 4 elements:
1. return type
2. A function name
3. A parameter list enclosed in parenthesis
4. A function body

As soon as all the instructions in main () execute, the control passes out of
main ( ), and terminates the whole program and return a value to the operating
system.

By default, main () in C++ returns an int value to the operating system.


Thus, main () must end with the return 0 statement. A return value zero denotes
success and a non-zero value denotes failure or error. Return type is used to
declaration of main function, return 0 is the exit code of the program.

Documentation section/Comments section

Comments are an essential element of a program that comes into use for
increasing the readability of a program. In addition, it also helps in describing its
functioning. Similarly, comments are not executable statements. Thus, they do not
increase the size of a file.

14
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

Comments in C++ can be written in two ways. They are:

a. Single line comment: Single line comment is written within a single line
after ‘//’. For e.g.

// This is a single line comment in C++.

b. Multiple line comment: Multiple line comment is written in multiple lines


and is enclosed within ‘/*’ and ‘*/’. For e.g.

/*
This is multiple
line comment in
C++.
*/

Namespace

Ever since its creation, C++ has undergone a lot of changes by the C++ Standards
Committee. Similarly, Namespace is one of the new features in this language. It
allows the grouping of different entities such as classes, objects, functions and a
variety of C++ tokens, etc., under a single name.

Different users can form separate namespaces. Thus, they can make use of similar
names of the entities. This will avoid the compile-time error that may be present
because of the identical-name conflicts.

The C++ Standards Committee has rearranged the entities of the standard library
under a namespace known as std. The entities of a namespace can be accessed in
several ways which are as follows:

• By specifying the using directive


15
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

using namespace std;

cout<<“Hello World”;

• By specifying the full member name

std: :cout<<“Hello World”;

• By specifying the using-declaration

using std:: cout;

cout<<“Hello World”;

As soon as the new-style header includes, its contents include in the std
namespace. Thus, all modern C++ compilers support these statements.

#include<iostream>

using namespace std;

But, some old compilers may not support these statements. In this scenario, these
single statements replace the statements.

#include<iostream.h>

Simple program

C++ program to find the sum of two numbers

#include <iostream> /* Link Section */

16
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

using namespace std; /* Using directive */

class findsum /* Class definition */


{
private:
int a,b;
public:
void input();
void sum();
};

void findsum::input() /* Member function definition */


{
cout << "Enter values of a and b:";
cin >> a >> b;
}

void findsum::sum() /* Member function definition */


{
cout <<"Sum = "<<a+b;
}

int main() /* Main function definition */


{
findsum x;
x.input();
x.sum();
return 0;
}

17
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

Object Oriented Programming:

OOP stands for Object-Oriented Programming. Object-oriented programming –


As the name suggests uses objects in 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.

Procedural programming is about writing procedures or functions that perform


operations on the data, while object-oriented programming is about creating
objects that contain both data and functions.

“Object oriented programming as an approach that provides a way of


modularizing programs by creating partitioned memory area for both data and
functions that can be used as templates for creating copies of such modules on
demand”.

The below diagram shows the characteristics of data and functions in OOP.

Features of the Object Oriented programming

1. Emphasis is on doing rather than procedure.


2. Programs are divided into what are known as objects.

18
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data
structure.
5. Data is hidden and can’t be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added.
8. Follows bottom-up approach in program design.

BASIC CONCEPTS OF OBJECTS ORIENTED PROGRAMMING

1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing

Objects:
Objects are basic building blocks for designing programs. Objects are the real-
world entities may represent a person, place, or a table of data.” An object is a
collection of data members and associated member functions. Each object is
identified by a unique name. Every object must be a member of a particular
class. Ex: Apple, orange, mango are the objects of class fruit.

Object take up space in memory and have address associated with them. At the
time of execution of a program, the objects interact by sending messages to one
another but they didn’t know the details of data or functions within an object

Class:
Class is a blueprint for an object in OOP language. The objects can contain data
and code to manipulate the data. The objects can make by user defined data type
with the help of class. Therefore, objects are the variables of the class. Class are
a way of grouping objects having similar characteristics. Once a class is
defined, any number of objects of that class is created. Classes are user-defined
data types. A class can hold both data and functions.

Some important properties of class are:-

• Class is a user-defined data type


19
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

• A class contains members like data members and member functions.


• Data members are variables of the class
• Member functions are the methods that are used to manipulate the data
members.
• Data member defines the properties of the class whereas the member
function defines the behaviour of the class.
• A class can have multiple objects which have properties and behaviour
that in common for all of them.

Data Encapsulation:
A class can contain variables for storing data and functions to specify various
operations that can be performed on data. This process of wrapping up of data
and functions that operate on data as a single unit is called as Encapsulation.
When using Data Encapsulation, data is not accessed directly, it is only
accessible through the functions present inside the class. Data Encapsulation
enables the important concept of data hiding possible.

Data Abstraction:
Abstraction refers to the act of representing essential features without including
the back ground details or explanations. It represents a functionality of a
program without knowing implementation details. It is an approach that speaks
about hiding of the complexity and consumes only the functionality.

Inheritance:
Creating a new class from an existing class or base class is called Inheritance.
The base class is also known as parent class or super class, the new class that is
formed is called derived class. Derived class is also known as a child class or
sub class. Inheritance helps in reducing the overall code size of the program,
which is an important concept in object-oriented programming. The concept of
inheritance provides the idea of reusablity. Instead of rewritting the code you
can create the class from the existing class and extending the functionality of
existing class. This means that we can add additional features to an existing
class without modifying it. This is possible by designing a new class that will
have the combined features of both the classes.

Polymorphism:
Polymorphism comes from the Greek words “poly” and “morphism”. “poly”
means many and “morphism” means form i.e. many forms. Polymorphism

20
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

means the ability to take more than one form. Advantage of this is you can
make an object behave differently in different situations, so that no need to
create different objects for different situations. Polymorphism can be achieved
with the help of Overloading and Overriding concepts and it is classified into
compile time polymorphism and Runtime polymorphism.

Function with same name but different arguments.

Functioning is different.

Example: add(int a, int b) add(int a, int b, int c)

add(float a, float b) add(float a, float b, float c)

Dynamic binding:
Binding refers to the linking of a procedure call to the code to the executed in
response to the call. Dynamic binding means the code associated with a given
procedure call is not known until the time of the call at run-time. It is associated
with a polymorphic reference depends upon the dynamic type of that reference.

Message Passing:
An object oriented program consists of a set of objects that communicate with
each other. A message for an object is a request for execution of a procedure
and therefore will invoke a function (procedure) in the receiving object that
generates the desired result. Message passing involves specifying the name of
the object, the name of the function (message) and information to be sent.

Benefits of Object Oriented Programming:


Object Oriented Programming provides several benefits to both program
designer and user

• Reusability: In OOP programs, functions and modules that are written by


a user can be reused by other users without any modification.

• Inheritance: Through this we can eliminate redundant code and extend the
use of existing classes.

21
Yenepoya Institute of Technology
Introduction of C++ Programming 2022

• Data Hiding: The programmer can hide the data and functions in a class
from other classes. It helps the programmer to build the secure programs.

• Reduced complexity of a problem: The given problem can be viewed as a


collection of different objects. Each object is responsible for a specific
task. The problem is solved by interfacing the objects. This technique
reduces the complexity of the program design.

• Easy to Maintain and Upgrade: OOP makes it easy to maintain and


modify existing code as new objects can be created with small differences
to existing ones. Software complexity can be easily managed.

• Message Passing: The technique of message communication between


objects makes the interface with external systems easier.

• Modifiability: it is easy to make minor changes in the data representation


or the procedures in an OOP 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.

Application of OOP:

The most popular application of oops up to now, has been in the area of user
interface design such as windows. There are hundreds of windowing systems
developed using OOP techniques. Real business systems are often much more
complex and contain many more objects with complicated attributes and
methods. OOP is useful in this type of applications because it can simplify a
complex problem. The promising areas for application of OOP include.

1. Real – Time systems.


2. Simulation and modelling
3. Object oriented databases.
4. Hypertext, hypermedia and expertext.
5. Al and expert systems.
6. Neural networks and parallel programming.
7. Decision support and office automation systems.
8. CIM / CAM / CAD system.

22
Yenepoya Institute of Technology

You might also like