Structured Programming in C++ Programming | C++ Programming

Structured Programming in C++ Programming

Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a top-down design model, in which developers map out the overall program structure into separate subsections to make programs more efficient and easier to understand and modify similar functions is coded in a separate module or sub-module, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. In this technique, program flow follows a simple hierarchical model that employs three types of control flows: sequential, selection, and iteration.


Almost any language can use structured programming techniques to avoid common pitfalls of unstructured languages. Most modern procedural languages include features that encourage structured programming. Object-oriented programming (OOP) can be thought of as a type of structured programming, uses structured programming techniques for program flow, and adds more structure for data to the model. Some of the better known structured programming languages are Pascal, C, PL/I, and Ada.


Object-oriented Programming 

The fundamental idea behind object-oriented programming is to combine or encapsulate both data (or instance variables) and functions (or methods) that operate on that data into a single unit. This unit is called an object. The data is hidden, so it is safe from accidental alteration. An object’s functions typically provide the only way to access its data. In order to access the data in an object, we should know exactly what functions interact with it. No other functions can access the data. Hence OOP focuses on data portion rather than the process of solving the problem.

An object-oriented program typically consists of a number of objects, which communicate with each other by calling one another’s functions. This is called sending a message to the object. This kind of relation is provided with the help of communication between two objects and this communication is done through information called message. In addition, object-oriented programming supports encapsulation, abstraction, inheritance, and polymorphism to write programs efficiently. Examples of object-oriented languages include Simula, Smalltalk, C++, Python, C#, Visual Basic .NET and Java etc.


Object-Oriented Concepts 

The basic concepts underlying OOP are: Class, Object, abstraction, encapsulation, inheritance, and polymorphism.

 Abstraction:
             Abstraction is the essence of OOP. Abstraction means the representation of the essential features without providing the internal details and complexities. In OOP, abstraction is achieved by the help of class, where data and methods are combined to extract the essential features only. Encapsulation: Encapsulation is the process of combining the data (called fields or attributes) and functions (called methods or behaviors) into a single framework called class. Encapsulation helps preventing the modification of data from outside the class by properly assigning the access privilege to the data inside the class. So the term data hiding is possible due to the concept of encapsulation, since the data are hidden from the outside world.

 Inheritance: 

              Inheritance is the process of acquiring certain attributes and behaviors from parents. For examples, cars, trucks, buses, and motorcycles inherit all characteristics of vehicles. Object-oriented programming allows classes to inherit commonly used data and functions from other classes. If we derive a class (called derived class) from another class (called base class), some of the data and functions can be inherited so that we can reuse the already written and tested code in our program, simplifying our program.

 Polymorphism: 

              Polymorphism means the quality of having more than one form. The representation of different behaviors using the same name is called polymorphism. However the behavior depends upon the attribute the name holds at particular moment.

Advantages of Object-oriented Programming 

Some of the advantages are: 

 Elimination of redundant code due to inheritance, that is, we can use the same code in a base class by deriving a new class from it.
 Modularize the programs. Modular programs are easy to develop and can be distributed independently among different programmers.
 Data hiding is achieved with the help of encapsulation.
 Data centered approach rather than process centered approach.
 Program complexity is low due to distinction of individual objects and their related data and functions.
 Clear, more reliable, and more easily maintained programs can be created.

Object-based Programming 

The fundamental idea behind object-based programming is the concept of objects (the idea of encapsulating data and operations) where one or more of the following restrictions apply:
1. There is no implicit inheritance
2. There is no polymorphism
3. Only a very reduced subset of the available values are objects (typically the GUI components)

Object-based languages need not support inheritance or sub-typing, but those that do are also said to be object-oriented. An example of a language that is object-based but not object-oriented is Visual Basic (VB). VB supports both objects and classes, but not inheritance, so it does not qualify as object-oriented. Sometimes the term object based is applied to prototype-based languages, true object-oriented languages that do not have classes, but in which objects instead inherit their code and data directly from other template objects. An example of a commonly used prototype-based language is JavaScript.


Output Using cout 

The keyword cout (pronounced as ‘C out’) is a predefined stream object that represents the standard output stream (i.e monitor) in C++. A stream is an abstraction that refers to a flow of data.

The << operator is called insertion or put to operator and directs (inserts or sends) the contents of the variable on its right to the object on its left. For example, in the statement cout<<"Enter first value:"; the << operator directs the string constant “Enter first value” to cout, which sends it for the display to the monitor.


 Input with cin 

The keyword cin (pronounced ‘C in’) is also a stream object, predefined in C++ to correspond to the standard input stream (i.e keyword). This stream represents data coming from the keyboard.

The operator >> is known as extraction or get from operator and extracts (takes) the value from the stream object cin its left and places it in the variable on its right. For example, in the statement cin>>first;, the >> operator extracts the value from cin object that is entered from the keyboard and assigns it to the variable first.

Cascading of I/O operators 

We can use insertion operator (<<) in a cout statement repeatedly to direct a series of output streams to the cout object. The streams are directed from left to right. For example, in the statement cout<<Sum=”<
Similarly, we can use extraction operator (>>) in a cin statement repeatedly to extract a series of input streams from the keyboard and to assign these streams to the variables, allowing the user to enter a series of values. The values are assigned from left to right. For example, in the statement cin>>x>>y;, the first value entered will be assigned to the variable x and the second value entered will be assigned to variable y.

Manipulators 

Manipulators are the operators used with the insertion operator (<<) to modify or manipulate the way data is displayed. The most commonly used manipulators are endl, setw, and setprecision.
The end l Manipulator 
           This manipulator causes a linefeed to be inserted into the output stream. It has the same effect as using the newline character ‘\n’. for example, the statement
cout<<First value =”<will cause two lines of output.

The setw Manipulator 
           This manipulator causes the output stream that follows it to be printed within a field of n characters wide, where n is the argument to setw(n). The output is right justified within the field. For example,
cout<Output:   
John
is
boy
The setprecision Manipulator 
           This manipulator sets the n digits of precision to the right of the decimal point to the floating point output, where n is the argument to setprecision(n). For example,
float a = 42.3658945, b = 35.24569, c = 58.3214789, d = 49.321489; cout

Output:
42.365894
35.246
58.321
49.32
Note: The header file for setw and setprecision manipulators is iomanip.h.


Reference Variable 

Reference variable is an alias (another name) given to the already existing variables of constants. When we declare a reference variable memory is not located for it rather it points to the memory of another variable.
Consider the case of normal variables
Structured Programming



Structured Programming

Type Conversion 

There are two types of type conversion: automatic conversion and type casting.
Automatic Conversion (Implicit Type Conversion) 
When two operands of different types are encountered in the same expression, the lower type variable is converted to the type of the higher type variable by the compiler automatically. This is also called type promotion. The order of types is given below:
Structured Programming



Type Casting 

Sometimes, a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically. For this C++ permits explicit type conversion of variables or expressions as follows:
(type-name) expression //C notation
type-name (expression) //C++ notation
For example, 
int a = 10000;
int b = long(a) * 5 / 2; //correct
int b = a * 5/2; //incorrect (can you think how?) 

Post a Comment

0 Comments