CS_1002_3_C-- Overview and Basics
CS_1002_3_C-- Overview and Basics
CS_1002_3_C-- Overview and Basics
(CS 1002)
1-5
*For more information: https://linuxhint.com/best-text-editors-for-ubuntu/
From a High-level Program to an Executable File
Note
Errors occurring at any step will prevent execution of the step that follows.
1-6
What Is a Program Made Of?
Common elements in programming languages:
– Key/reserved words (predefined meaning)
– Programmer-defined identifiers (rules apply)
– Operators (e.g., + for “add, * for multiply)
– Punctuation (symbols that organize, e.g., comma (,),
semicolon(;), parentheses, etc.)
– Syntax ( rules of “grammar” )
1-7
First C++ Program
Preprocessor Directive
Standard Namespace
main function
1-8
Preprocessor Directives
#include<iostream>
# is a preprocessor directive
• Examples…
Example Program 1
#include <iostream>
using namespace std;
int main()
{
int num1 = 5, num2, sum;
num2 = 12;
1-11
Example Program 2
#include <iostream>
using namespace std;
int main()
{
int num1 = 5, num2, sum;
cout << “Enter second number: “;
cin >> num2;
sum = num1 + num2;
cout << “The sum is “ << sum;
return 0;
}
1-12
Key Words
• Also known as reserved words
• Have a special meaning in C++
• Can not be used for another purpose
• Written using lowercase letters
• Examples in program (shown in green):
using namespace std;
int main()
1-13
Some C++ Reserve Words
auto break int long
case char register return
const continue short signed
default do sizeof static
double else struct switch
enum extern typedef union
float for unsigned void
goto if volatile while
Programmer-Defined Identifiers
• Names made up by the programmer
• Not part of the C++ language
• Used to represent various things, such as
variables (memory locations)
• Example in program (shown in green):
int num1;
1-15
Variables
• A variable is a name for a cell in computer memory
(RAM) where a value can be stored.
• The memory cell (variable) holds a data value
• A variable must be defined before it can be used
• Example variable definition (declaration):
double num1;
1-16
Variables
- Variables are identifiers which represent some
unknown, or variable-value.
x = a + b;
Speed_Limit = 90;
Declaring Variables
TYPE <Variable Name> ;
Examples:
int marks;
double Pi;
int suM;
char grade;
Marks
FE07
Variables Initialization
• Variables may be given initial values, or
initialized, when declared. Examples:
length
int length = 7 ; 7
diameter
float diameter = 5.9 ; 5.9
initial
char initial = ‘A’ ; ‘A’
Operators
• Used to perform operations on data
• Many types of operators:
– Arithmetic: +, -, *, /
– Assignment: =
– Input: (stream extraction) >>
– Output: (stream insertion) <<
1-28
Comments
• Two types of comments
1. Single line comment: // my program
2. Multi-line (paragraph) comment:
/* my
Program */
int main ( )
{
string name; //Name of student
cout<< “Enter you name";
cin>>name;
/* Now print hello , and students name */
cout<< “Hello “ << name;
return 0;
}
Omitting std:: prefix
- using directive brings namespaces or its sub-items
into current scope
#include<iostream>
using namespace std;
int main()
{
cout<<“Hello World!”<<endl;
cout<<“Bye!”;
return 0;
}
Namespaces
• Namespace pollution
– Occurs when building large systems from pieces
– Identical globally-visible names clash
– How many programs have a “print” function?
– Very difficult to fix
int i, j;
i = 7;
7 = i;
j * 4 = 7;
rvalue and lvalue
• Are the two occurrences of “a” in this expression the
same?
a = a + 1;
One on the left : location of the variable (whose name is
a, or address);
One on the right: value of the variable (whose name is
a);
• Example:
cout << “Total sales are $” << sales << ‘\n’;
cin >> Sales1 >> Sales2 >> Sales3;
The >> Operator
3-38
The >> Operator
• When ENTER key pressed, keyboard input goes to the input buffer
(where it is stored as characters)
1 2 3 T O M B R O W N 7 2 . 5 eol
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 position
• String “123” converted to whole number (int) 123 and stored into
variable count. Next, >> starts at pos 5 (space)
3-39
String input (Variables)
// Read first and second name
#include<iostream>
#include<string>
int main() {
string first;
string second;
cout << “Enter your first and second names:";
cin >> first >> second;
cout << "Hello “ << first << “ “ << second;
return 0;
}
Any Questions