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

Week 1 - Lecture 2 - Basics of C++, Good Programming

1) The document discusses a lecture on the basic C++ environment and good programming practices. 2) It introduces programming and what a program and the process of programming are. 3) It then discusses the basics of a typical C++ environment, including the language definition, development tools like compilers and libraries, and popular IDEs for C++ like Dev C++. 4) The document provides examples of simple C++ code and explains aspects of the code like comments, input/output streams, and formatting output.

Uploaded by

DOHA aqeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Week 1 - Lecture 2 - Basics of C++, Good Programming

1) The document discusses a lecture on the basic C++ environment and good programming practices. 2) It introduces programming and what a program and the process of programming are. 3) It then discusses the basics of a typical C++ environment, including the language definition, development tools like compilers and libraries, and popular IDEs for C++ like Dev C++. 4) The document provides examples of simple C++ code and explains aspects of the code like comments, input/output streams, and formatting output.

Uploaded by

DOHA aqeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Week# 1

Introduction to Computers and Programming

Lecture# 2
Basic of Typical C++ Environment, Good Programming Practice
(Dr Taimur Ahmed)
Introduction to Programming
❑ What is a Program?
➢ A program is a precise sequence of steps to solve a particular problem
➢ A complete set of activities to be performed in a particular order –
what’s the purpose?

❑ What is Programming?
➢ A process of designing and building an executable computer program to
accomplish a specific computing result or to perform a specific task
➢ e.g solve some mathematical problems, video game for fun, edit
images/videos, an app to browse internet or send/receive
emails/messages, etc

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 2


Basics of a Typical C++ Environment
❑ C++ Language definition
❑ Program-development environment (tools)
❖ compiler, linker, editor, debugger
❑ C++ Standard Library (software)
❖ precompiled routines you can use

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 3


Programming IDE
❑ What is an IDE?
➢ Integrated Development Environment: A software that provides a user-
friendly environment for building applications that combines common
developer tools into a single graphical user interface (GUI).
❑ An IDE typically consists of:
➢ Source code editor: A text editor that can assist in writing software
code with features such as syntax highlighting with visual cues,
providing language specific auto-completion, and checking for bugs as
code is being written.
➢ Local build automation: Utilities that automate simple, repeatable tasks
as part of creating a local build of the software for use by the
developer, like compiling computer source code into binary code,
packaging binary code, and running automated tests.
➢ Debugger: A program for testing other programs that can graphically
display the location of a bug in the original code.

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 4


IDE for C/C++
❑ Available IDEs for C/C++
➢ Dev C++
➢ Visual Studio Code
➢ Eclipse
➢ Net Beans
➢ Code Blocks
➢ MANY MORE

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 5


IDEs for C/C++
❑ Dev C++
➢ Open source
➢ Easy to use and setup
➢ Simple full-featured IDE for C/C++ coding
➢ Debugging feature
➢ Syntax highlighting
➢ Project/file manager
❑ Dev C++ installation
➢ Download .exe file from https://sourceforge.net/projects/orwelldevcpp/

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 6


Lets Code!

1 // This is my first program in Programming Fundamentals course


2 // Single-line comments.
3 #include <iostream>Function main returns an
4 integer { begins Preprocessor
value.
Left brace function directive to
5 // function main begins
body.program include
execution
Function input/output stream
main appears
6 int main() header
exactly once file <iostream>.
in every C++
7 {
program..
8 std::cout << "Welcome to Programming Fundamentals - COMP 111\n";
9 Corresponding right brace }
10 return 0; // indicate thatbody.
ends function program ended successfully
11 Name coutStream insertion
belongs to operator.
12 } // end function main namespace std.

Keyword return is one of


Welcome to Programming Fundamentals - COMP 111
several means to exit
function; value 0 indicates
program terminated
successfully.

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 7


Lets Code!
❑ Preprocessor directives
➢ Processed by preprocessor before compiling
➢ Begin with #
❑ Input/output streams in C++
➢ cin (pronounce “see in”)
❖ Standard input stream
❖ Normally keyboard
➢ cout(pronounce “see out”)
❖ Standard output stream
❖ Normally computer screen
❑ Comments
➢ Document programs - ALWAYS
➢ Improve program readability
➢ Ignored by compiler
➢ Single-line comment
❖ Begin with //

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 8


Lets Code!
❑ Standard output stream object
➢ std::cout
➢ “Connected” to screen
➢ <<
❖ Stream insertion operator
❖ Value to right (right operand) inserted into output stream

❑ Namespace
➢ std:: specifies using name that belongs to “namespace” std
➢ std:: removed through use of using statements

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 9


Lets Code!
❑ Escape characters \
➢ Indicates “special” character output
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 10


Lets Code!

1 // Lecture 2
2 // Printing a line with multiple statements.
3 #include <iostream>
4
5 // function main begins program execution Multiple stream insertion
6 int main() statements produce one line
7 {
of output.
8 std::cout << "Welcome ";
9 std::cout << "to C++!\n";
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main

Welcome to C++!

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 11


Lets Code!

1 // Fig. 1.5: fig01_05.cpp


2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 // function main begins program execution Using newline characters to
6 int main() print on multiple lines.
7 {
8 std::cout << "Welcome\nto\n\nC++!\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome
to

C++!

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 12


C++ Code Execution Steps
Edit
Program created in the editor
(Dev-C++, Editor foo.cpp and stored on disk.
emacs)
Preprocessor strips out comments, expands
Preprocessor macros

Compiler creates object code and stores it on


Compile (and Link) foo.o disk (.o file)
(CC , g++) Compiler
Linker links the object code with the libraries,
C++ Library Linker foo stores in a.out on disk.
(a.out)

Loader
Loader puts program
..
in memory (RAM)
Run ..
..
(load and execute)
CPU takes each instruction and executes it, possibly
./foo, ./a.out
CPU storing new data values in memory (RAM) as the
..
..
program executes.
..

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 13


Good Programming Practices!

What you must know?


Skills required for Programming
❑ Analytical Skills - Paying Attention to Details
➢ Fully understand the problem at hand
➢ Pay attention to the logic e.g. Ahmad sleeps 30 hours a day
❑ Reusability in mind
❑ Think about User Interfaces
❑ Understand the fact that computers are stupid
❑ Comment the code (always)

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 15


Reusability of a Program

❑ While writing a program, always keep


in mind that it could be reused at
some other time
❑ Write in a way that it can be used to
solve some other related problem
❑ Suppose we have to calculate the area
of a given circle. We know the area
of a circle is (Pi * r2). Now we have
written a program which calculates
the area of a circle with given
radius. At some later time we are
given a problem to find out the area
of a ring. The area of the ring can
be calculated by subtracting the area
of outer circle from the area of the
inner circle. Hence, we can use the
program that calculates the area of a
circle to calculate the area of the
ring.

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 16


Think about Good User Interfaces (UI)
❑ Never assume that computer users know a lot of things,
this is a big mistake
❑ Never assume the user of your program as computer literate
❑ Try to design your UIs in a way to minimize the chances of
errors/mistakes
❑ Always provide an easy to understand and easy to use
interface that is self explanatory

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 17


Think of computers as stupid entity
❑ Computers do exactly what you tell them to do: no more, no less
-- unlike human beings.
❑ Computers can't think by themselves. In this sense, they differ
from human beings.
❑ For example, if someone asks you, “What is the time?”, “Time
please?” or just, “Time?” you understand anyway that he is
asking the time, but computer is different.
❑ Instructions to the computer should be explicitly stated.
Computer will tell you the time only if you ask it in the way
you have programmed it.
❑ When you're programming, it helps to be able to "think'' as
stupidly as the computer does, so that you are in the right
frame of mind for specifying everything in minute detail, and
not assuming that the right thing will happen by itself

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 18


Always Comment the Code
❑ Always comment the code liberally.

❑ Comments are used to explain the functioning of the


programs. It helps the other programmers as well as the
creator of the program to understand the code.

❑ The comment statements do not affect the performance of


the program as these are ignored by the compiler and do
not take any memory in the computer.

Lecture# 2 - Basic of Typical C++ Environment, Good Programming Practice | 19

You might also like