0% found this document useful (0 votes)
644 views11 pages

Lab Manual 2 C++ PDF

This document provides an introduction to fundamentals of C++ programming through examples. It begins with an overview of C++ as a programming language, including its features. It then discusses reasons for learning C++, such as its widespread use, ability to learn computer architecture, and job opportunities. The document proceeds to provide examples of "Hello World" and other basic C++ programs. It explains the key components of a C++ program and how to print output and take input. The overall purpose is to familiarize students with basic C++ concepts and building blocks through hands-on programming experiments.

Uploaded by

Zain iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
644 views11 pages

Lab Manual 2 C++ PDF

This document provides an introduction to fundamentals of C++ programming through examples. It begins with an overview of C++ as a programming language, including its features. It then discusses reasons for learning C++, such as its widespread use, ability to learn computer architecture, and job opportunities. The document proceeds to provide examples of "Hello World" and other basic C++ programs. It explains the key components of a C++ program and how to print output and take input. The overall purpose is to familiarize students with basic C++ concepts and building blocks through hands-on programming experiments.

Uploaded by

Zain iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

PROGRAMMING
FUNDAMENTALS

Experiment 2
Fundamentals of a C++ Program

CLO 1: Understand the fundamental concepts of programming constructs to


develop C++ programs, using an Integrated Development Environment
(IDE).

1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Purpose:

This experiment provides an introduction Fundamentals of a basic C++ Program. Students

will compile and run programs consisting of C++ building blocks.

Objectives:

At the end of this experiment you will:

1) Get familiar with the basic structure of C++ Program.

2) Able to run and check different but related programs consisting of C++ building blocks.

Equipment and Components:

3) Dev-C++ 5.0 Beta 9.2 (4.9.9.2) 9.0 MB with Minge/GCC 3.4.2

4) Code::Blocks IDE 20.03

2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

What is C++ (programming language)?

• C++ is a statically-typed, free-form programming language.”


• In simple terms, C++ is a sophisticated, efficient and a general-purpose
programming language based on C. It was developed by Bjarne Stroustrup in
1979.
• Many of today’s operating systems, system drivers, browsers and games use
C++ as their core language. This makes C++ one of the most popular languages
today.
• Since it is an enhanced/extended version of C programming language, C and
C++ are often denoted together as C/C++.

Features of C++

• C++ is fast

Since, C++ is an extended version of C, the C part of it is very low level.

This offers a huge boost in speed that high level languages like Python, Java
don’t give you.
• C++ is statically typed

C++ is a statically typed programming language.


In simple terms, C++ doesn’t allow the compiler to make assumptions about
the type of data e.g. 10 is different from “10” and you have to let C++ know
which one you are talking about. This helps the compiler catch errors and bugs
before execution of the program.

• Object oriented programming with C++

Object oriented programming helps you solve a complex problem intuitively.


With its use in C++, you are able to divide these complex problems into
smaller sets by creating objects.

• Power of standard library (Standard template library - STL)

3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

The power of C++ extends with the use of standard libraries contained in it.
These libraries contain efficient algorithms that you use extensively while
coding. This saves sufficient amount of programming effort, which otherwise
would have been wasted reinventing the wheel

5 Reasons Why you should learn C++?

1. C++ is irreplaceable

With the use of C++ in development of modern games, operating systems,


browsers, and much more, it is safe to say that C++ is irreplaceable.

Many major applications like

o Adobe Products like Photoshop, Illustrator, InDesign


o Amazon - one of the biggest e-commerce sites
o Autodesk products for Computer Aided Design
o Facebook - social networking site is heavy C++ centric products.

Moreover, the fact that there’s a huge community improving C++ on every
iteration means that it is only expected to be used even more in the coming
future.
2. You learn the internal architecture of a computer

Since, C++ is a middle level language, you will write code that interacts
directly with the internal hardware of the computer.

You’ll learn how the computer memory really works, how information is stored
in them, how you can retrieve them and so on. (Data Structures Course)

It is sure to expand your knowledge on the architecture of the computer.

3. Over 600,000 C++ repositories on Github

Github, the leading open source collaboration platform, has over 600,000
repositories for C++ alone.

This metric itself proves the worth of C++ in the open source community as
well.

4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Be it gaming, graphics, windows applications, you can find tons of great open
source projects extensively used today. And, you can always create your own.

4. 60% StackOverflow Answer rate and active community

Likewise, with over 400,000 C++ questions asked on StackOverflow, the


number one Q&A platform for developers, more than 60% questions have
been answered.

The number of questions asked and the percentage of them answered


shows the interest and active support for C++ today.

So, you can expect many great developers to help you solve real-life
problems using C++.

5. C++ job opportunities and salary

C++ developers can expect an average of yearly $100,000 salary with over
7,700 jobs advertised every month.

The requirement of jobs comes mostly from game development, rendering


engines and the windows applications.

4 Things to Know Before you Code in C++


1. C++ cannot be learnt in a day

Learning any language takes time and that holds even more truth for C++.

If you are here to learn C++ in a day, then you’re going to end up facing failure.

You only start learning with regular practice and dedication. So, I suggest you
to invest valuable time learning C++.

2. No, you don’t need to learn C before C++

People have different theories whether one should learn C before C++ or not.
If you ask me, it isn’t a must. You can easily start with C++. If you already
know C, you will have a head start in learning C++ as they have similar
attributes like syntax and semantics.

5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Let’s Start Coding:


C++ "Hello, World!" Program
A "Hello, World!" is a simple program that outputs/Prints Hello, World! on the
screen

// Your First C++ Program


#include <iostream>

using namespace std;

int main()
{
cout<<"Hello, World!" Program";
}
Note:
1. any line starting with // is a comment. Comments are intended for the
person reading the code to better understand the functionality of the
program. It is completely ignored by the C++ compiler.

2. The #include is a "preprocessor" directive that tells the compiler to put code
from the header called iostream into our program before actually creating
the executable (object Code). By including header files, you gain access to
many different functions. For example, iostream is necessary header file to
use cout object in our program that print output on the screen.

3. In programming, we cannot have variables, functions, etc with the same


name. So to avoid those conflicts we use namespaces. “using namespace
std” means we use the namespace named std. std is an abbreviation for
standard. So that means we use all the things with in std namespace. If we
don’t want to use this line of code, we can use the things in this namespace
like this. std::cout, std::endl.

4. int main() {...}

6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

A valid C++ program must have the main() function. The curly braces
indicate the start and the end of the function. The execution of code beings
from this function.

5. cout object prints the content inside the quotation marks. It must be
followed by << followed by the format string. In our example, "Hello
World!" is the format string.

6. ; is used to indicate the end of a statement.

7. The return 0; statement is the "Exit status" of the program. In simple terms,
the program ends with this statement.

Things to take away


• We use cout in order to print output on the screen.
• We must include iostream if we want to use cout.
• The execution of code begins from the main() function.

int main() {

// Write your code here


}

C++ Basic Input/Output


use the cin object to take input from the user, and the cout object to display output to the user with
the help of examples.

C++ Output
In C++, cout sends formatted output to standard output devices, such as the screen. We use the
cout object along with the << operator for displaying output.

7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example 1: String Output

#include <iostream>
using namespace std;

int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}

Output

This is C++ Programming

How does this program work?

• We first include the iostream header file that allows us to display output.
• The cout object is defined inside the std namespace. To use the std namespace, we used
the using namespace std; statement.
• Every C++ program starts with the main() function. The code execution begins from the
start of the main() function.
• cout is an object that prints the string inside quotation marks " ". It is followed by the <<
operator.
• return 0; is the "exit status" of the main() function. The program ends with this
statement, however, this statement is not mandatory.

Note: If we don't include the using namespace std; statement, we need to use std::cout
instead of cout.

#include <iostream>

int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}

8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example 2: Numbers and Characters Output

To print the numbers and character variables, we use the same cout object but without using
quotation marks.

#include <iostream>
using namespace std;

int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';

cout << num1 << endl; // print integer


cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}

Output

70
256.783
character: A

Notes:

• The endl manipulator(that is used to change the formatting of output) is used to insert a
new line. That's why each output is displayed in a new line.
• The << operator can be used more than once if we want to print different variables, strings
and so on in a single statement. For example:

cout << "character: " << ch << endl;

C++ Input
In C++, cin takes formatted input from standard input devices such as the keyboard. We use the
cin object along with the >> operator for taking input.

9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example 3: Integer Input/Output

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}

Output

Enter an integer: 70
The number is: 70

In the program, we used

cin >> num;

to take input from the user. The input is stored in the variable num. We use the >> operator with
cin to take input.

C++ Taking Multiple Inputs


#include <iostream>
using namespace std;

int main() {
char a;
int num;

cout << "Enter a character and an integer: ";


cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num;

return 0;
}

10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Output

Enter a character and an integer: F


23
Character: F
Number: 23

Task
• Create a C++ Program to Generate the Following Output. (10 Marks)

11

You might also like