Week 4-5 PDF
Week 4-5 PDF
Week 4 and 5
Chapter 2 Language Features
Introduction
C++ is quite similar to C programming. In fact, C++ supports all the features offered
with C with the addition to various other important features like object-oriented
programming, operator overloading, exception and error handling, the namespace feature,
and many more. We can say C++ is the advanced version of C programming. Features of
C++ give multiple reasons to upgrade our skills from C to C++.
In simple terms, portability refers to using the same piece of code in varied environments.
Let us understand this C++ feature with the help of an example. Suppose you write a piece of
code to find the name, age, and salary of an employee in Microsoft Windows and for some
apparent reason you want to switch your operating system to LINUX. This code will work in a
similar fashion as it did in Windows.
3. Simple
When we start off with a new language, we expect to understand in depth. The simple
context of C++ gives an appeal to programmers, who are eager to learn a new programming
language.
If you are already familiar with C, then you don’t need to worry about facing any trouble
while working in C++. The syntax of C++ is almost similar to that of C. Afterall C++ is referred
to as “C with classes”.
It is important to note that C++ is a high-level programming language, unlike C which is a mid-
level programming language. It makes it easier for the user to work in C++ as a high-level
language as we can closely associate it with the human-comprehensible language, that is,
English.
5. Popular
After learning C, it is the base language for many other popular programming languages
which supports the feature of object-oriented programming. Bjarne Stroustrup found Simula
67, the first object-oriented language ever, lacking simulations and decided to develop C++.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
6. Case sensitive
Just like C, it is pretty clear that the C++ programming language treats the uppercase and
lowercase characters in a different manner. For instance, the meaning of the
keyword ‘cout’ changes if we write it as ‘Cout’ or “COUT”. Other programming languages
like HTML and MySQL are not case sensitive.
7. Compiler-Based
Unlike Java and Python that are interpreter-based, C++ is a compiler based language and
hence it a relatively much faster than Python and Java.
Since C++ supports the use of pointers, it allows us to allocate memory dynamically. We may
even use constructors and destructors while working with classes and objects in C++.
9. Existence of Libraries
The C++ programming language offers a library full of in-built functions that make things easy
for the programmer. These functions can be accessed by including suitable header files.
10. Speed
As discussed earlier, C++ is compiler-based hence it is much faster than other programming
languages like Python and Java that are interpreter-based.
C C++
C was developed by Dennis Ritchie between C++ was developed by Bjarne Stroustrup in
1969 and 1973 at AT&T Bell Labs. 1979 with C++'s predecessor "C with
Classes".
When compared to C++, C is a subset of C++. C++ is a superset of C. C++ can run most of C
code while C cannot run C++ code.
In C, data are free entities and can be In C++, Encapsulation hides the data to
manipulated by outside code. This is because ensure that data structures and operators
C does not support information hiding. are used as intended.
C does not support function and operator C++ supports both function and operator
overloading. overloading.
C does not allow functions to be defined In C++, functions can be used inside a
inside structures. structure.
C does not have namespace feature. C++ uses NAMESPACE which avoid name
collisions.
A namespace is a declarative region that
provides a scope to the identifiers (the
names of types, functions, variables, etc)
inside it. Namespaces are used to organize
code into logical groups and to prevent
name collisions that can occur especially
when your code base includes multiple
libraries. All identifiers at namespace scope
are visible to one another without
qualification. Identifiers outside the
namespace can access the members by
using the fully qualified name for each
identifier.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
C uses functions for input/output. For C++ uses objects for input output. For
example scanf and printf. example cin and cout.
C has no support for virtual and friend C++ supports virtual and friend functions.
functions.
C provides malloc() and calloc() functions for C++ provides new operator for memory
dynamic memory allocation, and free() for allocation and delete operator for memory
memory de-allocation. de-allocation.
C does not provide direct support for error C++ provides support for exception handling.
handling (also called exception handling) Exceptions are used for "hard" errors that
make the code incorrect.
C++, on the contrary, is an object-oriented programming language. Here the data of the
problem is the main focus and the classes are built around this data. Functions operate on
the data and closely bound to data.
C++ follows a bottom-up approach to programming. In this, we start with low-level design or
coding and then build on this low-level design to get a high-level solution.
C++, on the other hand, is more suitable for server-side applications, network applications or
for applications like gaming, etc.
However, C language does not support object-oriented features of C++ and hence it is not
compatible with C++ programs. Therefore programs written in C++ will not run on C
compilers.
In C++, as we are dealing with classes and objects, the main building block of the program is
Data. Thus, data is tightly secured using classes, access specifiers, encapsulation, etc.
A C++ program is divided into classes and objects. The problem is designed into classes and
the objects of these classes are the executing units that are created by the main functions
and are executed.
In C++, the data is read from the standard input device using ‘cin’ while it is printed to the
output device using ‘cout’.
#11) Focus/Emphasis:
Being a procedural language, C has more emphasis on the sequence of steps or procedures
to solve a problem.
C++, on the other hand, is object-oriented and thus puts more focus on objects and classes
around which the solution is to be built.
However, in C language, we can have a main() function called by the other functions in the
code.
#13) Variable:
Variables need to be declared at the beginning of the function block in C, on the contrary, we
can declare variables anywhere in a C++ program provided they are declared before they are
used in the code.
References act as aliases for the variables and point to the same memory location as a
variable.
C language only supports pointers and not references. C++ supports pointers as well as
references.
#16) Enumerations:
We can declare enumerations in C as well as C++. But in C, the enumeration constants are of
Integer type. It is the same as declaring an integer constant without any type of safety.
In C++, the enumerations are different. They are of distinct types. Thus to assign an integer
type to a variable of an enumerated type, we need explicit type conversion.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
#17) Strings:
As far as strings are concerned, the declaration ‘char []’ declares a string array. But when the
string declared as above is passed between the functions, then there is no guarantee that it
will not be changed by the other external functions as these strings are mutable.
This drawback is not there in C++ as C++ supports string data type that defines immutable
strings.
The C language does not support default parameters. Whereas C++ supports the use of
default arguments.
#20) Structures:
Structures in C and C++ use the same concept. But the difference is, in C, as we cannot
include functions as members.
On the other hand, C++ supports the concept of classes and objects and almost all the
applications in C++ are built around classes and objects.
Apart from this C++ also supports Boolean and string data types which are not supported by
C.
#24) Inheritance:
Inheritance is also an important feature of object-oriented programming that is supported by
C++ and not C.
#25) Functions:
C does not support functions with default arrangements like default parameters etc. C++
supports functions with default arrangements.
#26) Namespace:
Namespaces are not supported in C but are supported by C++.
#27) Source Code:
C is a free-format language that gives us the ability to program anything. C++ is derived from
C and also has object-oriented programming features which make it more efficient as far as
the source code is concerned.
#28) Abstraction:
Abstraction is the way to hide the implementation details and expose only the required
interface to the user. This is one of the distinguishing features of Object-oriented
programming.
#29) Encapsulation:
Encapsulation is a technique using which we encapsulate the data from the outside world.
This aids in information hiding.
C++ uses classes which bundle data and the functions operating on this data in a single unit.
This is encapsulation. C does not have this feature.
C++ puts great emphasis on data and uses abstraction and encapsulation for information
hiding.
C doesn’t put any emphasis on data and does not deal with information hiding.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
#31) Polymorphism:
Polymorphism simply means that one object has many forms and it is an essential feature of
object-oriented programming. Being an object-oriented language, C++ supports
polymorphism.
C has no support for object-oriented programming and does not support polymorphism.
However, we can simulate the dynamic dispatch of functions in C using function pointers.
#34) Mapping:
As far as the mapping of data with functions is concerned, C language is very complicated as
it does not keep any focus on data.
Whereas C++ has a good mapping of data and functions as it supports classes and objects
that bind data and functions together.
In C we use functions like malloc (), calloc (), realloc (), etc., to allocate memory and free ()
function to free the memory. But, in C++, we use new () and delete () operators to allocate
and deallocate the memory respectively.
In C, ‘stdio.h’ is the default header used while C++ uses <iostream> as the default header.
#37) Exception/Error Handling:
C++ supports exception/error handling using the try-catch blocks. C doesn’t support
exception handling directly but we can handle errors using some workaround.
#38) Keywords:
C++ supports a lot more keywords than that of C. In fact, C has only 32 keywords whereas
C++ has 52 keywords.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
#39) Templates:
Templates allow us to define classes and objects independent of the data type. Using
templates, we can write generic code and call it for any data type.
C++ being object-oriented uses classes and objects and thus supports templates. C, on the
other hand, doesn’t support the concept of templates.
5 Compatibility with Not Compatible with C++. Compatible with C as C++ is a subset of C.
each other
7 Ease of coding Allows us to code everything. Comes with highly advanced Object-
Oriented concepts.
9 Program division Program divided into Program divided into classes and objects.
functions.
No Characteristics C C++
12 The main() function Can call main through other Not possible to call main from any point.
functions.
22 Data Types Only built-in and primitive Boolean and string types supported in
data types are supported. addition to built-in data types.
No Boolean and string types.
No Characteristics C C++
34 Mapping Cannot easily map data and Data and functions can be easily mapped.
functions.
1
bool
Stores either value true or false.
2
char
Typically a single octet (one byte). This is an integer type.
3
int
The most natural size of integer for the machine.
4
float
A single-precision floating point value.
5
double
A double-precision floating point value.
6
void
Represents the absence of type.
7
wchar_t
A wide character type.
C++ also allows to define various other types of variables, which we will cover in subsequent
chapters like Enumeration, Pointer, Array, Reference, Data structures, and Classes.
Following section will cover how to define, declare and use various types of variables.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
Example
Try the following example where a variable has been declared at the top, but it has been
defined inside the main function −
#include <iostream>
using namespace std;
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
// Variable definition:
int a, b;
int c;
float f;
// actual initialization
a = 10;
b = 20;
c = a + b;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
When the above code is compiled and executed, it produces the following result −
30
23.3333
Same concept applies on function declaration where you provide a function name at the time
of its declaration and its actual definition can be given anywhere else. For example −
// function declaration
int func();
int main() {
// function call
int i = func();
}
// function definition
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
int func() {
return 0;
}
Lvalues and Rvalues
There are two kinds of expressions in C++ −
• lvalue − Expressions that refer to a memory location is called "lvalue" expression. An
lvalue may appear as either the left-hand or right-hand side of an assignment.
• rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it which
means an rvalue may appear on the right- but not left-hand side of an assignment.
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric
literals are rvalues and so may not be assigned and can not appear on the left-hand side.
Following is a valid statement −
int g = 20;
But the following is not a valid statement and would generate compile-time error −
10 = 20;
There are some rules that must be in your knowledge to work with C++ variables.
• A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
the underscore character.
• The first character must be a letter or underscore.
• Blank spaces cannot be used in variable names.
• Special characters like #, $ are not allowed.
• C++ keywords cannot be used as variable names.
• Variable names are case-sensitive.
• A variable name can be consisting of 31 characters only if we declare a variable more
than one characters compiler will ignore after 31 characters.
• Variable type can be bool, char, int, float, double, void or wchar_t.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
Local Variables
Variables that are declared inside a function or block are local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not
known to functions outside their own. Following is the example using local variables −
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Global Variables
Global variables are defined outside of all the functions, usually on top of the program. The
global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. Following is the example using global
and local variables −
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
cout << g;
return 0;
}
A program can have same name for local and global variables but value of local variable inside
a function will take preference. For example −
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int g = 10;
cout << g;
return 0;
}
When the above code is compiled and
executed, it produces the following result
−
10
Initializing Local and Global Variables
When a local variable is defined, it is not
initialized by the system, you must
initialize it yourself. Global variables are
initialized automatically by the system when you define them as follows −
int 0
char '\0'
float 0
double 0
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
pointer NULL
Learning Resources
1. CBSUA Website
2. Student Handbook
3. Fundamentals of Computing 1 (SY 2020-2021) Syllabus
4. Google.com
5. PowerPoint Presentation
Tasks/Activities
A Facebook group chat will be created where the students can discuss their ideas and thoughts. Chat
in Facebook Group allow us to communicate between users in a chat channel. Virtual Learning Portal
(VLP) will be the main source of lessons on the topic. This platform will be utilized throughout the
course.
The student will be assessed by filling out a questionnaire in google doc regarding the lessons that
have been presented. This assessment will determine the knowledge that they’ve learned with the
Video Lectures Presentation.
Instructions:
Tasks/Activities