What Is Procedural Programming?
What Is Procedural Programming?
What Is Procedural Programming?
Syntax
class class_name {
data_type data_name;
return_type method_name(parameters);
}
Example
#include<iostream>
using namespace std;
class calculator {
int number1;
int number2;
char symbol;
public :
void add() {
cout<<"The sum is "<<number1 + number2 ;
}
void sub() {
cout<<"The subtraction is "<<number1 - number2 ;
}
void mult() {
cout<<"The multiplication is "<<number1 * number2 ;
}
void div() {
cout<<"The division is "<<number1 / number2 ;
}
calculator (int a , int b , char sym) {
number1 = a;
number2 = b;
symbol = sym;
switch(symbol){
case '+' : add();
break;
case '-' : add();
break;
case '*' : mult();
break;
case '/' : div();
break;
default : cout<<"Wrong operator";
}
}
};
int main() {
calculator c1(12 , 34 , '+');
}
Encapsulation In object oriented programming, encapsulation is the
concept of wrapping together of data and information in a single unit.
A formale defination of encapsulation would be: encapsulation is
binding togather the data and related function that can manipulate the
data.
Real life example,
In our colleges, we have departments for each course like computer
science, information tech. , electronics, etc. each of these departments
have their own students and subjects that are kept track of and being
taught. let's think of each department as a class that encapsulates the
data about students of that department and the subjects that are to be
taught. Also a department has some fixed rules and guidelines that are
to be followed by the students that course like timings, methods used
while learning, etc. this is encapsulation in real life, there are data and
there are ways to manipulate data.
Due to the concept of encapsulation in object oriented programming
another very important concept is possible, it is data abstraction or
Data Hiding. it is possible as encapsulating hides the data at show only
the information that is required to be displayed.
Polymorphism The name defines polymorphism is multiple forms.
which means polymorphism is the ability of object oriented
programming to do some work using multiple forms. The behaviour of
the method is dependent on the type or the situation in which the
method is called.
Let’s take a real life example, A person can have more than one
behaviour depending upon the situation. like a woman a mother,
manager and a daughterAnd this define her behaviour. This is from
where the concept of polymorphism came from.
In c++ programming language, polymorphism is achieved using two
ways. They are operator overloading and function overloading.
Operator overloading In operator overloading and operator can
have multiple behaviour in different instances of usage.
Function overloading Functions with the same name that can do
multiple types based on some condition.
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
Class Objects
There are several advantages of OOP in C++. Some of them are as follows:
1. Easier Troubleshooting
One of the major benefits of using object-oriented programming when troubleshooting applications in
C++ is its ability to help identify and isolate problems. By isolating objects, developers can more easily
trace the source of an issue and determine which part of the code needs to be changed or improved.
2. Code Reusability
Save time and money by avoiding having to write new sections of code each time a project needs an
update or feature change.
Reduce development costs while also improving program efficiency because with OOPs, entire blocks
of code can be written once and then reused multiple times throughout the application.
Implement changes quickly without having to worry about breaking existing functionality or
duplicating efforts unnecessarily.
3. Data Redundancy
Data redundancy is a common issue that can occur when working with databases. It occurs when
multiple copies of the same piece of data are stored in different locations, resulting in unused disk space
and likely conflicts between records. Although the term “redundancy” is disagreeable in this context, it
is however seen as one of the benefits of OOP in C++ because it minimizes the repetition of tasks.
4. Code Flexibility
When an application is written with OOPs, developers will find that editing existing code becomes
much easier as changes need to be made within one isolated area without affecting any other parts of
the program. This means that tasks like bug fixing or adding new features can be done quickly and
easily, requiring minimal effort on behalf of the developer.
5. Polymorphism Flexibility
Polymorphism is an incredibly powerful tool when it comes to coding applications in C++, allowing
developers to easily create flexible and extensible programs that can be adapted for multiple purposes.
In its simplest form, polymorphism is the ability of a single object or class to take on different forms
depending on how it’s used. This means that you can use the same codebase but change certain aspects
of its behaviour based on user input or other environmental factors.
6. Better Productivity
In order to ensure better productivity when coding applications in C++, it is important for developers
to employ a consistent methodology throughout the entire project. This means taking the time to plan
out their program’s design before actually jumping into writing code, ensuring that all components are
properly identified and structured ahead of time. This will help reduce debugging efforts later on down
the line.
7. Security
When encrypting applications in C++, security is of extreme importance. One-way developers can
ensure data protection and privacy within their software solutions is by utilizing encapsulation
principles which limit access rights between objects based on user permission levels inside these objects
– ultimately making it harder for outside sources (such as hackers) from gaining unauthorized access or
manipulating data stored within the system itself.
Real-Time System design: Real-time system inherits complexities and makes it difficult to build
them. OOP techniques make it easier to handle those complexities.
Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored, searched, and
edited easily. Hypermedia on the other hand is a superset of hypertext. OOP also helps in laying the
framework for hypertext and hypermedia.
AI Expert System: These are computer application that is developed to solve complex problems
which are far beyond the human brain. OOP helps to develop such an AI expert System
Office automation System: These include formal as well as informal electronic systems that
primarily concerned with information sharing and communication to and from people inside and
outside the organization. OOP also help in making office automation principle.
Neural networking and parallel programming: It addresses the problem of prediction and
approximation of complex-time varying systems. OOP simplifies the entire process by simplifying
the approximation and prediction ability of the network.
Stimulation and modelling system: It is difficult to model complex systems due to varying
specifications of variables. Stimulating complex systems require modeling and understanding
interaction explicitly. OOP provides an appropriate approach for simplifying these complex models.
Object-oriented database: The databases try to maintain a direct correspondence between the real
world and database object in order to let the object retain it identity and integrity.
Client-server system: Object-oriented client-server system provides the IT infrastructure creating
object-oriented server internet(OCSI) applications.
CIM/CAD/CAM systems: OOP can also be used in manufacturing and designing applications as it
allows people to reduce the efforts involved. For instance, it can be used while designing blueprints
and flowcharts. So it makes it possible to produce these flowcharts and blueprint accurately.
What is C++?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ gives programmers a high level of control over system resources and memory.
C++ was developed by Bjarne Stroustrup(बज़्ने स्ट्र ॉस्ट्ुप), as an extension to the C language.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which 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++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ support classes and objects, while C does not.
A compiler, like GCC, to translate the C++ code into a language that the computer will understand
Simple Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Example explained
Line 1: #include <iostream> is a header file library that lets us work with input and output objects,
such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the standard
library.
Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think
of it as something that (almost) always appears in your program.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function.
Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example it will output "Hello World!".
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using
namespace std line can be omitted and replaced with the std keyword, followed by the :: operator for
some objects:
Example:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
New Lines
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
Applications Of C++:
There are several uses or benefits of C++ for developing applications. For example, the
applications that are based on the graphic user interface(GUI) like adobe photoshop and
others. It is popular among students as a beginning language. Some of the major
applications that are used in C++ by major software vendors, sellers Etc.
• Google
• Mozilla
• Microsoft
• Rockstar Games
• MongoDB
• Games and Animations
• Media Access
• Compilers
• Scanning
•
Input / Output in C++0
C++ comes with libraries that provide us with many ways for performing input and output.
In C++ input and output are performed in the form of a sequence of bytes or more
commonly known as streams.
• Input Stream: If the direction of flow of bytes is from the device(for example,
Keyboard) to the main memory then this process is called input.
• Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called output.
#include <iostream>
using namespace std;
int main()
{
char sample[] = "Harivandana";
cout << sample << " – College Of computer science ";
return 0;
}
standard input stream (cin): Usually the input device in a computer is the keyboard. C++
cin statement is the instance of the class istream and is used to read input from the standard
input device which is usually a keyboard.
The extraction operator(>>) is used along with the object cin for reading inputs. The
extraction operator extracts the data from the object cin which is entered using the
keyboard.
#include <iostream>
using namespace std;
int main()
{
int age;
The C++ program is written using a specific template structure. The structure of the
program written in C++ language is as follows:
Documentation Section:
• This section comes first and is used to document the logic of the program that
the programmer going to code.
• It can be also used to write for purpose of the program.
• Whatever written in the documentation section is the comment and is not
compiled by the compiler.
• Documentation Section is optional since the program can execute without them.
Below is the snippet of the same:
For Example:
/* This is a C++ program to find the factorial of a number
The basic requirement for writing this program is to have knowledge of loops
To find the factorial of number iterate over range from number to one
*/
Linking Section:
The linking section contains two parts:
Header Files:
Generally, a program includes various programming elements like built-in functions,
classes, keywords, operators, etc. that are already defined in the standard C++ library.
In order to use such pre-defined elements in a program, an appropriate header must be
included in the program.
Standard headers are specified in a program through the preprocessor directive #include. In
Figure, the iostream header is used. When the compiler processes the
instruction #include<iostream>, it includes the contents of the stream in the program.
This enables the programmer to use standard input, output, and error facilities that are
provided only through the standard streams defined in <iostream>.
Namespaces:
• A namespace permits grouping of various entities like classes, objects, functions,
and various C++ tokens, etc. under a single name.
• Any user can create separate namespaces of its own and can use them in any other
program.
namespace std contains declarations for cout, cin, endl, etc. statements.
using namespace std;
Namespaces can be accessed in multiple ways:
1. using namespace std;
2. using std :: cout;
Definition Section:
It is used to declare some constants and assign them some value.
In this section, anyone can define your own datatype using primitive data types.
Global Declaration Section:
• Here, the variables and the class definitions which are going to be used in the
program are declared to make them global.
• The scope of the variable declared in this section lasts until the entire program
terminates.
• These variables are accessible within the user-defined functions also.
Function Declaration Section:
• It contains all the functions which our main functions need.
• Usually, this section contains the User-defined functions.
Main Function:
• The main function tells the compiler where to start the execution of the program.
The execution of the program starts with the main function.
• All the statements that are to be executed are written in the main function.
• The compiler executes all the instructions which are written in the curly
braces {} which encloses the body of the main function.
• Once all instructions from the main function are executed, control comes out of
the main function and the program terminates and no further execution occur.
Example:
// Driver code
int main()
{
int a = 2, b = 3;
// Driver code
int main()
{
int a = 2, b = 3;
b = a + b;
a = b - a;
b = b - a;
Namespace in C++
Namespace provide the space where we can define or declare identifier i.e.
variable, method, classes.
Using namespace, you can define the space or context in which identifiers are defined i.e.
variable, method, classes. In essence, a namespace defines a scope.
Advantage of Namespace
you might be writing some code that has a function called xyz() and there is another library
available which is also having same function xyz(). Now the compiler has no way of
knowing which version of xyz() function you are referring to within your code.
The best example of namespace scope is the C++ standard library (std) where all the
classes, methods and templates are declared. Hence while writing a C++ program we
usually include the directive using namespace std;
Defining a Namespace:
A namespace definition begins with the keyword namespace followed by the namespace name as
follows:
namespace namespace_name
You can also avoid prepending of namespaces with the using namespace directive. This directive tells
the compiler that the subsequent code is making use of names in the specified namespace.
Tokens in C++:
C++ is a powerful language. In C++, we can write structured programs and object-oriented
programs also. C++ is a superset of C and therefore most constructs of C are legal in C++
with their meaning unchanged. However, there are some exceptions and additions.
Token
When the compiler is processing the source code of a C++ program, each group of
characters separated by white space is called a token. Tokens are the smallest individual
units in a program. A C++ program is written using tokens.
It has the following tokens:
• Keywords
• Identifiers
• Constants
• Strings
• Operators
Keywords
Keywords(also known as reserved words) have special meanings to the C++ compiler and
are always written or typed in short(lower) cases.
Keywords are words that the language uses for a special purpose, such as void, int, public,
etc. It can’t be used for a variable name or function name or any other identifiers.
The total count of reserved keywords is 95. Below is the table for some commonly used
C++ keywords.
C++ Keyword
double new switch auto else operator break enum private this
Identifiers refer to the name of variables, functions, arrays, classes, etc. created by the
programmer. They are the fundamental requirement of any language.
Rules for naming identifiers:
• Identifier names can not start with a digit or any special character.
• A keyword cannot be used as s identifier name.
• Only alphabetic characters, digits, and underscores are permitted.
• The upper case and lower case letters are distinct. i.e., A and a are different in
C++.
• The valid identifiers are A,Name .
• So there are some main properties of keywords that distinguish keywords from
identifiers:
Keywords Identifiers
All variables use data type during declaration to restrict the type of data to be stored. we
can say that data types are used to tell the variables the type of data they can store.
Whenever a variable is defined in C++, the compiler allocates some memory for that
variable based on the data type with which it is declared.
C++ supports a wide variety of data types and the programmer can select the data type
appropriate to the needs of the application.
Data types specify the size and types of values to be stored. However, storage
representation and machine instructions to manipulate each data type differ from machine
to machine, although C++ instructions are identical on all machines.
Simple Example:
// C++ Program to Demonstrate the correct size of various data types on your computer.
#include <iostream>
using namespace std;
int main()
return 0;
User-Defined DataTypes:
The data types that are defined by the user are called the derived datatype or user-defined
derived data type.
These types include:
• Class
• Structure
• Union
• Enumeration
• Typedef defined DataType
Below is the detailed description of the following types:
Class:
The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-
defined data type, which holds its own data members and member functions, which can be
accessed and used by creating an instance of that class. A class is like a blueprint for an object.
Syntax:
Example:
#include <iostream>
class Student
public:
};
int main()
s1.id = 1;
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
Structure:
A structure is a user defined data type in C/C++. A structure creates a data type that can be
used to group items of possibly different types into a single type.
Syntax:
struct address {
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
For Example:
#include <iostream>
struct Point {
int x, y;
};
int main()
arr[0].x = 10;
arr[0].y = 20;
return 0;
}
Union:
Like Structures, union is a user defined data type. In union, all members share the same
memory location.
For example, in the following C program, both x and y share the same location. If we
change x, we can see the changes being reflected in y.
#include <iostream>
union test {
int x, y;
};
int main()
// A union variable t
union test t;
t.x = 2;
<< endl
<< endl;
t.y = 10;
<< endl
<< "x = " << t.x
<< endl;
return 0;
Enumeration:
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to
integral constants, the names make a program easy to read and maintain.
Syntax:
enum State {Working = 1, Failed = 0};
For Example:
#include <iostream>
Tue,
Wed,
Thur,
Fri,
Sat,
Sun };
int main()
day = Wed;
return 0;
}
Typedef:
C++ allows you to define explicitly new data type names by using the keyword typedef.
Using typedef does not actually create a new data class, rather it defines a name for an
existing type. This can increase the portability(the ability of a program to be used across
different types of machines.
Syntax:
typedef type name;
#include <iostream>
int main()
b1 = 'c';
return 0;