Mooc Report
Mooc Report
School of Computing
GRAPHIC ERA HILL UNIVERSITY BHIMTAL CAMPUS
SATTAL ROAD, P.O. BHOWALI
DISTRICT- NAINITAL-263136
2024 - 2027
1
BHIMTAL CAMPUS
2
BHIMTAL CAMPUS
CERTIFACATE
BHIMTAL CAMPUS
3
CONTENT
S. NO. CONTENTS PER MODULE Page No.
1 History of Programming and Introduction to C++ 06
2 Introduction 06-13
3 Condition and Iteration Structures in C++ 14-21
4 Arrays in C++ 22-23
ACKNOWLEDGEMENT
I Express my gratitude to Saylor Academy, for coming up with this interesting and thought provoking
course, compiling the best contents and most important working towards changing the words, making
the difference and inspiring us to become the climate saver ourselves , knowledge that I gained will last
a lifetime.
4
I Want to thanks https://www.saylor.org/for broadcasting the content and the very nice user interface.
Many thanks to my institution, Graphic Era Hill University, Bhimtal Campus MOOC Coordinator – Dr.
Jitendra Chaudhary for motivating us to look beyond the syllabus, to take up the opportunity and to
learn as much as we can, as long as we can.
📅 Date: [14-may-2025]
5
📝 1 Introduction
In today’s technology-driven world, programming languages play a crucial role in developing software
applications, websites, operating systems, and much more. C++ is one of the most widely used
programming languages, known for its power, flexibility, and performance. This report presents an
overview of the history of programming languages and an introduction to C++, as discussed in Module 1
of the Saylor Academy C++ course.
The evolution of programming languages began in the early 19th century and has advanced significantly
over the decades.
🔹 Early Beginnings
🔹 High-Level Languages
1980s – C++:
Developed by Bjarne Stroustrup, C++ was created as an extension of C. It added object-oriented
programming (OOP) features such as classes and inheritance. This allowed for better data
abstraction, code reuse, and modularity.
C++ is a general-purpose programming language that supports both procedural and object-oriented
programming. It is widely used for system/software development, game development, drivers, client-
server applications, and embedded firmware.
✨ Features of C++
#include <iostream>
int main() {
return 0;
using namespace std; – Allows use of standard library functions without prefix.
Learning C++ provides a solid foundation in computer programming. It helps understand fundamental
programming concepts that apply to many other languages. It is also essential for competitive
programming, system-level development, and software engineering.
💼 Job Opportunities: Many tech companies require C++ developers for high-performance
applications.
🧱 Base for Advanced Languages: Languages like Java, C#, and even Python borrow concepts
from C++.
📝 Introduction
7
Ubuntu is one of the most popular Linux distributions for programmers. It offers a powerful
environment for C++ development using the GNU Compiler Collection (GCC). This report provides a
simple guide to writing, compiling, and running a C++ program on Ubuntu.
Before compiling any program, ensure that g++, the GNU C++ compiler, is installed on your system.
You can use any text editor such as nano, vim, gedit, or Visual Studio Code to write your C++ code.
bash
nano hello.cpp
#include <iostream>
int main() {
return 0;
bash
./hello
✅ Output
Hello, world!
📝 Introduction
C++ programs are built using basic commands for input, output, variables, and control structures. These
commands form the core of all beginner-level C++ programming.
#include <iostream>
int main() {
return 0;
int age;
9
if (age >= 18) {
cout << i;
Use if, for, and while for program logic and repetition.
📦 Variable Types
int x = 10;
📦 Variables
A variable is a named space in memory to store data that can change during program execution.
🧾 Data Types
10
Type Description Example
🔒 Constants
The cout (character output) stream is used to display information to the user.
<< is the insertion operator, used to send data to the output stream.
📌 Example:
The cin (character input) stream is used to accept input from the user.
int age;
>> is the extraction operator, used to take data from the input stream and store it in a variable.
📌 Example:
int age;
➕ Addition: +
➖ Subtraction: -
✖️Multiplication: *
12
➗ Division: /
Used to divide one number by another. For integer division, the result will be an integer.
🔢 Modulus: %
🔍 Condition Structures
Condition structures in C++ allow the program to execute different code depending on whether a
specific condition is true or false. The most common condition structures are if, else, and else if.
if Statement
The if statement checks if a condition is true. If the condition is true, the block of code inside the if
statement is executed.
else Statement
13
The else statement is used in conjunction with if to provide an alternative action when the condition is
false.
} else {
else if Statement
} else {
Iteration structures are used to repeatedly execute a block of code multiple times. C++ supports several
types of loops:
for Loop
The for loop is used when the number of iterations is known beforehand.
while Loop
The while loop continues executing as long as the specified condition is true.
14
int i = 0;
while (i < 5) {
i++;
do...while Loop
The do...while loop guarantees at least one iteration, even if the condition is false, as it checks the
condition after the loop executes.
int i = 0;
do {
i++;
🔧 Testing in C++
Testing ensures that the program produces the correct output and behaves as expected. It typically
involves the following steps:
2. Integration Testing: Ensuring that different parts of the program work together as expected.
3. System Testing: Testing the entire program to verify that it works under various scenarios.
🐞 Debugging in C++
Debugging involves identifying and fixing errors in the code. Errors can be categorized as:
1. Syntax Errors: Mistakes in the structure of the program (e.g., missing semicolons, incorrect
function names).
15
2. Runtime Errors: Errors that occur while the program is running, such as division by zero or
accessing out-of-bounds arrays.
3. Logical Errors: Errors that result in incorrect output, even though the program runs without
crashing.
Print Statements: Inserting cout statements in the code to print variable values and track
program flow.
cout << "The result is: " << result << endl;
Using a Debugger (GDB): The GNU Debugger (GDB) is a powerful tool that allows you to step
through the program line by line, set breakpoints, and inspect variables.
bash
IDE Debuggers: Integrated Development Environments (IDEs) like Visual Studio or Code::Blocks
come with built-in debuggers, which provide graphical interfaces for stepping through code, setting
breakpoints, and inspecting variables.
📝 Introduction
In C++, the scope of a variable refers to the part of the program where the variable is accessible.
Variables can be declared inside functions (local variables) or outside functions (global variables).
Understanding variable scope is crucial to avoid errors related to variable visibility and lifetime. In this
section, we explore the scope of variables within a simple function.
📦 Local Variables
Local variables are declared within a function and can only be accessed inside that function. Once the
function execution ends, the local variable is destroyed.
#include <iostream>
16
using namespace std;
void greet() {
cout << "Hello, " << name << "!" << endl;
int main() {
greet();
return 0;
🌍 Global Variables
Global variables are declared outside of all functions, and they are accessible throughout the program.
However, they should be used with caution because they can be modified by any function, which may
lead to unintentional side effects.
Scope: Refers to the region of the program where a variable can be accessed.
For example, local variables have a limited lifetime (exist only during the function call), while global
variables have a global lifetime (exist for the entire program runtime).
Function parameters are also local variables. They are scoped only within the function they are passed
into.
#include <iostream>
17
using namespace std;
cout << "The number is: " << number << endl;
int main() {
display(5);
return 0;
When an argument is passed by value, a copy of the argument is made and passed to the function. The
function works with this copy, so any changes made to the parameter inside the function do not affect
the original variable.
Explanation: In this example, the variable number is passed by value to the modifyValue()
function. The function modifies the copy of number, but the original variable number in main()
remains unchanged.
When an argument is passed by reference, the function operates directly on the original variable,
meaning changes to the parameter will affect the original variable. This is done by using the reference
operator (&).
1. length() / size()
Returns the number of characters in the string.
18
string str = "Hello";
2. empty()
Checks if the string is empty.
3. append() / +=
Adds characters or a string to the end.
str.append(" World");
str += "!";
4. insert()
Inserts characters at a specific position.
5. erase()
Removes characters from the string.
6. find()
Finds the first occurrence of a substring.
19
7. substr()
Returns a substring from a given position.
8. compare()
Compares two strings lexicographically.
9. transform()
Converts string to upper or lower case.
10. stoi()
Converts a string to an integer.
11. to_string()
Converts a number to a string.
20
Module 3- Arrays in C++
📦 Declaring an Array
To declare an array, specify the type of its elements and the number of elements it will hold.
Here, arr is an integer array capable of storing 5 elements. The array indices range from 0 to 4.
🔢 Initializing an Array
If fewer values are provided than the size of the array, the remaining elements are initialized to 0.
Declaring a Structure:
struct Student {
21
string name;
int age;
float grade;
};
Here, Student is a structure with three members: name (string), age (integer), and grade (float).
Structure Initialization:
🔧 Unions in C++
Declaring a Union:
union Data {
int intVal;
float floatVal;
char charVal;
};
Here, Data is a union with three members: intVal, floatVal, and charVal. All members share the same
memory location.
Using a Union:
Accessing members is done in the same way as structures, using the dot (.) operator. However,
remember that only one member can store a value at a time:
Data data;
data.intVal = 10;
Union Size:
22
The size of a union is the size of its largest member because all members share the same space:
cout << sizeof(Data); // Output: Size of the largest member (int, float, or char)
🔢 Enumerations in C++
An enumeration (enum) is a user-defined data type that consists of integral constants. It allows you to
define a set of named values, which makes the code more readable and maintainable.
A class is a blueprint for creating objects. It combines data members (attributes) and member
functions (behaviors) into a single unit.
Class Syntax:
class ClassName {
// Data members
// Member functions
};
Access Specifiers:
Creating Objects:
23
ClassName obj; // Create an object
Inheritance allows a derived class to inherit properties and behaviors (data members and member
functions) from a base class. This promotes code reuse.
Syntax:
};
Access Specifiers:
o public: Members of the base class are accessible in the derived class.
o protected: Base class members are accessible only within derived classes.
o private: Base class members are not directly accessible in the derived class.
Polymorphism allows objects of different classes to be treated as objects of a common base class,
enabling dynamic method invocation.
Types of Polymorphism:
class Print {
public:
};
class Animal {
public:
24
virtual void sound() { cout << "Animal sound"; }
};
public:
};
int main() {
animal->sound();
25
Module 5- Class Templates in C++
A class template allows you to define a class that can work with any data type.
Syntax:
public:
T value;
};
A function template allows you to write a function that can work with any data type.
Syntax:
T add(T a, T b) {
26
return a + b;
C++ provides file handling capabilities through the fstream library, which allows reading from and
writing to files.
#include <fstream>
2. Opening a File
3. Writing to a File
ofstream outFile("output.txt");
ifstream inFile("input.txt");
string line;
cout << line << endl; // Print each line from file
27
C++ provides a mechanism to handle runtime errors using exception handling with the try, throw, and
catch keywords.
1. Syntax:
References:
1. Saylor Academy C++ Course:
o As you completed the C++ course from Saylor, you can revisit the official course
material to check for further explanations and examples on exception handling.
2. C++ Documentation:
o C++ Exception Handling Overview - Official C++ reference for exception handling.
28