Module-1 Introduction of C++
Module-1 Introduction of C++
Module – 1
Introduction to Object Oriented Programming
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.
• Java
• JavaScript
• Python
• C++/C
• C#
• CSS
• HTML
• SQL
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.
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.
3
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
The main advantage of natural language is that it helps users to ask questions in
any subject and directly respond within seconds.
The following are the differences between low-level language and high-level
language:
5
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
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.
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.
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.
Include files
Class declaration
8
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
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.
9
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
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();
};
10
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
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++.
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.
void display();
Here c1 is object of class circle and operator dot (.) is used to access member
functions.
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.
11
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
int length;
int breadth;
public:
void getdata()
{
cin>>length;
cin>>breadth;
}
void putdata()
{
cout<<length;
cout<<breadth;
}
};
12
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
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
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.
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
a. Single line comment: Single line comment is written within a single line
after ‘//’. 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:
cout<<“Hello World”;
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>
But, some old compilers may not support these statements. In this scenario, these
single statements replace the statements.
#include<iostream.h>
Simple program
16
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
17
Yenepoya Institute of Technology
Introduction of C++ Programming 2022
The below diagram shows the characteristics of data and functions in OOP.
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.
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.
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.
Functioning is different.
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.
• 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.
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.
22
Yenepoya Institute of Technology