C++ by Rome - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

principles of object oriented programming

 

Concept about C++:

C++ is an enhanced version of the C language. C++ includes everything that is part of C language and adds support for object oriented programming (OOP). With very few, very major exceptions,  C++ is a superset of C.

 

A look at procedure oriented programming

Conventional programming, using high level languages such as COBOL, FORTRAN and C programming languages , is commonly known as procedure-oriented programming (POP). In the procedure-oriented approach, the problem is viewed as a sequence of things to be done such as reading, calculating and printing. A number of functions are written to accomplish these tasks. The primary focus is on functions. A typical program  structure for procedural programming is shown in Fig. 1.4.

 

img2.png

 

Main characteristics of procedure-oriented programming are

img3.png Emphasis is on doing things (algorithms).

img3.png Large programs are divided into smaller programs known as functions.

img3.png Most of the functions share global data.

img3.png Data move openly around the system from function to function.

img3.png Functions transform data from one form to another.

img3.png Employs top-down approach in program design. Top down and bottom up are designing approaches. In top down approach, first we are designing the main module (i.e main function) and then we will design all other sub modules.

 

 

Object-Oriented Programming paradigm:

The major motivating factor in the invention of object-oriented approach is to remove some of the flows encountered in the procedural approach. OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called object and then builds data and functions around these objects. The data of an object can be accessed only by the functions associated with that object. However, functions of one object can access the functions of other objects.  

 

img4.png

 

Striking features of object-oriented programming are:

img3.png Emphasis is on data rather than procedure.

img3.png Programs are divided into what are known as objects.

img3.png Data structures are designed such that they characterize the objects.

img3.png Functions that operate on the data of an object are tied together in the data structure.

img3.png Data is hidden and cannot be accessed by external functions.

img3.png Objects may communicate with each other through functions.

img3.png New data and functions can be easily added whenever necessary.

img3.png Follow bottom-up approach in program design. In bottom up approach, first we design all the sub modules related to application then we design main module.

 

 

C++ Console I/O:

 

In C programming language, we are using printf() function for output operation and scanf() function for input operation. But in C++, cout<< is using for output operation and cin>> function is using for input operation. “<<” operator is called insertion or put to operator . “>>”  operator is called extraction or get from operator.

 

 

Example:

 

cout<<”Object-Oriented Programming ”;

cout<<100.99;

 

int num;

cin>>num;

 

 

 

Program:

 

1) #include<iostream.h>

//C++ console i/o operator

int main()

{

int i, j;

double d;

i=10;

j=20;

d= 99.101;

cout<<"Here are some values:";

cout<<i;

cout<<' ';

cout<<j;

cout<<' ';

cout<<d;

return 0;

}

 

Output:

Here are some values- 10 20 99.101

 

Assignment:

Write a program that uses C++ style I/O and produce the output as:          

 6.62 9.34 99 110 

 

 

#include<iostream.h>

 

int main()

{

int i;

float f;

char s1[40], s2[40], s3[40];

cout<<"Enter an integer, float and string:";

cin>>i>>f>>s1>>s2>>s3;

cout<<"Here's your data:";

cout<<i<<' '<<f<<' '<<s1<<' '<<s2<<' '<<s3;

return 0;

}

  

 

C++ Comments  

 

In C++, we can include comments in our program two different ways such as-

1. For multiline comments, began a comment with /* and end it with */

2. For single line comments, began a comment with // and stop at the end of the line. 

 

Program:

 

3)#include<iostream.h>

//Comments program

int main()

{

char ch;   //this is a single line comment

/* this is a

multi line comment*/

cout<<"Enter keys, x to stop.\n";

do

{

cout<<":";

cin>>ch;

}while(ch!='x');

return 0;

}

output:

enter keys, x to stop:

dgsdds sdsd

x