0% found this document useful (0 votes)
20 views

Lecture Lab-1

The document provides details about Practical 1 for an Object Oriented Programming in C++ course. The practical aims to introduce students to basic C++ concepts like variables, data types, arithmetic operations, and comments. It outlines tasks for students such as implementing variable declarations and a simple C++ program, designing pseudocode for problems, and solving numeric problems using arithmetic. The document also provides reading resources on C++ and describes steps for setting up the Visual Studio C++ integrated development environment.

Uploaded by

Israr Qayyum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture Lab-1

The document provides details about Practical 1 for an Object Oriented Programming in C++ course. The practical aims to introduce students to basic C++ concepts like variables, data types, arithmetic operations, and comments. It outlines tasks for students such as implementing variable declarations and a simple C++ program, designing pseudocode for problems, and solving numeric problems using arithmetic. The document also provides reading resources on C++ and describes steps for setting up the Visual Studio C++ integrated development environment.

Uploaded by

Israr Qayyum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Practical’s

Practical 1
Revision of (Object Oriented) Programming In C++

Objectives
The main purpose of this Lab Practical is to introduce students with basic concepts of C++. The components
of programs, such as key words, variables, operators, and punctuation are covered. Basic parts of a C++
program, data types, the use of variables and literals, assignment statements, simple arithmetic operations,
program output, and comments.

Scope
The Scope of this lab practical is to:
 Implement initialization and declarations of variables
 Develop a simple program in MS Visual C++
 Design Pseudocode for various programming problems
 Solve numeric problems using arithmetic operations
Activity Time Boxing
Task Activity Name Total
No. Time
1 Concept Deliverance 30
2 Setting Up MS Visual C++ 10
5 Evaluation/Marking 60
Total Time: 180

Tools and Reading Resources


 Book: Object Oriented Programming in C++ by Robert Lafore, Chap 2
 Book: Starting out with C++ Early Objects 9th Edition.,Chap 2
 https://www.youtube.com/watch?v=IsAoIqnNia4
 https://devblogs.microsoft.com/cppblog/getting-started-with-visual-studio-for-c-and-cpp-development/
Theoretical Explanation
The ISO C++ Standard
The C++ language and standard library are defined by their ISO standard. C++ is a compiled language. For
a program to run, its source text has to be processed by a compiler, producing object files, which are
combined by a linker yielding an executable program. A C++ program typically consists of many source
code files (usually simply called source files).
Basic C++ Program Construction
int main()
// main method is the global function of c++ program that takes nothing but it should return some integer
value .int specifies integer value
{ }// curly braces indicates grouping of line of code and start of a function
// double slash sign is used to add comments/additional information about the line of code. For multiple
line comments use
/*
Here goes my comments
*/
Sample C++ program that displays Hello World on Console Screen.
#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!\n";
return 0;
}
Program Statements:
The program statement is the fundamental unit of C++ programming. There are two statements in the
FIRST program: the line
cout << "Hello, World!\n";
and the return statement
return 0; // A semicolon signals the end of the statement

The identifier cout (pronounced “C out”) is actually an object. It is predefined in C++ to correspond to
the standard output stream. The operator << is called the insertion or put to operator. It directs the
contents of the variable on its right to the object on its left.
Directives
The two lines that begin the FIRST program are directives.

preprocessor directive directive.


#include <iostream> using namespace std;

They’re not part of the basic C++ language, but A C++ program can be divided into different
they’re necessary anyway namespaces. A namespace is a part of the
program in which certain names are recognized;
outside of the namespace they’re unknown.
It isn’t part of a function body and doesn’t end
with a semicolon
(#). It’s called a preprocessor directive. It is an
instruction to compiler to include another file in
the current program with name of iostream
header file

Input, Processing, and Output Cycle:


Computer programs typically perform a three-step process of gathering input, performing some process on
the information gathered, and then producing output .

cin >> hours;


cin >> rate ;
cout<< “pay”;
int pay= hours * rate;
cout<< pay;

Designing and creating a program:


Designing and creating a program is a stepwise approach that is used to develop a program. Let’s say you
have been given a problem to perform basic Arithmetic operations (add, subtract, divide and multiply) .For
this you need a pseudocode which is a recipe/written outline to design a program in any language, which
can be further transcribed
Step#1: identify the purpose of the program

Purpose: Perform Arithmetic operations on integers


Input: two integer variables
Processing: adding/subtracting the numbers and storing the computation result in variable
Output: display results in this format
Enter the first variable: 10
Enter the second variable : 2
Addition :12
Multiplication:20
Division:5
Subtraction=8
Step#2: Visualize Program by running
Enter the first variable: 10
Enter the second variable : 2
Addition :12
Multiplication:20
Division:5
Subtraction=8
Step#3: develop Pseudocode to design a program
Using a guidelines form step 2 we are going to write a pseudocode

Start

Declare variables variable1, variable2 , variable3, variable4, variable5, variable6

Initialize variables variable1, variable2 , variable3, variable4, variable5, variable6

Display a text that says “Enter the first variable”


Asks user to input the first variable e.g variable1

Display a text that says “Enter the second variable”


Asks user to input the second variable e.g variable2
Compute the addition operations on two user entered variables
Variable3= variable1<operator > variable2

Stop

Step#4: Check for logical Errors

Find for any logical errors e.g directives and libraries are not added

Step#5& 6: Check for logical Errors


Write source code of program and compile it.

Step#9:Run the program using test data for input.

Identifiers in C++
An identifier is a programmer -defined name that represents some element of a program. Variable names
are examples of identifiers.

Legal Identifiers
Basic C++ Data Types

Escape Sequences
Escape sequences can be used as separate characters or embedded in string constants. Table 2.1 shows a
list of common escape sequences.

Escape Sequence Character

\n Newline
\t Tab
\a Bell /Beep
\b Backspace
\f Form feed
\r Return
\’ Single quotation mark
\” Double quotation marks
\\ Backslash
\xdd Hexadecimal notation

PRE-Lab

Setting Up Visual Studio C++

Step 0: Install Visual Studio C++


Install the Visual Studio setup on your PCs or laptops. Follow the video tutorial (Provided in reading
material section of the manual) steps effectively. Indicate which files / functional units are involved while
running a simple program in C++ Interactive development Environment.

Step 1: Create your app project

To create your app, first, you'll create a new project and solution.

1. If you've just started Visual Studio, you'll see the Visual Studio 2019 dialog box. Choose Create a
new project to get started.

2. Otherwise, on the menubar in Visual Studio, choose File > New > Project. The Create a new
project window opens.
3. In the list of project templates, choose Console App, then choose Next.
4. In the Configure your new project dialog box, select the Project name edit box, name your new
project Calculator Tutorial, and then choose Create.
5. An empty C++ Windows console application gets created. Console applications use a Windows
console window to display output and accept user input. In Visual Studio, an editor window opens
and shows the generated code:

6. To build your project, choose Build Solution from the Build menu. The Output window shows
the results of the build process.

In-Lab

Practice Tasks

Practice Task #1: [Expected Time= 25 mins]

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by
creating a class named 'Triangle' with the constructor having the three sides as its parameters?

Practice Task #2: [Expected Time= 25 mins]

Write a program to print the area of a rectangle by creating a class named 'Area' having two
functions. First function named as 'setDim' takes the length and breadth of the rectangle as
parameters and the second function named as 'getArea' returns the area of the rectangle. Length
and breadth of the rectangle (Take input from user)?
Evaluation Criteria:
As defined in Lab Rubrics.

Post-Lab

Task #1:

Take 20 integer inputs from user and print the following:


number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0.

You might also like