0% found this document useful (0 votes)
82 views39 pages

Introduction To C++

The code declares double variables a and b and assigns values, adds a and b and assigns the result to c, then prints c. It also declares a string variable message, assigns the text "peace on earth" to it, and prints the message. This program demonstrates basic variable declaration and assignment of numerical and string data types, as well as output statements.

Uploaded by

Ilmaf 313
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)
82 views39 pages

Introduction To C++

The code declares double variables a and b and assigns values, adds a and b and assigns the result to c, then prints c. It also declares a string variable message, assigns the text "peace on earth" to it, and prints the message. This program demonstrates basic variable declaration and assignment of numerical and string data types, as well as output statements.

Uploaded by

Ilmaf 313
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/ 39

Introduction to C++

Dwi Ary

Department of Statistics
Faculty of Science and Data Analytics
Institut Teknologi Sepuluh Nopember
2023
Programming Languages
1950s FORTRAN (FORmula TRANslator)
1960s BASIC language (Beginner’s All-purpose
Symbolic Instruction Code)
1970s UCSD Pascal
1970s C language
1980s C++ language

2
How Computer Executes a Task
Statement: A = A + B
Meaning: add the number B to the number A

How CPU executes this task:


• (FETCH A, R1) copy the binary of A from RAM to the CPU
register R1
• (FETCH B, R2) copy the binary of B from RAM to the CPU
register R2
• (ADD, R1, R2, R3) copy the contents of R1 and R2 to the adder,
perform the addition, and store the results in
register R3
• (STORE, R3, A) copy the content of R3 to the RAM address of A

3
Inventor

It was created by Bjarne


Stroustrup and his team at Bell
Laboratories (AT&T, USA).

C++ was derived from the C


programming language

Bjarne Stroustrup

4
Characteristics
C++ contains all the features available in C:
• Universally usable modular programs
• Efficient, close to the machine programming
• Portable programs for various platforms

C++ supports the concepts of object-oriented


programming (OOP):
• Data abstraction
• Data encapsulation
• Inheritance
• Polymorphism

5
Object-Oriented Programming

• OOP objects combine data (properties) and


functions (capacities).
• Advantages:
• Reduced susceptibility to errors
• Easy re-use
• Low maintenance requirement

6
Error and Warning

Error
Indicating issues in your syntax.
Be sure to start with the first error shown
when troubleshooting the errors.

Warning
To draw your attention to a possible error
in the program’s logic

7
Get Started to C++
Supporting applications:

Dev-C++ Codeblocks

A file containing C++ code is conventionally designated by one of the suffixes:


.c .cc .cpp .cxx

8
Try This Code!

#include <iostream>
using namespace std;

int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}

9
Try This Code!

Header #include <iostream>


Predefined names using namespace std;
Type of function
Function name
int main()
Beginning of function {
int year;
year = 1821;
Function block
cout << year << "\n";
return 0;

End of function }

10
Standard Namespace
Immediately after the include statements, we state:
using namespace std;

which declares that the names of the functions


defined in the standard std system library will be
adopted in the code.

If we do not make the “using namespace std”


declaration, then we would have to state:
std::string a;

11
The Main Function
Each C++ application has a main function.
When execution has been concluded, the main
function returns the integer 0.

The main function has the general syntax:


Indicating that an integer will be returned on completion
int main() Marking the beginning and the end of the enclosed main
{ program consisting of various instructions
… Additional lines of code
return 0;
Semicolon is a delimiter, marking the end of the
} preceding command return 0, which concludes the
execution

12
Grammar and Syntax
Name of a Variable
In C++, the name of a variable must start with a letter and contain only
letters, numbers, and the underscore (_).

#include <iostream>
using namespace std;

int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}

13
Grammar and Syntax
Name of a Variable
More detail rules:
- A name contains a series of letters, numbers, or underscore
characters
- The first character must be a letter or underscore
- There are no restrictions on the length of a name and all the
characters in the name are significant
- C++ keywords are reserved and cannot be used as names

14
Keywords in C++

15
Grammar and Syntax
C++ is case sensitive
#include <iostream>
using namespace std;

int main()
Year ≠ year
{ echidna ≠ echiDna
int year; return ≠ Return
year = 1821;
cout << Year << "\n";
return 0;
}

16
Grammar and Syntax
A semicolon (;) to end a statement
If we do not include the semicolon, the compiler will assume that the
statement in the next line is a continuation of the statement in the
present line.

#include <iostream>
using namespace std;

int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}

17
Grammar and Syntax
Multiple command in a line
Two or more statements can be placed in the same line provided they are
separated with semicolons.
#include <iostream>
using namespace std;

int main()
{
int year; year = 1821;
cout << year << "\n";
return 0;
}

18
Grammar and Syntax
In-line comments
In-line comments may be inserted following the double slash “//”.

#include <iostream>
using namespace std;

int main()
{
int year;
year = 1821; // to return year in 1821
cout << year << "\n";
return 0;
}

19
Grammar and Syntax
Commentary
All text enclosed between a slash-asterisk pair (/*) and the converse
asterisk-slash pair (*/) is commentary and ignored by the compiler.
/*-----Running the first code in C++----*/
#include <iostream>
using namespace std;

int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}

20
Question 1
How many commands are executed in the following
line?
a = 3.0; // b = 4.0;

21
Classification
The fundamental types in C++ are:
- Integer types
Arithmetic types
- Floating-point types
- Void type For expressions that do not represent a value

Data types:
• Numerical
• Boolean
• Characters
• Strings, etc.

22
Numerical Variable Declaration
Every numerical variable must be declared either as
an integer or as a real number registered in single or
double precision.
Example:
int a; Integer int a;
float b; a = 875;
Real
double c; int a = 875;
float b = -9.30;
int i; float c = 10.45e-3;
int j;
int i, j; double pi=3.141592653589793238;
Initialized numerical variables

23
Numerical Variable Declaration

24
Boolean Variables
The result of a comparison or a logical association
using AND or OR.
A Boolean variables can be either false or true.
Internal value for true will be represented as the
numerical value 1 and false by a zero.
bool hot;
hot = true;

bool hot = true;

25
Characters
A single character is encoded according to the ASCII
protocol.

char a;
a = ‘B’;

char a = ‘B’;
char a = 66;

char a = ‘B’;
int c = a;

26
Escape Sequences

27
Strings
A string is an array of characters.
It consists of a sequence of characters enclosed in
double quotes.
string name;
name = “Today is a beautiful day”;

28
Other Data Types

29
Question 2
What are the values of the integers c and d evaluated
by the following statements?
char a = ‘=‘; int c = a;
char b = ‘1’; int d = b;

30
Question 3
Which of the variable definitions shown below is
invalid or does not make sense?
int a(2.5); const long large;
int b = '?’; char c('\'');
char z(500); unsigned char ch = '\201';
int big = 40000; unsigned size(40000);
double he's(1.2E+5); float val = 3/2;

31
Example
#include <iostream>
using namespace std;

int main()
{
double a = 4;
double b = 2;
double c;
c = a+b;
cout << c << "\n";
string message;
message = "peace on earth";
cout << message << "\n";
return 0;
}
32
Example
#include <iostream>
using namespace std;
int gVar1; // Global variables,
int gVar2 = 2; // explicit initialization
int main()
{
char ch = 'A';
cout << "Value of gVar1: " << gVar1 << endl;
cout << "Value of gVar2: " << gVar2 << endl;
cout << "Character in ch: " << ch << endl;
int sum, number = 3; // Local variables
sum = number + 5;
cout << "Value of sum: " << sum << endl;
return 0;
}
33
Constant Objects
The const keyword is used to create a “read only’
object.
As an object of this type is constant it cannot be
modified at a later stage and must be initialized
during its definition.
const double pi = 3.1415947;

34
Volatile Objects
The keyword volatile creates variables that can be
modified not only by the program but also by other
programs and external events.
volatile unsigned long clock_ticks;

It is also possible to combine the keywords const and


volatile when declaring a variable.
volatile const unsigned time_to_live;

35
Example
// Circumference and area of a circle with radius 2.5
#include <iostream>
using namespace std;
const double pi = 3.141593;
int main()
{
double area, circuit, radius = 1.5;
area = pi * radius * radius;
circuit = 2 * pi * radius;
cout << "\nTo Evaluate a Circle\n" << endl;
cout << "Radius: " << radius << endl
<< "Circumference: " << circuit << endl
<< "Area: " << area << endl;
return 0;
}
36
Exercise 1
Write a C++ program that outputs the following text
on screen:
Today is the best day of my life!
Oh yes,
What a happy day!

Use the manipulator endl where appropriate.

37
Exercise 2
The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you should have understood '
<< " the basic of "C++." << endl.
return 0
)

Resolve the errors and run the program to test your


changes!
38
References
• Kirch-Prinz, U. and Prinz, P., (2002), A Complete
Guide to Programming in C++
• Lafore, R., (2002), Object-Oriented Programming in
C++
• Pozrikidis, C., (2007), Introduction to C++
Programming and Graphics

39

You might also like