Course Introduction
C++ Primer – GNU Compilation starters
C. Papachristos
Robotic Workers Lab
University of Nevada, Reno
CS-202
Your Instructor
CS-202 C. Papachristos
Christos Papachristos
Assistant Professor
Department of Computer Science & Engineering
Robotics Research @ Collaborative Robotic Workers Lab (Co-RoboWork Lab) - UNR
Coding / SW Engineering for Field Robotics
MS in Electrical & Computer Engineering
PhD in Autonomous (Aerial) Robotics
PE - 202D (Office) / EJCH - 253 (Lab)
Course Requirements
CS-202 C. Papachristos
Basic program design
Control structures for selection , iteration
Use functions , employ/implement prototypes, pass parameters
Multi-dimensional arrays (declaration/usage/passing as parameters)
Basic stream I/O (with external file usage)
ALL are Strict Prerequisites !
Course Rules
CS-202 C. Papachristos
Grading Policy (tentative):
You cannot earn a C in the course without having earned a C on both:
Plus/Minus grading will be assigned as indicated in the Syllabus.
Grade re-scaling may be assigned based on an outstanding or inferior Final exam.
Note: Also, you cannot earn a passing grade (D-) in the course without a passing grade on both categories.
Class presence will be required, tracked, and factored in, for the course’s Lab Sections. For general university policy regarding class absence, see UAM 3,020.
Component | Percentage |
Projects & Labs | 50% (Breakdown: 40% – 10% each) |
Midterm Exam | 20% |
Final Exam | 30% |
Course Rules
CS-202 C. Papachristos
Tasks & Responsibilities:
Weekly Projects – Bi-weekly MAX
Turn in via WebCampus! LATE submission (24hrs max) incurs 20% penalty.
After 24hrs NO SUBMISSION WILL BE ACCEPTED.
Lab Assignments – In-Lab! NO SUBMISSION WILL BE ACCEPTED AFTER.
Academic Dishonesty:
Cheating, plagiarism or otherwise obtaining grades under false pretenses constitute academic dishonesty according to the code of this university. Academic dishonesty will not be tolerated and penalties can include filing a final grade of "F"; reducing the student's final course grade one or two full grade points; awarding a failing mark on the coursework in question; or requiring the student to retake or resubmit the coursework. For more details, see the University of Nevada, Reno General Catalog.
(Also, refer to Academic Standards in course syllabus and online)
Course Rules
CS-202 C. Papachristos
Tasks & Responsibilities:
Weekly Projects – Bi-weekly MAX
Turn in via WebCampus! LATE submission (24hrs max) incurs 20% penalty.
After 24hrs NO SUBMISSION WILL BE ACCEPTED.
Lab Assignments – In-Lab! NO SUBMISSION WILL BE ACCEPTED AFTER.
Academic Dishonesty:
Note:
There exist widely accessible and reliable tools to cross-compare you code:
vs your classmates’ code !
vs students’ code from previous semesters !
Semantic analysis and comparison means IT WILL CATCH similarly structured code, � even if you rename your variables / change indentation / etc. !
Course Rules
CS-202 C. Papachristos
Academic Standards Policy for Writing Code – CSE Department:
Course Objectives
CS-202 C. Papachristos
Introductory-level experience to:
Basic software engineering techniques , Proper program design principles
Pointers , Dynamic Memory
C++ libraries , Classes – operators , the STL
(Coding) Tools:
Course Help
CS-202 C. Papachristos
Course & Projects:
Peer-Assisted Study Session (PASS) Leader:
Jazel A. Suguitan jazels@nevada.unr.edu
3 hrs in-class, 4 hrs PASS sessions
Teaching Assistants:
XinYing Wang, Bashira Akter Anima, Hudson Lynam, Yuchuan Liu
xinyingw@, banima@, hlynam@, ycliu@ [nevada.unr.edu]
50 mins per Lab section , Tutoring & Lab Quizzes
Disability Services:
Any student with a disability needing academic adjustments or accommodations is requested to speak with the Disability Resource Center as soon as possible to arrange for appropriate accommodations.
Course Week
CS-202 C. Papachristos
Course , Projects , Labs:
Projects will start this Thursday 1/23.
Labs will start this Thursday 1/23.
Monday | Tuesday | Wednesday | Thursday | Friday |
|
|
| Lab (8 Sections) |
|
| CLASS |
| CLASS |
|
PASS Session (Tentative) | PASS Session (Tentative) | Project DEADLINE | NEW Project |
|
Challenges
CS-202 C. Papachristos
Getting started:
The Linux environment (some transfer students could have a hard time).
Start with your Projects early on !
Familiarize yourselves with the Xubuntu environment at the ECC systems.
Don’t play down the significance of the Lab sessions.
Don’t wait to seek help. Benefit from all sources.
Your TAs.
Your PASS Leader.
WebCampus material (lectures, samples, etc.) & discussions.
The web… http://en.cppreference.com/w/cpp
Today’s Concepts
CS-202 C. Papachristos
Differences between programming languages
C++ vs Python
Compiled vs Interpreted programs
Programming “style” restrictions
C++ concepts (Learning C++ is only part of the deal)
Classes
Object-Oriented Programing
Object-Oriented Design
(But Remember: C++ is the “Latin” of programming languages)
Today’s Concepts
CS-202 C. Papachristos
Differences between programming languages
Compiled vs Interpreted programs
Programming “style” restrictions
C++ concepts (Learning C++ is still a significant part of the deal)
Classes
Object-Oriented Programing
Object-Oriented Design
(Remember: C++ is the “Latin” of programming languages)
Today’s Practices
CS-202 C. Papachristos
Using the Linux Operating System.
Developing under the GCC (GNU Compiler Collection) suite.
(IDEs such as Eclipse, XCode, Codeblocks etc. are not part of the supported toolchains, your programs will have to compile & run on installations like the ones on the machines of SEM321).
Goal : Code – Compile – Run
C++ Basic Info
CS-202 C. Papachristos
Created in 1979 by Bjarne Stroustrup – the originator. At Bell Labs (home of UNIX and C).
The story: “Invent a computer programming language so arcane and complex that no one except him would be able to use it.”
The background: “Languages like Simula and Ada.”
The anecdote: “Colleagues laughed a his first cut at an inscrutably hard language. Then, he went back to his lair and didn’t emerge until he had added references and templates.”
C++ Basic Info
CS-202 C. Papachristos
Added object-oriented features to C.
Renamed to C++ in honor of auto-increment operator.
Later standardized with several International Organization for�Standards (ISO) specifications.
Greatly influenced Java development (1991).
C++98
C++03
C++11
C++14
C++17
C++20
Note: OLD !
(and C++03 more or less just fixes C++98 defects)
Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
Modular units: functions
Program structure: hierarchical
Data and operations are not bound to each other
Examples:
C, Pascal, Basic, Python
Object-Oriented (OO)
Modular units: objects
Program structure: a graph
Data and operations are bound to each other
Examples:
C++, Java, Python
A collection of Objects
A hierarchy of functions
Procedural vs Object-Oriented
CS-202 C. Papachristos
The ATM Machine paradigm
Fundamental Software Development problems:
Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
Focused on the question: “What should the program do next?” Structure program by:
Large amount of data and/or tasks makes projects/programs unmaintainable.
Object-Oriented (OO)
Package-up self-contained & modular pieces of code.
The world is made up of interacting Objects.
Pack away details into boxes (Objects), allowing us to consider them in their abstract form.
Focus on their (numerous) interactions.
Key concepts:
A collection of Objects
A hierarchy of functions
Classes
CS-202 C. Papachristos
What is a … Class
C++ Classes are very similar to C Structs, in that they both include user-defined sets of data items, which collectively describe some entity such as a Student, a Book, an Airplane, or a data construct such as a String, a ComplexNumber, etc…
Bank Account
account number
owner’s name
balance
interest rate
more?
deposit money
withdraw money
check balance
transfer money
more?
Operations
(behaviors)
Type
Attributes (state)
String
sequence of characters
encoding
more?
compute length
concatenate
test for equality
more?
class BankAccount
class String
Classes
CS-202 C. Papachristos
What is a … Class
A non-primitive & user-defined data type containing:
Bank Account
account number
owner’s name
balance
interest rate
more?
deposit money
withdraw money
check balance
transfer money
more?
Operations
(behaviors)
Type
Attributes (state)
String
sequence of characters
encoding
more?
compute length
concatenate
test for equality
more?
class BankAccount
class String
Objects
CS-202 C. Papachristos
What is an … Object
A particular instance of a class.
Scrooge’s Account
Wilde’s Account
Smith’s Account
43-261-5
Adam Smith
$ 1,766 . 00
2.5 %
12-345-6
Ebenezer Scrooge
$ 9,999,999,999 . 99
0.0125 %
65-432-1
Oscar Wilde
$ 8 . 45
3.5 %
account number
owner’s name
balance
interest rate
class BankAccount
Bank Account
For any of these accounts, one can…
deposit money
withdraw money
check balance
transfer money
Interpreters, Compilers, Hybrids
CS-202 C. Papachristos
Interpreted Languages (e.g. JavaScript, Perl, Ruby)
translate & execute
source code
interpreter
compile
source code
binary code
execute
compiler
command
compile
translate & execute
source code
bytecode
Java Virtual Machine (JVM)
Java compiler
Interpreter translates source into binary operations and executes it.
Small, easy to write
Interpreter is a program unique to each platform (i.e. operating system).
Compiled Languages (e.g. C, C++)
Many other models: (e.g. Java, Python etc.)
Bytecode is platform independent
Java Virtual Machine (the target platform) is an interpreter that is platform dependent.
Compiler is platform dependent.
Code once compiled is expressed in the instructions of the target machine�(e.g. target architecture).
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
C++ source code
Linux
C++
compiler
Windows
C++
compiler
Linux
linker
Linux C++ binary
Linux C++ executable code
Windows
linker
Windows C++ binary
Windows C++ executable code
Linux C++
code library
binary library code
Windows C++
code library
binary �library code
Any
text editor
C++ Compilation & Linkage
CS-202 C. Papachristos
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
Optional
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
Just Compile them
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
And then Link them
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
Also Link to an external Library libm.so
Note: Actually libstdc++ requires libm, so with g++ it is automatically linked.
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
From:
C++ Compilation & Linkage
CS-202 C. Papachristos
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog1.o prog1.cpp
g++ -Wall –O3 –std=c++11 –c -o prog2.o prog2.cpp
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o
g++ -Wall –O3 –std=c++11 –o prog12 prog1.o prog2.o -lm
g++ -Wall –O3 –std=c++11 –o prog1 prog1.cpp -lm
Standalone Compile
Compile Objects & Link
Linking
Running under (Linux)
./prog1
From:
Hands-on C++ Compilation & Project Submission
CS-202 C. Papachristos
Log into Xubuntu at the ECC – or – install a Virtual Machine and the same Xubuntu OS on your laptop:
https://unr.canvaslms.com/courses/49327/files/folder/Projects/VirtualBox%20Installation%20Guides#
C++ Primer
CS-202 C. Papachristos
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Preprocessor directives (e.g. Includes), Function Prototypes
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
The main – Note: returns an int
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Variable Definition
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Console Output
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Console Input
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast!\n"
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Selection Structures /
Flow Control
C++ Primer
CS-202 C. Papachristos
Sample Dialog 1:
Hello new programming enthusiast!
Welcome to C++.
How many lines of C code have you written in your life?
1000
You might find C++ to have a very steep learning curve.
Sample Dialog 2:
Hello new programming enthusiast!
Welcome to C++.
How many lines of C code have you written in your life?
1000000
Your background might very well save the day!
User Types in
1000 at the prompt
User Types in
1000000
at the prompt
#include <iostream>
using namespace std;
int main()
{
int numberOfCodeLines;
cout << "Hello new programming enthusiast! "
<< "Welcome to C++.\n";
cout << "How many lines of C code have you written in your life?" << endl;
cin >> numberOfCodeLines;
if (numberOfCodeLines < 10000) {
cout << "You might find C++ to have a very steep learning curve." << endl;
}
else {
cout << "Your background might very well save the day!" << endl;
}
return 0;
}
C++ Primer
CS-202 C. Papachristos
Return value
C++ Identifiers & Variables
CS-202 C. Papachristos
C++ Identifiers
Can’t use keywords/reserved words.
Case-sensitivity and validity of identifiers.
Meaningful names!
Used for variables, class names, and more.
Variables
Must declare all data before use in program.
Variable Declaration
CS-202 C. Papachristos
C++ Variables
When we declare a variable, we tell the compiler:
When and where to set aside memory space for the variable.
How much memory to set aside.
How to interpret the contents of that memory;
int a ;
double b ;
What name we will be referring to that location in memory;
int a ;
double b ;
Variable Declaration
CS-202 C. Papachristos
C++ Variables
Syntax: <type> <legal identifier> ;
Examples:
int sum ;
float average ;
double grade = 98 ;
Must be declared before being used
Must be declared to be of a specific & known type �(e.g. int, float, char, etc.)
Don’t forget the semicolon at the end!
Variable Declaration
CS-202 C. Papachristos
C++ Variables
Naming conventions are rules for names of variables to improve readability
Different standards exist, suggested:
Start with a lowercase letter
Indicate "word" boundaries with an uppercase letter
Restrict the remaining characters to digits and lowercase letters
topSpeed bankRate1 timeOfArrival
Indicate "word" boundaries with an underscore
top_speed bank_rate_1 time_of_arrival
Note: variable names are still case sensitive!
Widely-used standard:
Google standard https://google.github.io/styleguide/cppguide.html
Data Types
CS-202 C. Papachristos
Primitive Types
Tables from:
Absolute C++
Copyright © 2016
Pearson, Inc.
All rights reserved.
Data Types
CS-202 C. Papachristos
Primitive Types
Mostly seen/used primitive types.
Tables from:
Absolute C++
Copyright © 2016
Pearson, Inc.
All rights reserved.
Data Assignment
CS-202 C. Papachristos
You can (and often should) initialize data in declaration statement
Note: Otherwise results can be “Undefined” if you don’t initialize!
int myValue; 1; 0; -1; 234251; -32141521;
int myValue = 0; 0;
Assigning data during execution
Value Categories (C++ Heritage from C) :
Lvalues (left-side) & Rvalues (right-side)
Lvalues must be variables
Rvalues can be any expression
Example: distance = rate * time;
Lvalue: "distance“
Rvalue: "rate * time" (Note: The entire expression)
???
OK
Note: More complicated than where they�can appear, but keeping it simple for now …
Note: Actually C++ has
GLvalues, Xvalues, PRvalues )
You can (and often should) initialize data in declaration statement
Note: Otherwise results can be “Undefined” if you don’t initialize!
int myValue; 1; 0; -1; 234251; -32141521;
int myValue = 0; 0;
Assigning data during execution
Value Categories (C++ Heritage from C) :
Lvalues (left-side) & Rvalues (right-side)
Lvalues must be variables
Rvalues can be any expression
Example: distance = rate * time;
Lvalue: "distance“
Rvalue: "rate * time" (Note: The entire expression)
Note: “Reading” from an Uninitialized variable is
Undefined Behavior
4.1 Lvalue-to-rvalue conversion
1 - A glvalue of a non-function, non-array type T can be converted to a prvalue. … If the object to which the glvalue refers is not an object of type T … or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.
Data Assignment
CS-202 C. Papachristos
???
OK
Compatibility of Data Assignments
Type mismatches
Cannot place value of one type into variable of another type !
But sometimes, a conversion is possible …
int intVar = 2.99; 2 is assigned to intVar!
Only the integer part “fits”, so that’s all that goes …
Called “Implicit Type Conversion”
Literals
2, 5.75, 'Z', "Hello World\n"
Also known as “constants”: can’t change in program
Data Assignment
CS-202 C. Papachristos
Literals / Literal Data
Examples:
2 // Literal constant int
5.75 // Literal constant double
'Z' // Literal constant char
"Hello World\n" // Literal constant string
Cannot change their values during execution
Called “literals” because you “literally typed” them in your program!
Program Data
CS-202 C. Papachristos
Constants / Constant Data
You should not use literal constants directly in your code –
It might seem obvious to you, but not so:
limit = 52 ;
Is this weeks per year... or cards in a deck?
Also allows you to change multiple instances in a central place.
Program Data
CS-202 C. Papachristos
Constants / Constant Data
Two ways to go about this:
One way: Preprocessor Define
#define WEEKS_PER_YEAR 52
(Note: there is no “=”)
Alternative way: Constant Variable
Append “const” keyword to declaration
const float PI = 3.14159;
Program Data
CS-202 C. Papachristos
This means that the Preprocessor will just replace the WEEKS_PER_YEAR text in the code before compilation.
PI is a fully-fledged variable !
Console Input / Output by-Example
Terminal/Console Input
#include <iostream>
using namespace std;
int main ( ) {
int int_value;
char cString_value[10];
cin >> int_value;
cin >> cString_value;
return 0;
}
CS-202 C. Papachristos
Console Input / Output by-Example
Terminal/Console Input
#include <iostream>
using namespace std;
int main ( ) {
int int_value;
char cString_value[10];
cin >> int_value;
cin >> cString_value;
return 0;
}
CS-202 C. Papachristos
Console Input / Output by-Example
Terminal/Console Input
#include <iostream>
using namespace std;
int main ( ) {
int int_value;
char cString_value[10];
cin >> int_value;
cin >> cString_value;
return 0;
}
Note: Make you type up to 9 characters (C-strings require +1 NULL-terminating character) otherwise you might get weird results…
CS-202 C. Papachristos
Console Input / Output by-Example
Terminal/Console Output
#include <iostream>
using namespace std;
int main ( ) {
int int_value;
char cString_value[10];
cout << "Give me an Integer" << endl ;
cin >> int_value;
cout << "Give me a String" << endl ;
cin >> cString_value;
cout << "Number: " << int_value << " and String: " << cString_value << endl;
return 0;
}
CS-202 C. Papachristos
Console Input / Output by-Example
Terminal/Console Output
#include <iostream>
using namespace std;
int main ( ) {
int int_value;
char cString_value[10];
cout << "Give me an Integer" << endl ;
cin >> int_value;
cout << "Give me a String" << endl ;
cin >> cString_value;
cout << "Number: " << int_value << " and String: " << cString_value << endl;
return 0;
}
CS-202 C. Papachristos
Time for Questions !
CS-202
CS-202 C. Papachristos