Fundamental of Programming I
Fundamental of Programming I
C++ was developed by Bjarne Stroustrup at Bell Laboratories over a period starting in
1979. Since C++ is an attempt to add object-oriented features (plus other improvements) to C,
earlier it was called as “C with Objects”. As the language developed, Stroustrup named it as C++
in 1983.
Procedural and object oriented programming
Procedural Programming
Procedural programming uses a list of instructions to tell the computer what to do step-by-
step. Procedural programming relies on - you guessed it - procedures, also known as routines or
subroutines. A procedure contains a series of computational steps to be carried out. Procedural
programming is also referred to as imperative programming. Procedural programming languages
are also known as top-down languages.
Procedural programming is intuitive in the sense that it is very similar to how you would expect
a program to work. If you want a computer to do something, you should provide step-by-step
instructions on how to do it. It is, therefore, no surprise that most of the early programming
languages are all procedural. Examples of procedural languages include Fortran, COBOL and C,
which have been around since the 1960s and 70s.
Object-Oriented Programming
Object-oriented programming, or OOP, is an approach to problem-solving where all
computations are carried out using objects. An object is a component of a program that knows
how to perform certain actions and how to interact with other elements of the program. Objects
are the basic units of object-oriented programming. A simple example of an object would be a
person. Logically, you would expect a person to have a name. This would be considered a
property of the person. You would also expect a person to be able to do something, such as
walking. This would be considered a method of the person.
A method in object-oriented programming is like a procedure in procedural programming. The
key difference here is that the method is part of an object. In object-oriented programming, you
organize your code by creating objects, and then you can give those objects properties and you
can make them do certain things.
A key aspect of object-oriented programming is the use of classes. A class is a blueprint of an
object. You can think of a class as a concept and the object as the embodiment of that concept.
What is the basic structure of a C++ program?
Structure of a C++ program
A C++ program is structured in a specific and particular manner. In C++, a program is divided
into the following three sections:
1. Standard Libraries Section
2. Main Function Section
3. Function Body Section
For example, let’s look at the implementation of the Hello World program:
4. #include <iostream>
5. using namespace std;
6.
7. int main() {
8. cout << "Hello World!" << endl;
9. return 0;
#include is a specific preprocessor command that effectively copies and pastes the entire
text of the file, specified between the angle brackets, into the source code.
The file <iostream>, which is a standard file that should come with the C++ compiler, is
short for input-output streams. This command contains code for displaying and getting
an input from the user.
namespace is a prefix that is applied to all the names in a certain set.
iostream file defines two names used in this program - cout and endl.
This code is saying: Use the cout and endl tools from the std toolbox.
Main function section
1
int main() {}
The Preprocessor
The preprocessor handles statements or lines of code that begin with the "#" character, which are
called "preprocessor directives." Note that directives are not C++ statements (and therefore do
not end with a semicolon) but rather instruct the preprocessor to carry out some action. The
preprocessor reads and processes each file one at a time from top to bottom. It does not change
the contents of any of the files that it processes but creates a temporary file that contains the
processed code. The compiler component reads and translates the temporary file from C++ to
machine code. When the compiler component finishes processing the code in the temporary file,
it removes the file. Two of the most common directives, and the first that we will use,
are #include and #define.
<iostream> It is used to define the cout, cin and cerr objects, which correspond to standard output stream
standard input stream and standard error stream, respectively.
<iomanip> It is used to declare services useful for performing formatted I/O, such as setprecision and setw
Output:
Output:
Comments in C++
C++ Comments
The C++ comments are statements that are not executed by the compiler. The comments in C++
programming can be used to provide explanation of the code, variable, method or class. By the
help of comments, you can hide the program code also.
There are two types of comments in C++.
o Single Line comment
o Multi Line comment
C++ Single Line Comment
The single line comment starts with // (double slash).
Let's see an example of single line comment in C++.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x = 11; // x is a variable
6. cout<<x<<"\n";
7. }
Output:
11
35