0% found this document useful (0 votes)
15 views

Chapter 2 BCP

Uploaded by

Dej Ayn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter 2 BCP

Uploaded by

Dej Ayn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

Chapter 2
Fundamentals of C++ Programming Language
2.1 A brief history of C++
The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was
doing work for his Ph.D. thesis. He began work on "C with Classes", which as the name implies
was meant to be a superset of the C language. His goal was to add object-oriented programming
into the C language, which was and still is a language well-respected for its portability without
sacrificing speed or low-level functionality.
His language included classes, basic inheritance, in lining, default function arguments, and
strong type checking in addition to all the features of the C language. The first C with Classes
compiler was called Cfront, which was derived from a C compiler called CPre. It was a program
designed to translate C with Classes code to ordinary C.
In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in
the C language is an operator for incrementing a variable, which gives some insight into how
Stroustrup regarded the language. Many new features were added around this time, the most
notable of which are virtual functions, function overloading, references with the & symbol, the
const keyword, and single-line comments using two forward slashes.
In 1985, C++ was implemented as a commercial product. The language was not officially
standardized yet. The language was updated again in 1989 to include protected and static
members, as well as an inheritance from several classes.
In 1990, Turbo C++ was released as a commercial product. Turbo C++ added a lot of additional
libraries which have had a considerable impact on C++'s development. In 1998, the C++
standards committee published the first international standard for C++ ISO/IEC 14882:1998,
which is informally known as C++98.
2.2 Major programming paradigms
The major land marks in the programming world are the different kinds of features or properties
observed in the development of programming languages. Among these the following are worth
mentioning: Procedural, Structured and Object Oriented Programming Paradigms.
2.2.1 Procedural programming
 Procedural programming is a programming paradigm based upon the
concept of procedure call. Procedural programming is often a better choice
than simple sequential programming in many situations which involve
moderate complexity or which require significant ease of maintainability.
 Possible benefits: the ability to re-use the same code (function or procedure)
at different places, an easier way to keep track of program flow than a
collection of “GO TO” or “JUMP” statements.

BASIC COMPUTER PROGRAMMING 1


ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

2.2.2 Structured programming


 Process of writing a program in small, independent parts. This makes it easier
to control a program's development and to design and test its individual
component parts.
 Structured programs are built up from units called modules, which normally
correspond to single procedures or functions.
 Can be seen as a subset or sub discipline of procedural programming. It is
most famous for removing or reducing reliance on the GO TO statement.
2.2.3 Object-Oriented Programming
 The idea behind OOP is that, a computer program is composed of a collection
of individual units, or objects as opposed to traditional view in which a
program is a list of instructions to the computer.
 Object-oriented programming is claimed to give more flexibility, easing
changes to programs. The OOP approach is often simpler to develop and
maintain.
2.3 Object Oriented Programming
A programming language structure wherein the data and their associated processing ("methods")
are defined as self-contained entities called "objects." The norm today, object-oriented
programming (OOP) languages, such as C++ and Java, provide a formal set of rules for creating
and managing objects. The data are stored in a traditional relational database or in an object
database if the data have a complex structure.
2.3.1 Basic terminologies in Object-Oriented-Programming
 Object: The instance of a class / it’s the working entity of a class
 Class: This is the model or standard about the capability of what an object can
do
 Method: Can modify a class state that would apply across all the instances of
the class
 Instance: These are like Objects, however, let’s think about it in these terms:
A blueprint for a car design is the class description, all the cars manufactured
from that blueprint are objects of that class. Your car that has been made from
that blueprint is an instance of that class.
2.3.2 Features in Object Oriented Programming (OOP):
i. Encapsulation: Encapsulation is accomplished when each object maintains a private
state, inside a class. Encapsulation is an Object Oriented Programming
concept that binds together the data and functions that manipulate the data, and that
keeps both safe from outside interference and misuse. Data encapsulation led to the
important OOP concept of data hiding.
ii. Abstraction: Abstraction is an extension of encapsulation. It is the process of
selecting data from a larger pool to show only the relevant details to the object.
Data Abstraction is a process of providing only the essential details to the outside
world and hiding the internal details, i.e., representing only the essential details in the

BASIC COMPUTER PROGRAMMING 2


ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

program. Data Abstraction is a programming technique that depends on the separation


of the interface and implementation details of the program.
iii. Inheritance: In C++, inheritance is a process in which one object acquires all the
properties and behaviors of its parent object automatically. In such way, you can
reuse, extend or modify the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is
the specialized class for the base class.
iv. Polymorphism: The word polymorphism means having many forms. Typically,
polymorphism occurs when there is a hierarchy of classes and they are related by
inheritance. C++ polymorphism means that a call to a member function will cause a
different function to be executed depending on the type of object that invokes the
function.
2.4 The structure of C++ programs
To understand the basic parts of a simple program in C++, let’s have a look at the following
code:
#include<iostream.h>
#include<conio.h>
void main()
{
cout<<”\n Hello World!”;
getch();
}
Any C++ program file should be saved with file name extension “.CPP”.
Type the program directly into the editor, and save the file as hello.cpp, compile it and
then run it. It will print the words Hello World! on the computer screen. The first character is the
#. This character is a signal to the preprocessor. Each time you start your compiler, the
preprocessor runs through the program and looks for the pound (#) symbols and act on those
lines before the compiler runs.
The “include” instruction is a preprocessor instruction that directs the compiler to include a copy
of the file specified in the angle brackets in the source code.
If the path of the file is not specified, the preprocessor looks for the file under c:\tc\include\
folder or in include folder of the location where the editor is stored.
The effects of line 1, i.e. include <iostream.h> is to include the file iostream.h into the program
as if the programmer had actually typed it.
When the program starts, main() is called automatically.
Every C++ program has a main() function.
The return value type for main() here is void, which means main function will not return a value
to the caller (which is the operating system).

BASIC COMPUTER PROGRAMMING 3


ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

The main function can be made to return a value to the operating system.
The Left French brace “{“signals the beginning of the main function body and the corresponding
Right French Brace “}” signals the end of the main function body. Every Left French Brace
needs to have a corresponding Right French Brace.
The lines we find between the braces are statements or said to be the body of the function.
A statement is a computation step which may produce a value or interact with input and output
streams. The end of a single statement ends with semicolon (;). The statement in the above
example causes the sting “Hello World!” to be sent to the “cout” stream which will display it on
the computer screen.
2.5 Compilation process of C++
Each C++ source file needs to be compiled into an object file. The object files resulting from the
compilation of multiple source files are then linked into an executable, a shared library, or a
static library (the last of these being just an archive of object files). C++ source files generally
have the .cpp, .cxx or .cc extension suffixes.
A C++ source file can include other files, known as header files, with the #include directive.
Header files have extensions like .h, .hpp, or .hxx, or have no extension at all like in the C++
standard library and other libraries’ header files (like Qt). The extension doesn’t matter for the
C++ preprocessor, which will literally replace the line containing the #include directive with the
entire content of the included file.
The first step that the compiler will do on a source file is run the preprocessor on it. Only source
files are passed to the compiler (to preprocess and compile it). Header files aren’t passed to the
compiler. Instead, they are included from source files.
Each header file can be opened multiple times during the preprocessing phase of all source files,
depending on how many source files include them, or how many other header files that are
included from source files also include them (there can be many levels of indirection). Source
files, on the other hand, are opened only once by the compiler (and preprocessor), when they are
passed to it.
For each C++ source file, the preprocessor will build a translation unit by inserting content in it
when it finds an #include directive at the same time that it’ll be stripping code out of the source
file and of the headers when it finds conditional compilation blocks whose directive evaluates
to false. It’ll also do some other tasks like macro replacements.
Once the preprocessor finishes creating that (sometimes huge) translation unit, the compiler
starts the compilation phase and produces the object file.
2.6 Input(Cin) / Output(Cout) in C++
Cout: is an object used for printing data to the screen.
 To print a value to the screen, write the word cout, followed by the insertion
operator also called output redirection operator (<<) and the object to be
printed on the screen.
 Syntax: Cout<<Object;
 The object at the right hand side can be:

BASIC COMPUTER PROGRAMMING 4


ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

 A literal string: “Hello World”


 A variable: a place holder in memory
 Cin is an object used for taking input from the keyboard.
 To take input from the keyboard, write the word cin, followed by the input redirection
operator (>>) and the object name to hold the input value.
 Syntax: Cin>>Object
 Cin will take value from the keyboard and store it in the memory.
 Thus the cin statement needs a variable which is a reserved memory place holder.
 Both << and >> return their right operand as their result, enabling multiple input or
multiple output operations to be combined into one statement. The following example
will illustrate how multiple input and output can be performed:
 For example;
 Cin>>var1>>var2>>var3;
 Here three different values will be entered for the three variables. The
input should be separated by a space, tan or newline for each variable.
 Cout<<var1<<”, “<<var2<<” and “<<var3;
 Here the values of the three variables will be printed where there is a “,”
(comma) between the first and the second variables and the “and” word
between the second and the third.
2.7 Comments in C++
A comment is a piece of descriptive text which explains some aspect of a program.
 Program comments are text totally ignored by the compiler and are only
intended to inform the reader how the source code is working at any particular
point in the program.
 C++ provides two types of comment delimiters:
 Single Line Comment: Anything after // {double forward slash} (until the end
of the line on which it appears) is considered a comment.
 Eg: cout<<var1; //this line prints the value of var1
 Multiple Line Comment: Anything enclosed by the pair /* and */ is
considered a comment.
 Eg: /*this is a kind of comment where Multiple lines can be enclosed in one
C++ program */
 Comments should be used to enhance (not to hinder) the readability of a
program. The following two points, in particular, should be noted:
 A comment should be easier to read and understand than the code which it
tries to explain. A confusing or unnecessarily- complex comment is worse
than no comment at all.
 Over-use of comments can lead to even less readability. A Program which
contains so much comment that you can hardly see the code can by no means
be considered readable.

BASIC COMPUTER PROGRAMMING 5


ETHIOPIAN TECHNICAL UNIVERSITY BAHIR DAR SATELLITE CAMPUS

2.8 A simple C++ Programs


Let us look at a simple code that would print the words Hello World.
#include <iostream>
using namespace std;

// main() is where program execution begins.


int main() {
cout << "Hello World"; // prints Hello World
return 0;
}
Let us look at the various parts of the above program:
 The C++ language defines several headers, which contain information that is
either necessary or useful to your program. For this program, the
header <iostream> is needed.
 The line using namespace std; tells the compiler to use the std namespace.
Namespaces are a relatively recent addition to C++.
 The next line '// main() is where program execution begins.' is a single-line
comment available in C++. Single-line comments begin with // and stop at the
end of the line.
 The line int main() is the main function where program execution begins.
 The next line cout << "Hello World"; causes the message "Hello World" to be
displayed on the screen.
 The next line return 0; terminates main( )function and causes it to return the
value 0 to the calling process.

BASIC COMPUTER PROGRAMMING 6

You might also like