Module - Fundamental of Computing 1
Module - Fundamental of Computing 1
A. Course Overview
This course introduces the fundamental concepts of structured programming and provides a
comprehensive introduction to programming. Topics include basic of C++, basic data types,
control structures, functions, arrays, and the mechanics of running, testing, and debugging.
This course assumes computer literacy. In-class lectures and discussions are supplemented
by computer hands-on sessions.
Google Classroom Code:
B. Course Calendar
Introduction to computer
programming
12 M I D T E R M
Chapter 5 Polymorphism in C++
13 5.1 Concept of Polymorphism
5.2 Importance of Polymorphism
14 5.3 Implementing Polymorphism
5.4 Other Application of
15 Polymorphism
5.5 Polymorphism Explanation
18 F I N A L S
C. Instructor
D. Course Assessments
knowledge and
of ability to
Weeks 13 – 18 Examination 35% CO1, CO4, CO5 December 11 physiological express
processes technical
topics
WEEK 1
Introduction to VMGO, Course Outline, Grading System and the Subject
Introduction
The course will be initiated by discussing the core values of CBSUA to the students to the core. This
will involve PVMGOs and Quality Policy. The course outline, grading system, and class policies shall
also be introduced. Afterward, discussing of Fundamental of Computing and Introduction to
Computer Language. It is important to understand the relationship between the term’s programs,
programmers, and programming languages.
Learning Resources:
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
Computer
is a programmable, multiuse machine that accepts data, (raw, facts and figures)
and process or manipulates, it into information.
Computer Systems
a collection of entities(data, user, hardware, software and liveware) that are
designed to receive, process, manage and present information in a meaningful
format.
Data (information)
• Pieces of facts
• Computer organize and present information
Users
• People operating the computer
• Computer working for the people
• Users are most important part of computers
• Tell the computer what to do
Hardware
• Physical Components of the computer
• TANGIBLE (can be touched)
Software
Step-by-step instructions to perform the task
Also called a program
INTANGIBLE (cannot be touched)
Programs and software interchangeable terms
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
Input Devices
These devices allow user to enter the data into the computer.
Output Devices
I/O Devices
• Some devices are both input as well as output devices.
• Can perform I/O simultaneously
• Touchpad Screens
• CPU instruction set is built to the Control Unit called Commands that a CPU can
execute.
Storage Devices
Software
Types of Software
• SYSTEM SOFTWARE
• APPLICATION SOFTWARE
System Software
• Enables the application to interact with the computer and manages the computer
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
internal resources.
- Operating System
- Device Drivers
Application Software
Utility Programs
Types of Computers
1. Supercomputers
2. Mainframes
3. Minicomputers
4. Microcontrollers
Supercomputer
Most powerful computers
Physically largest in size
Hundreds of thousands of processors that can process huge amounts of data
Perform over 1 quadrillion calculations per second. e.g. IBM ASCI white
Ideal for handling large and highly complex problems that require extreme calculating
power
Mainframe
Mainly used by large organizations for critical application, typically bulk data
processing.
- Banks, Airlines, Insurance Companies
Measured in millions of integer operations per second (MIPS)
Vary in size from small, to medium, to large, depending on their use.
Normally Dumb Terminals are connected to these main frames. Processing is done by
Mainframes.
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
Minicomputers
Class of multi-user computers that lies in between mainframe computers (multi user)
and microcomputers or personal computers (single user)
Midrange computer, such as the higher-end SPARC, POWER and Itanium-based
systems from sun microsystem, IBM and Hewlett-Packard
Six primary types of computers are designed to be used by one person at a time:
• Desktop computers
• Workstations
• Notebook (or laptop) computers
• Tablet computers
• Handheld computers
• Smart phones
DESKTOP COMPUTERS
WORKSTATIONS
NOTEBOOK COMPUTERS
TABLET PCS
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
WEEK 2 and 3
Chapter 1 Basic of C++
Introduction
The course will be initiated by discussing the basic of C++, such as OOP’s Concept, syntax and
structure of C++, data types and variables in C++ programming language.
The major difference being OOPS concept, C++ is an object-oriented language whereas C language is
a procedural language. Apart from this there are many other features of C++ which gives this
language an upper hand on C language.
Object Oriented programming is a programming style that is associated with the concept of Class,
Object and various other concepts revolving around these two, like Inheritance, Polymorphism,
Abstraction, Encapsulation etc.
Encapsulation
Abstraction
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
Now before studying how to define class and its objects, lets first quickly learn what are access
specifies. Access specifies in C++ class defines the access control rules. C++ has 3 new keywords
introduce,
Public
Private
Protected
This access specifies are used to set boundaries for availability of members of class be it data
members or member functions Access specifies in the program, are followed by a colon. You can use
either one, two or all 3 specifies in the same class to set different boundaries for different class
members. They change the boundary for all the declarations that follow them.
Public
-means all the class members declared under public will be available to everyone. The data members
and member functions declared public can be accessed by other classes too. Hence there are
chances that they might change them. So, the key members must not be declared public.
Private
-means that no one can access the class members declared private outside that class. If someone
tries to access the private member, they will get a compile time error. By default, class variables and
member functions are private.
Protected
-is the last access specified, and it is similar to private, it makes class member inaccessible outside
the class. But they can be accessed by any subclass of that class. (if class A is inherited by class B,
then class B is subclass of class A.
Here we will discuss one simple and basic C++ program to print “Helo this is C++” and its structure in
parts with details and uses.
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
#include <iostream.h>
int main()
Header files are included at the beginning just like in C programming. Here iostream is a header file
which provides us with input & output streams.
Using namespace std tells the compiler to use standard namespace. Namespace collects identifiers
used for class, object and variables. Namespace can be used by two ways in a program, either by the
use of using statement at the beginning, like we did in above mentioned program or by using name
of namespace as prefix befire the identifier with scope resolution (::) operator.
main() is the function which holds the executing part of program its return type is int.
cout<< is used to print anything in scree, same as printf in C language. cin and cout are the same as
scanf and printf, only difference is that you do not need to mention format specifiers like, %d for int
etc, in cout & cin.
Comments
For single line comments, use // before mentioning comment, like cout<<”single line”; // This is
single line comment
Using Classes
Classes name must start with capital letter, and they contain data variables and member functions.
This is a mere introduction to classes,
class Abc
{ Remember
cout <<”Inside Member Function”; Variables can be declared anywhere
} in the entire program, but must be
declared, before they are used.
}; // Class ends here Hence, we don’t need to declare
variable at the start of the program.
int main()
This is how class is defined, its object is created and the member functions are used.
They are used to define type of variables and contents used. Data types define the way you use
storage in the programs you write. Data types can be built in or abstract.
These are the data types which are predefined and are wired directly into the compiler. Eg: int, char
etc.
These are the type, that user creates as a class. In C++ these are classes where as in C it was
implemented by structures.
Example:
Enumerated type declares a new type-name and sequence of value containing identifiers which has
values starting from 0 and incrementing by 1 every time.
Modifiers
Specifiers modify the meaning of the predefined built-in data types and expand them to a much
larger set. There are four data type modifiers in C++, they are:
Long
Short
Signed
Unsigned
Below mentioned are some important points you must know about the modifiers.
Long and short modify the maximum and minimum values that a data type will hold.
A plain int must have a minimum size of short.
Size hierarchy : short int <int <long int
Size hierarchy for floating point numbers is : float <double <long double
Long float is not a legal type and there are no short floating point numbers.
Signed types includes both positive and negative numbers and is the default type.
Unsigned numbers are always without any sign, that is always positive.
Variable are used in C++, where we need storage for any value, which will change in program.
Variable can be declared in multiple ways each with different memory requirements and
functioning. Variable is the name of memory location allocated by the compiler depending upon the
data type of the variable.
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
RAM
Each variable while declaration must be given a datatype, on which the memory assigned to the
variable depends. Following are the basic types of variables,
Variables must be declared before they are used. Usually it is preferred to declare them at the
starting of the program, but in C++ they can be declared in the middle of program too, but must be
done before using them.
Example :
char c;
int I; // declaration
i = 10; // initialization
if a variable is declared and not initialized by default it will hold a garbage value. Also, if a variable is
once declared and if try declare it again, we will get a compile time error.
int i,j;
i=10;
j=20;
int j=i+j; // compile time error, cannot redeclare a variable in same scope
All the variables have their area of functioning, and out of that boundary they don’t hold their value,
this boundary is called scope of the variable. For most of the cases its between the curly braces, in
which variable is declared that a variable exists, not outside it. We still study the storage classes
later, but as of now, we can broadly divide variables into two main types,
Global Variables
Local variables
Global variables
Global variables are those, which are once declared and can be used throughout the lifetime of the
program by any class or any function. They must be declared outside the main() function. If only
declared, they can be assigned different values at different time in program lifetime. But even if they
are declared and initialized at the same time outside the main() function, then also they can be
assigned any value at any point in the program.
Include <iostream>
int main()
}
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
Local variables are the variables which exist only between the curly braces, in which its declared.
Outside that they are unavailable and leads to compile time error.
Example:
include <iostream>
int main()
int i = 10;
There are also some special keyword, to impart unique characteristics to the variables in the
program. Following two are mostly used,
Example:
#include<iostream.h>
int main()
}
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
Operators are special type of functions that takes one or more arguments and produces a new value.
For example: addition (+),
Subtraction (-), multiplication (*) etc., are all operators. Operators are used to perform various
operations on variables and constants.
Assignment Operator
Mathematical Operators
Relational Operators
Logical Operators
Bitwise Operators
Shift Operators
Unary Operators
Ternary Operator
Comma Operator
Operates ‘=’ is used for assignment, it takes the right-hand side (called rvalue) and copy it into the
left-hand side (called lvalue). Assignment operator is the only operator which can be overloaded but
cannot be inherited.
Mathematical Operators
There are operator used to perform basic mathematical operations. Addition (+), Subtraction (-),
diversion(/) multiplication (*) and modulus(%) are the basic mathematical operators. Modulus
operator cannot be used with floating-point numbers.
C++ and C also use a shorthand notation to perform an operation and assignment at same type.
Example,
int x=10;
Relational Operators
These operators establish a relationship between operands. The relational operators are: less than
(<), greater than (>), less than or equal to (<=), greater than equal to (>=), equivalent (==) and not
equivalent (!=).
You must notice that assignment operator is (=) and there is a relational operator, for equivalent
(==). These two are different from each other, the assignment operator assigns the value to any
variable, whereas equivalent operator is used to compare values, life in if-else conditions,
Example
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
Logical Operators
The logical operators are AND (&&) and OR (||). They are used to combine two different expressions
together.
Bitwise Operators
There are used to change individual bits into a number. They work with only integral data types like
char, int and long and not with floating point values.
They can be used as shorthand notation too, &=, |=, ^=, ˜= etc.
Shift Operators
Shift operators are used to shift Bits of any variables. It is of three types,
Unary Operators
These are the operators which work on only one operand. There are many unary operators, but
increment ++ and decrement - - operators are most used.
Other Unary Operators: address of & dereference *, new and delete bitwise not ˜, logical not !,
unary minus – and unary plus +.
Ternary Operator
int a = 10;
Comma operator
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
This is used to separate variables names and to separate expressions. In case expressions, the value
of last expression is produced and used.
Example:
sizeOf is also an operator not a function, it is used to get information about the amount of memory
allocated for data types & Objects. It can be used to get size of user defined data types too. Sizeof
operator can be used with and without parentheses. cout << sizeof(double); //will print size of
double
int x=2;
int i=sizeOf x;
typedef is a keyword used in C language to assign alternative names to existing types. Its mostly
used with user defined data types, when names of data types get slightly complicated. Following is
the general syntax for using typedef,
the above statement defines a term ulong for an unsigned long type. Now this ulong identifier can
be used to define unsigned long type variables.
Again, the code inside the body of loop is executed and the value of ‘n’ becomes 2. When the value
of ‘n’ to 11. After this, the condition becomes false and the loop terminates.
typedef cab be used to give an alias name to pointers also. Here we have a case in which use of
typedef is beneficial during pointer declaration.
int* x,y;
by this declaration statement, we are actually declaring x as a pointer of type int, whereas y will be
declared as a plain integer.
A computer is used for performing many repetitive types of tasks the process of repeatedly
performing tasks is known as looping. The statements in the block may be executed any number of
times from Zero to Up to the Condition is True. The loop is that in which a task is repeated until the
condition is true or we can say in the loop will Executes all the statements are until the given
condition is not to be false.
1. Initialization
2. Condition check
3. Increment
Playing with loops makes programming fun. Before we try to understand loop, you should be
thorough with all the previous topics of C++
One way to do this is to print the first 10 natural numbers individually using cout. But what if you are
asked to print the first 100 natural numbers! You can easily do this with the help of loops.
1. While
2. do-while
3. for
all these are use for performing the repetitive tasks until the given conditions is not true.
1.8.1 While
While loop is known as Entry Controlled Loop because in the while loop first we initialize the value of
variable or starting point of execution and then we check the condition and if the condition is true
then it will execute the statements and then after it increments or decrements the value of a
variable. But in the while loop if a condition is false then it will never execute the statement so that
lets first look at the syntax of while loop.
while(condition)
Statement(s)
While loop checks whether the conditions written in () is true or not. If the condition is true, the
statements written in the body of the while loop i.e, inside the braces () are executed. These again
the condition is checked and found true, again the statements in the body of the while loop are
executed. This process continues until the condition becomes false.
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
#include<iostream>
int main(){
int n=1;
n++;
return 0;
Output
In our example, firstly we assigned a value 1 to a variable ‘n’. while (n <= 10) -checks the condition ‘n
<=10’. Since the value of n is 1 which is less than 10, the statements within the braces { } are
executed.
The value of ‘n’ i.e 1 is printed and n++ increases the value of ‘n’ by 1. So, now the value of ‘n’
becomes 2.
Now, again the condition is checked, this time also ‘n <=10’ is true because the value of ‘n’ is 2. So,
again the value of ‘n’ i.e 2 gets printed and the value of ‘n’ wo;; be increased to 3.
When the value of ‘n’ becomes 1-, again the condition ‘n <= 10’ is true and 10 gets printed for the
tenth time. Now, n++ increases the value to ‘n’ to 11.
This time, the condition ‘n <= 10’ becomes false and the program terminates.
#include <iostream>
int main(){
int a;
if (a%2 == 0){
else{
cout << "Want to check more : 1 for yes and 0 for no" << endl;
cout << "I hope you checked all your numbers" << endl;
return 0;
Output
Nesting of loops
Like ‘if/else’ we can also use one loop inside another. This is called nesting of loops. See this example
to make it clear.
#include<iostream>
int i;
int j;
cout << i << “*”<< j << “=” << (i*j) << endl;
return 0;
Output
When the first loop is executed, the value i is 12 and “Table of 12” gets printed. Now coming to the
second loop, the value of j is 1 and thus 12*1 =12 gets printed. In the second iteration of the inner
for loop, while the value of I is still 12, the value of j becomes 2 and thus 12 * 2 =24 gets printed.
Now coming to the second loop, the value of j is 1 and thus 12*1 12 gets
printed.
In the second iteration of the inner for loop, while the value of i is still 12, the
value of j becomes 2 and thus 12 * 2 = 24 gets printed. In the last iteration of
the inner for loop, the value of i is still 12 and the value of j becomes 10, thus
printing 12 * 10 = 120.
Now after all the iterations of the inner for loop are complete, there will be
the second iteration of the outer for loop increasing the value of i to 13 and
printing Table of 13. Again the inner for loop will be iterated with i equals 13.
We can use any loop inside any other loop according to the requirement. In
the above example, we used one for loop inside another.
Infinite Loop
There may exist some loops which can iterate r occur infinitely. These are
called Infinite p. These loops occur infinitely because their condition is always
true. We can make an infinite loop by leaving its conditional expression
empty (this is one of the many possible ways). When the conditional
expression is empty, it is assumed to be true. Lets see an example on how to
make a for loop infinite.
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
CASE STUDY
Real-World Applications of C++
1. Games:
C++ overrides the complexities of 3D games, optimizes resource
management and facilitates multiplayer with networking. The language is
extremely fast, allows procedural programming for CPU intensive functions
and provides greater control over hardware, because of which it has been
widely used in development of gaming engines. For instance, the science
fiction game Doom 3 is cited as an example of a game that used C++ well
and the Unreal Engine, a suite of game development tools, is written in C++
2. Graphic User Interface (GUI) based applications:
Many highly used applications, such as Image Ready, Adobe Premiere,
Photoshop and Illustrator, are scripted in C++.
3. Web Browsers:
With the introduction of specialized languages such as PHP and Java, the
adoption of C++ is limited for scripting of websites and web applications.
However, where speed and reliability are required, C++ is still preferred. For
instance, a part of Google's back-end is coded in C++, and the rendering
engine of a few open source projects, such as web browser Mozilla Firefox
and email client Mozilla Thunderbird, are also scripted in the programming
language.
4. Advance Computations and Graphics:
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++ provides the means for building applications requiring real time physical
simulations, high-performance image processing, and mobile sensor
applications. Maya 3D software, used for integrated 3D modeling, visual
effects and animation, is coded in C++
5. Database Software:
C++ and have been used for scripting MYSQL, one of the most popular
database management software. The software forms the backbone of a
variety of database-based enterprises, such as Google, Wikipedia, Yahoo and
YouTube etc.
6. Operating Systems:
C++ forms an integral part of many of the prevalent operating systems
including Apple's OS X and various versions of Microsoft Windows, and the
erstwhile Symbian mobile OS.
7. Enterprise Software:
C++ finds a purpose in banking and trading enterprise applications,
such as those deployed by Bloomberg and Reuters. It is also used in
development of advanced software, such as flight simulators and radar
processing
8. Medical and Engineering Applications:
Many advanced medical equipment, such as MRI machines, use C++
language for scripting their software. It is also part of engineering
applications, such as high-end CAD/CAM systems.
9. Compilers:
A host of compilers including Apple C++, Bloodshed Dev-C++, Clang C++
and MINGW make use of C++ language. C and its successor C++ are
leveraged for diverse software and platform development requirements, for
operating systems to graphic designing applications. Further, these
languages have assisted in the development of new languages for special
purposes like C, Java, PHP, Verilog etc.
Learning Resources:
CBSUA Website
Student Handbook
Fundamentals of Computing 1 (SY 2020-2021) Syllabus
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
Google.com
PowerPoint Presentation
Discussion Board
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. This platform will be
utilized throughout the course.
Competency Checklist
The student will be assessed by answering a questionnaire in this module regarding the lessons that
have been presented. This assessment will determine the knowledge that they’ve learned with the
Video Lectures Presentation.
Instructions:
KNOWLEDGE CHECK
Instruction : Circle the correct answer
1.The OOPs concept in C++, exposing only necessary information to users or
clients is known as
a. Abstraction
b. Encapsulation
c. Data hiding
d. Hiding complexity
2. Which of the following is an abstract
a. Class
b. Int
c. String
d. Double
3. Hiding the complexity is known
a. Abstraction
b. Encapsulation
c. Data hiding
d. Composition
4. For Cat and Animal class, correct way of inheritance is
a. class Cat: public Animal
b. class Animal: public Cat
c. Both are correct way
d. None is correct way
5. In a class, encapsulating an object of another class is called
a. Composition
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
b. Inheritance
c. Encapsulation
d. None
Tasks/Activities
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
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
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
5. Popular
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
9. Existence of Libraries
10. Speed
C C++
In C, data are free entities and can In C++, Encapsulation hides the
be manipulated by outside code. data to ensure that data structures
This is because C does not support and operators are used as
information hiding. intended.
C does not support function and C++ supports both function and
operator overloading. operator overloading.
C uses functions for input/output. C++ uses objects for input output.
For example scanf and printf. For example cin and cout.
C has no support for virtual and C++ supports virtual and friend
friend functions. functions.
C does not provide direct support C++ provides support for exception
for error handling (also called handling. Exceptions are used for
exception handling) "hard" errors that make the code
incorrect.
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.
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.
#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.
Pointers are the variables that point to memory addresses. Both C and C+
+ support pointers and various operations performed on pointers.
References act as aliases for the variables and point to the same memory
location as a variable.
#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.
#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.
#20) Structures:
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
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:
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
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.
#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.
#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.
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.
#38) Keywords:
C++ supports a lot more keywords than that of C. In fact, C has only 32
keywords whereas C++ has 52 keywords.
#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.
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
9 Program division Program divided into Program divided into classes and
functions. objects.
12 The main() Can call main through Not possible to call main from any
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
N
Characteristics C C++
o
N
Characteristics C C++
o
34 Mapping Cannot easily map data Data and functions can be easily
and functions. mapped.
N
Characteristics C C++
o
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
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
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.
Variable Definition in C++
A variable definition tells the compiler where and how much storage to
create for the variable. A variable definition specifies a data type, and
contains a list of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C++ data type including char, w_char, int,
float, double, bool or any user-defined object, etc.,
and variable_list may consist of one or more identifier names separated
by commas. Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; both declares and defines the variables i, j and k;
which instructs the compiler to create variables named i, j and k of type
int.
Variables can be initialized (assigned an initial value) in their declaration.
The initializer consists of an equal sign followed by a constant expression
as follows −
type variable_name = value;
Some examples are −
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
For definition without an initializer: variables with static storage duration
are implicitly initialized with NULL (all bytes have the value 0); the initial
value of all other variables is undefined.
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
// 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 −
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
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
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.
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>
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 main () {
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
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
pointer NULL
Learning Resources
6. CBSUA Website
7. Student Handbook
8. Fundamentals of Computing 1 (SY 2020-2021) Syllabus
9. Google.com
10. 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
Week 6 to 8
INTRODUCTION
C++ allows you to specify more than one definition for a function name
or an operator in the same scope, which is called function
overloading and operator overloading respectively.
An overloaded declaration is a declaration that is declared with the same
name as a previously declared declaration in the same scope, except that
both declarations have different arguments and obviously different
definition (implementation).
When you call an overloaded function or operator, the compiler
determines the most appropriate definition to use, by comparing the
argument types you have used to call the function or operator with the
parameter types specified in the definitions. The process of selecting the
most appropriate overloaded function or operator is called overload
resolution.
3.1 CONCEPT OF OVERLOADING
Overloading refers to the ability to use a single identifier to define multiple
methods of a class that differ in their input and output parameters.
Overloaded methods are generally used when they conceptually execute
the same task but with a slightly different set of parameters. Overloading
is a concept used to avoid redundant code where the same method name
is used multiple times but with a different set of parameters. The actual
method that gets called during runtime is resolved at compile time, thus
avoiding runtime errors. Overloading provides code clarity, eliminates
complexity, and enhances runtime performance. Overloading is used in
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
we can Change the Operation of the Operators. But With the help of
Operator Overloading we can Change the behavior of the unary Operator
means we can perform Operations means we can Increase or Decrease
the values of two or more Operands at a Time. And With the Help of
Comparison Operators we can also compare the Two Objects Means all
the Data Members of one Object can be compared with the Data Members
of the Other Object. Without the help of Operator Overloading this is not
possible to compare two Objects. So with the help of Comparison
Operators we can compare two Objects.
You can have multiple definitions for the same function name in the same
scope. The definition of the function must differ from each other by the
types and/or the number of arguments in the argument list. You cannot
overload function declarations that differ only by return type.
Following is the example where same function print() is being used to
print different data types −
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
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 main(void) {
printData pd;
return 0;
}
When the above code is
compiled and executed, it
produces the following result
−
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
properties will be accessed using this object, the object which will call this
operator can be accessed using this operator as explained below −
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.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
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Overloadable/Non-overloadable Operators
+ - * / % ^
& | ~ ! , =
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
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
:: .* . ?:
Learning Resources
11. CBSUA Website
12. Student Handbook
13. Fundamentals of Computing 1 (SY 2020-2021) Syllabus
14. Google.com
15. 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
Week 9 – 11
Chapter 4 INHERITANCE
Introduction
One of the most important concepts in object-oriented programming is
that of inheritance. Inheritance allows us to define a class in terms of
another class, which makes it easier to create and maintain an
application. This also provides an opportunity to reuse the code
functionality and fast implementation time. When creating a class, instead
of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members
of an existing class. This existing class is called the base class, and the
new class is referred to as the derived class. The idea of inheritance
implements the is a relationship. For example, mammal IS-A animal, dog
IS-A mammal hence dog IS-A animal as well and so on. Inheritance is one
of the core features of an object- oriented programming language. It
allows software developers to derive a new class from the existing class.
The derived class inherits the features of the base class (existing class).
The technique of deriving a new class from an old one is called
inheritance. The old class is referred to as base class and the new class is
referred to as derived class or subclass. Inheritance concept allows
programmers to define a class in terms of another class, which makes
creating and maintaining application easier. When writing a new class,
instead of writing new data member and member functions all over again,
programmers can make a bonding of the new class with the old one that
the new class should inherit the members of the existing class. A class can
get derived from one or more classes, which means it can inherit data and
functions from multiple base classes.
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
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
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
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Total area: 35
A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member
functions of derived classes should be declared private in the base class.
We can summarize the different access types according to - who can
access them in the following way −
A derived class inherits all base class methods with the following
exceptions −
Type of Inheritance
When deriving a class from a base class, the base class may be inherited
through public, protected or private inheritance. The type of
inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance, but public inheritance
is commonly used. While using different type of inheritance, following
rules are applied −
Public Inheritance − When deriving a class from a public base
class, public members of the base class become public members
of the derived class and protected members of the base class
become protected members of the derived class. A base
class's private members are never accessible directly from a
derived class, but can be accessed through calls to
the public and protected members of the base class.
Protected Inheritance − When deriving from a protected base
class, public and protected members of the base class
become protected members of the derived class.
Private Inheritance − When deriving from a private base
class, public and protected members of the base class
become private members of the derived class.
A C++ class can inherit members from more than one class and here is
the extended syntax −
class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would be
given for every base class and they will be separated by comma as
shown above. Let us try the following example −
#include <iostream>
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape, public PaintCost {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
return 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
When the above code is compiled and executed, it produces the following
result −
Total area: 35
Total paint cost: $2450
The derived class inherits the features from the base class and can
have additional features of its own. For example,
class Animal {
// eat() function
// sleep() function
};
Here, the Dog class is derived from the Animal class. Since Dog is derived
from Animal, members of Animal are accessible to Dog.
Inheritance in C++
Notice the use of the keyword public while inheriting Dog from Animal.
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
We can also use the keywords private and protected instead of public.
We will learn about the differences between
using private, public and protected later in this tutorial.
is-a relationship
A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
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
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
return 0;
}
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
Here, dog1 (the object of derived class Dog) can access members of the
base class Animal. It's because Dog is inherited from Animal.