Unit-2 C++ Basics-Lesson 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIT 2: C++ Basics

UNIT

2 C++
BASICS

This lesson covers an overview of C++ as a


programming language and the steps required to
process a program written in C++. CodeBlocks
will be introduced as an integrated development
environment that provides comprehensive
facilities to computer programmers for software
development.
This lesson discusses the basic elements of C++,
which are symbols, identifiers, reserved words,
data types, operators, and expressions. The
students will become familiar with the basic
structure of a C++ program and input/output
fundamentals which will enable them to begin
writing a simple program.

IT 103: COMPUTER PROGRAMMING 1 58


UNIT 2: C++ Basics

1. C++ fully supports object-oriented programming, including the four pillars of object-oriented
development.

LESSON 1:
An Overview of C++

OBJECTIVES:
At the end of the lesson, students will be able to:

▪ Identify the basic characteristics of C++.

▪ Enumerate the four pillars in object-oriented programming

▪ Explore how a C++ program is processed

IT 103: COMPUTER PROGRAMMING 1 59


UNIT 2: C++ Basics

The C++ Language

C++ is a statically input, compiled, general-purpose, case-sensitive, free-form


computer programming language that carries object-oriented, procedural, and generic
programming.

C++ is observed as a middle-level language, as it covers a combination of both low-


level and high-level language features.

C++ was created by Bjarne Stroustrup early in1979


at Bell Labs in Murray Hill, New Jersey, as a
development to the C language and originally named
as C with Classes, but far along, and was renamed
C++ in the year 1983.

This C++ is a superset of C, and that virtually any


legal C program is a standard C++ program.
These six frames (from the left) series of images
www.studysection.com
show that a bouncing ball animation can be viewed
when displayed in a rapid sequence.

C++ fully supports object-oriented programming, which includes the four pillars of
object-oriented development: encapsulation, abstraction/data hiding, inheritance, and
polymorphism.

▪ Encapsulation is a procedure of combining data members and functions in a


single unit called class. This is to prevent access to the data directly; the access
to them is provided through the class functions. It is the popular features of
Object-Oriented Programming (OOPs) that helps in data hiding.

How Encapsulation is achieved in a class


To do this:
1) Make all the data members private.
2) Create a public setter and getter functions for each data member in
such a way that the set function sets the value of the data member, and
the get function gets the value of the data member.

▪ Abstraction is a unique feature of Object-Oriented Programming, where you


show only relevant details to the user and hide irrelevant information.

For example, once you send an email to someone, you click send, and you get
the success message. What happens when you click send, how data is
transmitted over the network to the recipient is hidden from you (because it is
irrelevant to you).

▪ Inheritance is one of the features of Object Oriented Programming System


(OOPs). It allows the child class to acquire the properties (the data members)
and functionality (the member functions) of the parent class.
What is child class?
A class that inherits additional class is known as child class. It is also
known as a derived class or subclass.

IT 103: COMPUTER PROGRAMMING 1 60


UNIT 2: C++ Basics

What is the parent class?


The class inherited by other classes is known as a parent class,
superclass, or base class.

▪ Polymorphism is a feature of OOPs that permits the object to behave


differently in different conditions. In C++, we have two types of polymorphism:
1) Compile-time Polymorphism – This is also known as static (or early)
binding.
2) Runtime Polymorphism – This is also identified as dynamic (or late)
binding.1

Standard Libraries

Standard C++ consists of three important parts:

▪ The core language is giving all the building blocks, including variables, data
types, and literals, etc.
▪ The C++ Standard Library is giving a rich set of functions operating files,
strings, etc.
▪ The Standard Template Library (STL) provides a rich set of methods
operating data structures, etc.

The ANSI Standard

ANSI is the American member of ISO, the international standards organization. Since
1998, C++ has an international standard, a sort of a treaty between compiler vendors
from all around the world on what language features they all agree to implement. C++,
as defined by that standard, is known as ISO C++. The ANSI standard is an aim to
ensure that C++ is portable -- that code you write or compose for Microsoft compiler
will be compiled without errors, using a compiler on Mac, UNIX, Windows box, or an
Alpha.

Use of C++

▪ C++ is highly used to write device drivers and other software that rely on direct
manipulation of hardware under real-time constraints.

▪ C++ is computer programmers in virtually every application domain.

▪ C++ is broadly used for teaching and research because it is clean enough to
successfully teach basic concepts.

▪ A programmer who has used either an Apple Macintosh or a PC running


Windows has indirectly used C++ because the primary user interfaces are
written in C++.

IT 103: COMPUTER PROGRAMMING 1 61


UNIT 2: C++ Basics

Processing a C++ Program

In the previous lessons, we discussed machine language and high-level languages. A


computer can only understand machine language. There are steps required to process
a program written in C++, as discussed in the subsequent section.
1. A text editor is used in creating a C++ program using the rules or syntax of the
programming language. The written program is called the source code, or
source program and must be saved in a text file format that has the extension
.cpp.

The source program is a program written in a high-level language.


2. C++ statements are processed by a program called preprocessor. After
processing preprocessor directives, the next phase is to verify that the program
follows the programming language rule - that is, the program is syntactically
correct - and translate the program into the corresponding machine language.
The compiler checks the source program for syntax errors and then
translates into the comparable machine program if there are no errors. The
corresponding machine language program is called an object program.
Preprocessor directives are program lines included in the code of
programs preceded by a hash sign (#). These lines are not program
statements but commands for the preprocessor. 2
The preprocessor observes the code before the actual compilation of
code begins and resolves all these directives before regular statements
generate any code.
The preprocessor provides the ability to include header
files, macro expansions, conditional compilation, and line control.
Object program is known as the machine language version of the high-
level language program.
3. The programs that you write/compose in a high-level language are developed
using an integrated development environment (IDE). The IDE provides
programs that are useful in developing your program. Once the program is
designed and successfully compiled, you must still bring the code for the
resources used from IDE into your program to produce a final program that the
computer can execute. This prewritten code(program) exists in a place called
the library. A program known as linker combines the object program with the
programs from libraries.
The linker is a program that gathers the object program with other
programs in the library, and it is used in the computer program to create
the executable code.
4. The executable program must be loaded into the main memory for execution.
A program called a loader accomplishes this task.
The loader is a program that loads an executable program into the main
memory.
5. The final step is to execute the program.

IT 103: COMPUTER PROGRAMMING 1 62


UNIT 2: C++ Basics

Processing a C++ Program Flowchart

IT 103: COMPUTER PROGRAMMING 1 63

You might also like