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

15-(3-13) C++ Basics

The document outlines the curriculum for EECS 348, Software Engineering I, focusing on C++ programming basics. It includes reminders for assignment due dates, an overview of C++ history, its popularity, and fundamental programming concepts such as variable types, operators, and input/output streams. Additionally, it provides practical examples for writing, compiling, and executing a simple C++ program.

Uploaded by

sakthixs01
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)
8 views

15-(3-13) C++ Basics

The document outlines the curriculum for EECS 348, Software Engineering I, focusing on C++ programming basics. It includes reminders for assignment due dates, an overview of C++ history, its popularity, and fundamental programming concepts such as variable types, operators, and input/output streams. Additionally, it provides practical examples for writing, compiling, and executing a simple C++ program.

Uploaded by

sakthixs01
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/ 42

EECS 348

Software Engineering I
David O. Johnson
Spring 2025

C++ Basics David O. Johnson EECS 348 (Spring 2025) 1


Reminders
• Assignment 4 due (today): 11:59 PM, Thursday, March 13
• Assignment 5 due: 11:59 PM, Thursday, March 27

C++ Basics David O. Johnson EECS 348 (Spring 2025) 2


Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 3


In-Class Problem Solution
• 14-(3-11) In-Class Problem Solution.pptx

C++ Basics David O. Johnson EECS 348 (Spring 2025) 4


Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 5


C++
• For the next few lectures, we are going to learn about programming
in C++.
• C++ is one of today’s most popular software development
languages.
• C++ is another compiled language closely related to C.
• C++ and C share a common syntax.
• C++ evolved from C, which was developed by Dennis Ritchie at Bell
Laboratories
• C++, an extension of C, was developed by Bjarne Stroustrup in 1979
at Bell Laboratories as part of his PhD work.
• C++ provides several features that “spruce up” the C language
• The main difference between C++ and C is that you can do object-
oriented programming in C++.
• However, C++ has other additional functionality, not available in C.

C++ Basics David O. Johnson EECS 348 (Spring 2025) 6


History of C++

C++ Basics David O. Johnson EECS 348 (Spring 2025) 7


TIOBE Index (January 2025)
Programming
2025 2020 2015 2010 2005 2000 1995 1990
Language
Python 1 3 7 7 7 25 23 -
C++ 2 4 4 3 3 2 1 2
C 3 2 1 2 1 1 2 1
Java 4 1 2 1 2 3 - -
C# 5 5 5 6 9 9 - -
JavaScript 6 7 8 9 10 7 - -
Go 7 17 37 184 - - - -
Visual Basic 8 19 234 - - - - -
SQL 9 9 - - 100 - - -
Fortran 10 29 31 25 15 18 5 9

C++ Basics David O. Johnson EECS 348 (Spring 2025) 8


TIOBE Index
• The TIOBE Programming Community index is an
indicator of the popularity of programming languages.
• The index is updated once a month.
• The ratings are based on the number of skilled
engineers world-wide, courses, and third-party
vendors.
• Popular web sites Google, Amazon, Wikipedia, Bing and
more than 20 others are used to calculate the ratings.
• It is important to note that the TIOBE index is not about
the best programming language or the language in
which most lines of code have been written.
C++ Basics David O. Johnson EECS 348 (Spring 2025) 9
C++ Standard Library
• C++ Standard Library
– C++ programs consist of classes and functions.
– Most C++ programmers take advantage of the rich
collections of classes and functions in the C++ Standard
Library.
• Two parts to learning the C++ world
– The C++ language itself.
– How to use the classes and functions in the C++ Standard
Library
• Many special-purpose class libraries are supplied by
independent software vendors

C++ Basics David O. Johnson EECS 348 (Spring 2025) 10


Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 11


Writing a C++ Program – Hello, World!

Step 1:
• Write the code for a program (source code) using an editor such
as vi or nano (language sensitive), save as file hello.cpp

#include <iostream>

int main ( ) {
std::cout << "Hello, world!\n";
return 0
}

C++ Basics David O. Johnson EECS 348 (Spring 2025) 12


Compiling a C++ Program – Hello, World!

Step 2:
• Compile the program to convert from the source code to an
“executable” or “binary” (or object code):

$ g++ hello.cpp -o hello

Step 3:
• If the compiler produces any errors, fix them and recompile

C++ Basics David O. Johnson EECS 348 (Spring 2025) 13


Executing a C++ Program – Hello, World!

Step 4:
• Once there are no programming errors and you have
executable code, run it:

$ hello.exe

Hello, world!

C++ Basics David O. Johnson EECS 348 (Spring 2025) 14


Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 15


Welcome to C++! Program
• // indicates a comment; extends to the end of the line
• Can still use /* …. */ combination
• A # directive is a message to the C++ compiler preprocessor
• #include <iostream> is library for I/O
• Like C, white spaces are ignored
• main() function is part of every C++ program
• Like C, C++ programs begin executing at function main()
• The rest of the program consists of classes and functions

C++ Basics David O. Johnson EECS 348 (Spring 2025) 16


C and C++ and UNIX Input/Output
• UNIX executable files (e.g., program.exe) create three data streams (stdin, stdout,
and stderr) that can be used to transfer data to and from a program.

• stdin is the input stream (std::cin for C++)


• stdout is the output stream (std::cout for C++)
• stderr is the error stream (std::cerr for C++)

• Redirection allows you to redirect the output or errors to different destinations,


such as files or pipes (more about this later).

program.exe

C++ Basics David O. Johnson EECS 348 (Spring 2025) 17


Welcome to C++! Program
• Typically, output and input in C++ are accomplished with streams of characters.
• When a cout statement executes, it sends a stream of characters to the standard
output stream object std::cout which is normally connected to the screen.
• The notation std::cout specifies that we are using a name, in this case cout, that
belongs to namespace std.
• The names cin (the standard input stream) and cerr (the standard error stream)
also belong to namespace std.

C++ Basics David O. Johnson EECS 348 (Spring 2025) 18


Welcome to C++! Program
Printing a line of text
std::cout << “Welcome to C++\n”;
• The << operator is referred to as the stream insertion operator.
• The value to the operator’s right is inserted in the output stream.
• The escape sequence \n means newline (i.e., move cursor to the beginning of the
next line on the screen).
• Using multiple stream insertion operators (<<) in a single statement is referred to
as concatenating, chaining or cascading stream insertion operations

C++ Basics David O. Johnson EECS 348 (Spring 2025) 19


Other Escape Sequences

C++ Basics David O. Johnson EECS 348 (Spring 2025) 20


Welcome to C++! Program
Printing a line of text
• The welcome message can be printed in other ways:
std::cout << “Welcome ”;
std::cout << “to C++\n”;

C++ Basics David O. Johnson EECS 348 (Spring 2025) 21


Welcome to C++! Program
The return statement
• When the return statement is used at the end of main the value 0 indicates that
the program has terminated successfully.
• According to the C++ standard, if program execution reaches the end of main
without encountering a return statement, it’s also assumed that the program
terminated successfully.

C++ Basics David O. Johnson EECS 348 (Spring 2025) 22


#using Directive
• using directives eliminate the need to repeat the std:: prefix as we
did in the earlier program.

• Once we insert these using declarations:


using std::cout;
using std::cin;
using std::endl;

• We can write:
cout instead of std::cout
cin instead of std::cin
endl instead of std::endl

• endl is the end-of-the-line character (a better alternative for \n)


C++ Basics David O. Johnson EECS 348 (Spring 2025) 23
Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 24


C++ Variable Types (Same as C)

• The most common types are: char, int, float, and double
• Strings are arrays of characters
• Declare a variable before you use it

/* declares an integer called x. Its value is not assigned.*/


int x;

/* declares two floating point numbers; set z equal to pi */


float y, z = 3.14159;
z = 4; /* now z is equal to 4 */

myVal = 2; /* An error because myVal is not declared. */

C++ Basics David O. Johnson EECS 348 (Spring 2025) 25


Basic C++
Variable Types
(Same as C)

C++ Basics David O. Johnson EECS 348 (Spring 2025) 26


Legal C++ Variable & Function Names
(Same as C)
A legal C++ variable or function name contains:
• Letters (a-z, A-Z)
• Digits (0-9)
• Underscores (_)

• Must always start with a letter or an underscore


• It cannot start with a number, contain spaces, or use reserved
keywords like "int" or "float" in the name.

Valid examples: age, total_count, myVar, _temp, x, y

Invalid examples: 2count, my variable, int, !value, my-var


C++ Basics David O. Johnson EECS 348 (Spring 2025) 27
C++ Arithmetic Operators (Same as C)
+ plus or addition
- minus or subtraction
* multiplication
/ division
% modulo

No exponent operator!!

Beware of division:
• If second argument is an integer, the result will
be an integer (floor – rounded toward zero):
9 / 10 = 0 whereas 9 / 10.0 = 0.9
• Division by 0 will cause a run-time error

C++ Basics David O. Johnson EECS 348 (Spring 2025) 28


C++ Logical Operators (Same as C)

== equal to
< less than
& bitwise AND
<= less than or equal
| bitwise OR
> greater than
^ bitwise XOR
>= greater than or equal
~ bitwise NOT
!= not equal
<< bitwise shift left
&& logical AND
>> bitwise shift right
|| logical OR
! logical NOT

C++ Basics David O. Johnson EECS 348 (Spring 2025) 29


C++ Bitwise Operators (Same as C)
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
& bitwise AND  0000 & 0001 = 0000 4 0100 4
5 0101 5
| bitwise OR  0000 | 0001 = 0001
6 0110 6
^ bitwise XOR  0011 ^ 0001 = 0010
7 0111 7
~ bitwise NOT  ~0011 = 1100
8 1000 8
<< bitwise shift left  0011 << 2 = 1100 9 1001 9
>> bitwise shift right  0011 >> 1 = 0001 10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
C++ Basics David O. Johnson EECS 348 (Spring 2025) 30
C++ Post- and Pre- Operators
(Same as C)
Operator Called Explanation
Increment x by 1,
x++ Post-increment
then use the new value x in the expression in which x resides
Use the current value of x in the expression in which x resides,
++x Pre-increment
then increment x by 1.
Decrement x by 1,
x-- Post-decrement
then use the new value x in the expression in which x resides
Use the current value of x in the expression in which x resides,
--x Post-decrement
then decrement x by 1.

int x=5; int y; int x=5; int y;


y = ++x; y = x++;
/* x == 6, y == 6 */ /* x == 6, y == 5 */

C++ Basics David O. Johnson EECS 348 (Spring 2025) 31


C++ Operator Precedence
(Same as C)
• Operator precedence means that some operations are done before others when
evaluating an expression.
• Operators with higher precedence are evaluated before those with lower
precedence.
• This concept is similar to the rules of arithmetic, where multiplication and division
take precedence over addition and subtraction.
• Here's the order of operator precedence in C++, from highest to lowest:
Parentheses: ()
Postfix operators: ++, --
Unary operators: +, -, !, ~, ++, --, (type)
Multiplicative operators: *, /, %
Additive operators: +, -
Relational operators: <, >, <=, >=
Equality operators: ==, !=
Logical AND operator: &&
Logical OR operator: ||
Assignment operators: =, +=, -= ... and so on
C++ Basics David O. Johnson EECS 348 (Spring 2025) 32
C++ Operator Associativity
(Same as C)
When an expression contains operators of the same precedence level, associativity
determines their evaluation order.

Left-Associative:
• Operators are evaluated from left to right.
• For instance, in a + b - c, addition and subtraction, being left-associative, will first
evaluate a + b, and then subtract c from the result.

Right-Associative:
• Although less common in C++, some operators are right-associative, meaning they
are evaluated from right to left.
• An example is the assignment operator =.
• In a = b = c, c is assigned to b, and then the resulting value of b is assigned to a.

C++ Basics David O. Johnson EECS 348 (Spring 2025) 33


Operator Precedence Example

C++ Basics David O. Johnson EECS 348 (Spring 2025) 34


C and C++ Keywords

C++ Basics David O. Johnson EECS 348 (Spring 2025) 35


Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 36


C++ Input/Output with Files
C++ provides the following classes to perform output and input of
characters to/from files:

ofstream: Stream class to write on files


ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.

C++ Basics David O. Johnson EECS 348 (Spring 2025) 37


C++ Writing to Text Files
Writing to a text file is performed in the same way we operated with cout:
// writing to a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile ("example.txt"); File example.txt:
if (myfile.is_open())
{ This is a line.
myfile << "This is a line.\n"; This is another line.
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

C++ Basics David O. Johnson EECS 348 (Spring 2025) 38


C++ Reading from Text Files
Reading from a text file is performed with the getline function from <string>.
// reading a text file
#include <iostream>
#include <fstream>
#include <string> File example.txt:
using namespace std;
int main () { This is a line.
string line; This is another line.
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{ stdout:
cout << line << '\n';
}
myfile.close(); This is a line.
} This is another line.
else cout << "Unable to open file";
return 0;
}
C++ Basics David O. Johnson EECS 348 (Spring 2025) 39
Any Questions?

C++ Basics David O. Johnson EECS 348 (Spring 2025) 40


In-Class Problem Rubric

Grading Level
Exceeds
Question Points Meets Expectations Unsatisfactory
Expectations
(80-89%) (0-79%)
(90-100%)
The bit value is
The bit value is
incorrect, but the
1a, b, c, d, e, f 14 correct and the Otherwise
work shows it is a
work is shown.
minor error
Code is basically
2 16 Code is correct correct, but contains Otherwise
a minor syntax error

C++ Basics David O. Johnson EECS 348 (Spring 2025) 41


In-Class Problem
1. Give the bit value of x in these C++ statements (assume 8 bits). Show your
work by giving the binary value before and after the operation (e.g., for x =
~5, show 0000 0101  1111 1010)
a) x = 0 & 1;
b) x = 0 | 1;
c) x = 3 ^ 1;
d) x = ~3;
e) x = 3 << 2;
f) x = 3 >> 1;
2. Rewrite the Python program below in C++ (Don’t forget the comment).

#Python function example


def add_one(num):
ans = num + 1
return ans
C++ Basics David O. Johnson EECS 348 (Spring 2025) 42

You might also like