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

Module 1 CSC 201

Uploaded by

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

Module 1 CSC 201

Uploaded by

peacewrld27
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

BABCOCK e-LEARNING Script on

Programming in C++

 COURSE CODE—COSC 201


 COURSE TITLE—Programming in C+

+
 AUTHOR---Adekola, Olubukola Daniel

 SCHOOL—Computing and

Engineering Sciences
 DEPARTMENT—Computer Science
COURSE DESCRIPTION:
An introduction to programming
methodology using C++, problem solving,
algorithm development, basic data types,
control structures, pointers, arrays, functions,
searching and sorting. Major differences
between Procedural and Object oriented
programming. Program style, program
design, code documentation Techniques, and
program correctness. Other topics include file
processing, introduction to stacks and queues
Course Objectives:
By the end of this course, student will be able
to:
 differentiate between C and C++ language

 identify new features that made C++ better

 understand the concept of OOP analysis and

design
 identify the efficiency of code reusability

inherent in C++
 design and develop functional, seamless and

quick to deliver programs with little or zero


tolerance for errors
Bulletin Content: CSC201

Introduction to problem solving methods and


algorithm development, designing, coding,
debugging and documenting programmes using
techniques of a good programming language style,
programming language and programming
algorithm development. A widely used
programming language (such as C++) should be
used in teaching the above.
Course Requirements:

Course Requirements:
 Practical Assignment, Experiment and Quizzes

 Term papers. Credit may be deducted for work

not turned in as assigned and as at when due.


 Mid-Semester Examination
 Final Examination
Software Requirement:

Dev C++ Compiler or other Integrated


Development Environment (IDE) compiler. Most
are freeware.

Recommended Texts:
1) Dietel P. and Deitel H.( 2012), C++ How To

Program, Eight Int’l Edition, Edinburgh Gate,


England , Pearson Education Ltd.,
2) John R. Hubbard (2007), Schaum’s Outline

Series of Theory and Problems of Programming


With C++, United States, Mc Graw Hill.
UNIT -1.1: In The Beginning (Brief
History of C++)

In 1972, Dennis Ritchie of Bell Laboratories developed C


programming language to create a new Operating
System(OS). C was concise and could produce compact
and speedy programs. It became widely known as the
UNIX operation system’s development language.
In 1985, Bjarne Stroustrup working on his Ph.D. at Bell

Laboratories, developed the C++ programming


language.
Respected features inherent in C included portability,

speed, precision and strongly typed nature. C++ original


idea is to add more capability to C. C++, a superset of C,
is an addition of object-oriented programming into the C
without actually changing the component of C
1.2 C++ Language Basics

C++ is an extension of C language which provides capabilities for object-


oriented programming.
C++ inherited portability (ability of code to run on variety of platforms)
property from its predecessor -C
Major constructs include Data Structures, Classes and Objects
Data Structures: This implies the arrangement of data i.e. its structure in
the computer memory. E.g. the array structure-If A is a named array, its
elements are referenced by subscripting A[i].
Classes: You are used to a program’s in-built data types like int which is a
fundamental data type for integer value representation, making classes is
simply making your own user-defined types.
Objects are essentially reusable software components that model items in
the real world. Modular, object-oriented programs are easier to
understand, correct and modify.
1.0 My First Program in C++
The following shows basic programming concepts
which does not necessarily include object-oriented
paradigm:
// My first C++ program to display welcome message on the screen
//double forward slash for single line comment
/* this is for multiple lines comment – this property is gained from C language */
#include <iostream> // allows output function to work
int main() // function main begins program execution
{ //open curly bracket is for start of a function
std::cout<<"Hello, welcome aboard!\n"; /* to display message in the quote

back slash and n is to print newline after the message*/


//blank line for program easy readability
return 0; // program ended successfully
} // close curly bracket to indicate end of a function

Output:
Hello, welcome aboard!
Basic features in the program

 Keywords: are generally word reserved by C++ compiler


for predefined use (they must not be used as your
identifier) e.g. int
 Std::cout is the standard output stream object.
 The “<<” is the stream insertion operator. It makes
the values to the right of this operator to be inserted in the
output stream.
 The backslash(\) is the escape character. Notifies It gives
the compiler notice that a special character is to be output
next. E.g. \n is for newline.
 Symbol (;) semicolon is the statement terminator; but
some like the #include, the conditional statement (if) do
not end with a semicolon
C++ basic escape sequence and its use

Escape Description and Use


Sequence
\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
\a Alert. Sound the bell
\\ Print a backslash character
\’ Print a single quote character. As this will not print on its own
if in a quote
\” Print a double quote character
A combination of the backslash and the next character is termed escape sequence
Black Box
My Second program

1. /* program to compare integer values input from the keyboard; it uses if statement and

2. relational operator. */
3. #include <iostream>
4. using std::cout; Lines 4 – 6: are using directives that removes the
5. using std::cin;
6. using std::endl; need to repeat the std:: in each of the statement like
7. cout. Alternatively, line 4 – 6 can be written as a single
8. int main() line statement as follow:
9. { Using namespace std
10. int num1; //variable name declaration
11. Int num2; //note lines 10 and 11 can also be written as one line: int num1, num2; */
12.
13. cout << “Enter the two integers to compare”; // help the user know what to do next
14. cin >> num1; // receive input from keyboard and store in memory location num1
15. cin >> num2; //num2 is also a temporary memory store as num1
16.
17. If (num1 > num2) // if statement is a conditional test statement
18. cout << num1 << “is greater than “ << num2 <<endl;
19 }
Operators, precedence and
associativity

Operators Associativity Type


() Left to right but settles the Grouping parentheses
innermost bracket first
* / % Left to right Multiplicative
+ - Left to right Additive
<< >> Left to right Stream
insertion/extraction
< <= > >= Left to right Relational
== != Left to right Equality
= Right to left Assignment
END OF MODULE ASSESSMENT (EMA)

Assignments/Tasks for the students:


1. Identify and correct the errors in each of the following statements
a) if (number <7 );
cout << “number is less than 7\n”;
Answer: cout <<“number is less than 7\n”
No semicolon because of the if statement
b) if (number => 7)
cout << ”number is equal to or greater than 7\n”;
2. Write a program that inputs three integers from the keyboard and print the
sum, product, and smallest of the numbers. Ensure your program is
interactive.
//program that inputs three integers from keyboard and print the
sum,product and smallest of the number//
#include<ios stream>
int main()
{
3. What does the following code print?
Cout << “*\n**\n***\n****\n”;
4. Write a program that reads an integer and
determines and prints whether it is even or
odd. [Hint: Use modulus operator]

5. State the order of evaluation of the operators


used in the following:
X = 8 + 15 * (6 – 2) -1

You might also like