Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

INTRODUCTION

Rating
-
Sold
-
Pages
39
Uploaded on
11-10-2022
Written in
2022/2023

DETAILS OF UNIT 4 OF OOSD

Institution
Course

Content preview

UNIT-4
1. Overview of C++
C++ is a general-purpose programming language that was developed as an enhancement of the C
language to include object-oriented paradigm. It is an imperative and a compiled language.
C++ is a middle-level language rendering it the advantage of programming low-level (drivers,
kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and
code structure of both C and C++ are the same.
Some of the features & key-points to note about the programming language are as follows:
 Simple: It is a simple language in the sense that programs can be broken down into logical units
and parts, has a rich library support and a variety of data-types.
 Machine Independent but Platform Dependent: A C++ executable is not platform-
independent (compiled programs on Linux won’t run on Windows), however they are machine
independent.
 Mid-level language: It is a mid-level language as we can do both systems-programming
(drivers, kernels, networking etc.) and build large-scale user applications (Media Players,
Photoshop, Game Engines etc.)
 Rich library support: Has a rich library support (Both standard ~ built-in data structures,
algorithms etc.) as well 3rd party libraries (e.g. Boost libraries) for fast and rapid development.
 Speed of execution: C++ programs excel in execution speed. Since, it is a compiled language,
and also hugely procedural. Newer languages have extra in-built default features such as
garbage-collection, dynamic typing etc. which slow the execution of the program overall. Since
there is no additional processing overhead like this in C++, it is blazing fast.
 Pointer and direct Memory-Access: C++ provides pointer support which aids users to directly
manipulate storage address. This helps in doing low-level programming (where one might need
to have explicit control on the storage of variables).
 Object-Oriented: One of the strongest points of the language which sets it apart from C.
Object-Oriented support helps C++ to make maintainable and extensible programs. i.e. Large-
scale applications can be built. Procedural code becomes difficult to maintain as code-size
grows.
 Compiled Language: C++ is a compiled language, contributing to its speed.

Applications of C++:

C++ finds varied usage in applications such as:
 Operating Systems & Systems Programming. e.g. Linux-based OS (Ubuntu etc.)
 Browsers (Chrome & Firefox)
 Graphics & Game engines (Photoshop, Blender, Unreal-Engine)
 Database Engines (MySQL, MongoDB, Redis etc.)
 Cloud/Distributed Systems

Some interesting facts about C++:

Here are some awesome facts about C++ that may interest you:
1. The name of C++ signifies the evolutionary nature of the changes from C. “++” is the C
increment operator.
2. C++ is one of the predominant languages for the development of all kind of technical and
commercial software.
3. C++ introduces Object-Oriented Programming, not present in C. Like other things, C++
supports the four primary features of OOP: encapsulation, polymorphism, abstraction, and
inheritance.
4. C++ got the OOP features from Simula67 Programming language.

,5. A function is a minimum requirement for a C++ program to run.(at least main() function)


2 Program structure
The C++ program is written using a specific template structure. The structure of the program
written in C++ language is as follows:




Documentation Section:
 This section comes first and is used to document the logic of the program that the programmer
going to code.
 It can be also used to write for purpose of the program.
 Whatever written in the documentation section is the comment and is not compiled by the
compiler.
 Documentation Section is optional since the program can execute without them. Below is the
snippet of the same.


1. // this is a program
2. /* This is a C++ program to find the
factorial of a number */


Linking Section:
The linking section contains two parts:
a. Header Files:
 Generally, a program includes various programming elements like built-in functions, classes,
keywords, constants, operators, etc. that are already defined in the standard C++ library.
 In order to use such pre-defined elements in a program, an appropriate header must be included
in the program.
 Standard headers are specified in a program through the preprocessor directive #include. In
Figure, the iostream header is used. When the compiler processes the
instruction #include<iostream>, it includes the contents of the stream in the program. This
enables the programmer to use standard input, output, and error facilities that are provided only
through the standard streams defined in <iostream>. These standard streams process data as a
stream of characters, that is, data is read and displayed in a continuous flow. The standard
streams defined in <iostream> are listed here.
#include<iostream>
b. Namespaces:

, A namespace permits grouping of various entities like classes, objects, functions, and
various C++ tokens, etc. under a single name.
 Any user can create separate namespaces of its own and can use them in any other program.
 In the below snippets, namespace std contains declarations for cout, cin, endl, etc. statements.
using namespace std;
 Namespaces can be accessed in multiple ways:
using namespace std;
using std :: cout;

Definition Section:
 It is used to declare some constants and assign them some value.
 In this section, anyone can define your own datatype using primitive data types.
 In #define is a compiler directive which tells the compiler whenever the message is found
replace it with “Factorial\n” .
 typedef int K; this statement telling the compiler that whenever you will encounter K replace it
by int and as you have declared k as datatype you cannot use it as an identifier.

Global Declaration Section:
 Here the variables and the class definitions which are going to be used in the program are
declared to make them global.
 The scope of the variable declared in this section lasts until the entire program terminates.
 These variables are accessible within the user-defined functions also.

Function Declaration Section:
 It contains all the functions which our main functions need.
 Usually, this section contains the User-defined functions.
 This part of the program can be written after the main function but for this, write the function
prototype in this section for the function which for you are going to write code after the main
function.

// Function to implement the
// factorial of number num
int factorial(k& num)
{
// Iterate over the loop from
// num to one
for (k i = 1; i <= num; i++) {
fact *= i;
}
// Return the factorial calculated
return fact;
}


Main Function:
 The main function tells the compiler where to start the execution of the program. The execution
of the program starts with the main function.
 All the statements that are to be executed are written in the main function.
 The compiler executes all the instructions which are written in the curly braces {} which
encloses the body of the main function.
 Once all instructions from the main function are executed control comes out of the main
function and the program terminates and no further execution occur. Below is the program to
illustrate this:

, // Documentation Section
/* This is a C++ program to find the
factorial of a number
The basic requirement for writing this
program is to have knowledge of loops
To find the factorial of a number
iterate over the range from number to 1
*/

// Linking Section
#include <iostream>
using namespace std;

// Defination Section
#define msg "FACTORIAL\n"
typedef int k;

// Global Declaration Section
k num = 0, fact = 1, storeFactorial = 0;

// Function Section
k factorial(k& num)
{
// Iterate over the loop from
// num to one
for (k i = 1; i <= num; i++) {
fact *= i;
}

// Return the factorial
return fact;
}

// Main Function
int main()
{
// Given number Num
k Num = 5;

// Function Call
storeFactorial = factorial(Num);
cout << msg;

// Print the factorial
cout << Num << "! = "
<< storeFactorial << endl;

return 0;
}

Output
FACTORIAL
5! = 120

Written for

Institution
Course

Document information

Uploaded on
October 11, 2022
Number of pages
39
Written in
2022/2023
Type
Class notes
Professor(s)
Ayasha malik
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
ayashamalik

Get to know the seller

Seller avatar
ayashamalik DTC
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
10
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions