Hochiminh University of Technology
Computer Science and Engineering - [CO1011 - 501127]
Fundamentals of
C++ Programming
Basic components
in C/C++
Lecturer: Dustin Nguyen, PhD.
Outcomes
❖
Be able to explain the source code using comments
❖
Writing clear code with indent and rules
2
“C makes it easy to shoot yourself in the foot; C++ makes it harder,
but when you do, it blows away your whole leg.”
– Bjarne Stroustrup
Today’s outline
❖
Program structure
❖
Data types and operators
❖
Variable declarations
❖
Integers
❖
Problem solving
4
Program structure
Program structure
❖
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "";
cout << “Login (please input your name): “ << endl;
getline(cin, name);
cout << "Hello " << name << “! Welcome to C++ world.” << endl;
return 0;
}
#include <stdio.h>
int main() {
char name[20] = "";
printf(“Login (please input your name): “);
gets(name);
printf(“Hello %s! Welcome to C world\n.", name);
return 0;
}
6
Program structure
❖
Preprocessing directive: anything begin with #
❖
define, undef, etc.: constant, macros
❖
include: source inclusion (use libraries or additional code)
❖
pragma: specify diverse options for compiler
❖
namespace: use to group components of a module, library, or a pack of smaller
libraries.
❖
main(): the entry point of our program. This is where everything start.
7
Program structure
❖
Global variables definition: these variable are visible to all classes and
functions in the program
❖
Scope: global vs. local
❖
Different between C and C++ in variable declaration
❖
Statements: instructions, operations, variable declarations, or a call to other
functions. Each statement ends with “;”
❖
Structure vs. Class definition and implementation
8
Program structure
❖
Another example
#include <iostream>
#include <string>
int main(int narg, char* argv[]) {
std::cout << “******************************************************”
std::cout << “*
This is an example of C++ program
*”
std::cout << “* It does nothing but print this text on the screen. *”
std::cout << “* Observe the difference between this code and the
*”
std::cout << “* previous code. What is the difference?
*”
std::cout << “******************************************************”
return 0;
}
9
<<
<<
<<
<<
<<
<<
std::endl;
std::endl;
std::endl;
std::endl;
std::endl;
std::endl;
Program structure
❖
Simple template
#include <something>
// define data structure
// define functions
// declare global variables
int main(int narg, char* argv[]) {
// local variables
/* Your code should be put here.
* Remember that in C++ you can declare variables where you need it.
* However, it is not good to declare variables that way since it
* is hard to maintenance, modify, and expand your code in the future.
* You should consider the scope of your variables before you declare
* them.
*/
return 0;
}
10
Comments
❖
Two types
❖
Single line comments: anything after //
❖
❖
a = 0; // set variable a to 0
Block comments: anything between /* and */
❖
b = 1; /* set b to 1.
remember that the value variable b
can be changed later */ c = -1;
11
Indents, coding style
❖
❖
Use indents to enhance your code
❖
Easy to manage flow of code
❖
Easy to read code
Coding requires skills and the programmer, in most of cases need to follow
rules of their community.
12
Compile the program
❖
Simplest way: using IDE
❖
❖
Visual Studio, Xcode, KDE IDE, Eclipse, etc.
Manually: gcc, g++
❖
gcc example.c
❖
g++ example.cpp
❖
Compile and link separately
❖
Use other build system: e.g. CMake
13
Data types and operators
Data types and operators
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "";
cout << “Login (please input your name): “ << endl;
getline(cin, name);
cout << "Hello " << name << “! Welcome to C++ world.” << endl;
return 0;
}
#include <iostream>
#include <string>
int main(int narg, char* argv[]) {
std::cout << “******************************************************”
std::cout << “*
This is an example of C++ program
*”
std::cout << “* It does nothing but print this text on the screen. *”
std::cout << “* Observe the difference between this code and the
*”
std::cout << “* previous code. What is the difference?
*”
std::cout << “******************************************************”
return 0;
}
15
<<
<<
<<
<<
<<
<<
std::endl;
std::endl;
std::endl;
std::endl;
std::endl;
std::endl;
Data types and operators
❖
Basic data types
❖
Numeric types: integer, floating-point data
❖
Character/String: character, array of characters, struct/class
❖
Boolean: true/false
❖
Structure/Class: defined by user, fully customized
❖
Enum: names associated with integer values
❖
auto: must be associated with an assignment operation (only in modern C++)
16
Data types and operators
❖
❖
Basic operators
❖
=: assignment. E.g.: x = 8;
❖
+, -, *, /, %, etc. : arithmetic operators. E.g.: a = a + b - 5;
❖
^, ~, &, |, >>, <<: bitwise operators. E.g.: a = a^b;
❖
!, &&, ||, >, <, ==, etc. : logic operators. E.g.: b = a && c || (!a);
Other operators
❖
<<, >>, ::, ?:, ., etc.
17
Data types and operators
❖
Compound assignments:
❖
+=, -=, *=, /=, %=
❖
>>=, <<=, &=, !=, ^=
❖
a <op>= b is equivalent to a = a <op> b
❖
E.g.: x += 8;// equivalent to x = x + 8
18
Data types and operators
❖
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int x, y, ret;
cout << “Input x: ”;
cin >> x;
cout << “Input y: ”;
cin >> y;
cout << “x + y = ” << x + y << endl;// compute value inside statement
/* use another variable to hold the result
* before print it on screen */
ret = (x - y) * (x + y);
cout << “x*x - y*y = ” << ret << endl;
cout << “x mod y = ” << x % y << endl;
return 0;
}
19
Variable declaration
Variable declaration
❖
[optional] <type> <variable name>[<array declaration>][=assigned values]
❖
int i, j;
❖
float x = 0.5f;
❖
const char* depName = “BK-CSE”;
❖
volatile int noOpt = 100;
❖
etc.
21
Variable declaration
❖
Rules for variable name (identifiers, in general):
❖
A valid identifier is a sequence of one or more letters, digits, or underscore characters (_).
❖
Spaces, punctuation marks, and symbols cannot be part of an identifier.
❖
Identifiers shall always begin with a letter/_.
❖
Case sensitive
❖
Reserved keywords:
❖
alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t,
char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double,
dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int,
long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected,
public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast,
struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union,
unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
22
Variable declaration
❖
Example
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
char name[50] = “”;
int
age;// user age
float height;
bool isStudent = false;// this flag is true if this is a student
cout << “Please input your name: ”;
gets(cin, name);
cout << “How old are you? ”;
cin >> age;
cout << “How tall are you? ”;
cin >> height;
cout << “Are you a student?”;
// process more...
return 0;
}
23
Assignment statement
❖
<left operand> = <expression>;
❖
return <left operand>
❖
<left operand> can’t be constant
❖
Example:
❖
pi = 3.1415;
❖
keyPressed = ‘q’;
24
Assignment statement
❖
At declaration time:
❖
❖
int x = 10;
Other syntaxes
❖
int y{8};
❖
AnimalC monkey(10.5, 30);
25