Вы находитесь на странице: 1из 18

INTRODUCTION TO

PROGRAMMING (C++)
SOME REMARKS ABOUT PROGRAMMING

• Programming is a core activity in the process of performing a task or solving


problems with the aid of a computer.
• Unfortunately, things are not (yet) that simple.
• We cannot use “natural language”
• We need programming languages
THE ORIGINS OF C++

• C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in early


1980’s, (it was based on the C Language developed by Dennis Ritchie).
• “++” is a syntactic construct used in C. It is used for increment operation.
• Thus, C++ means an increment or improvement to C.
THE ORIGINS OF C++ (Continued…)

• Why C?
• Compared to assembly language it is high-level, but it nevertheless includes many low-level
facilities to directly manipulate the computer’s memory.
• It is an excellent language for writing efficient “systems” programs.
• Why not C?
• For other types of programs, C code can be hard to understand.
• Prone to certain types of errors.
• Extra object-oriented facilities in C++ are partly included to overcome these shortcomings
ANSI/ISO C++

• ANSI – American National Standards Institution


• ISO- International Standards Organization
• The two both provide “official” and generally accepted standard definitions of many
programming languages.
THE C++ PROGRAMMING ENVIRONMENT

• The best way to learn a programming language is to try writing programs and test
them on a computer.
• To do this, we need several pieces of software:
• An editor with which to write and modify the C++ program components or source code.
• A compiler with which to convert the source code into machine instructions which can be
executed by the computer directly.
• A linking program with which to link the compiled program components with each other.
• A debugger to help diagnose problems
• All of these are included on our programming environment, the Turbo C++.
AN EXAMPLE OF C++ PROGRAM
//This is a sample comment
/*This is another sample comment*/
#include<iostream.h>
int main()
{
char name[20];
cin>>name;
cout<<“Hello World! My name is ”;
cout>>name;
return 0;
}
DISSECTING THE PROGRAM PARTS

• //This is a sample comment


• /*This is another sample comment*/
• These are what we call comments. It the compiler sees that a statement is preceded
by a double slash “//” or enclosed inside a slash and asterisk “/* . . . */”, it will disregard
the statement.
• Comments are used for several reasons.
DISSECTING THE PROGRAM PARTS

• #include<iostream.h>
• This statement is called include directive or header library.
• It tells the compiler and the linker that the program will need to be linked to a library routine
that handle inputs and outputs (cin and cout).
• The header file “iostream.h” contains the basic information about this library.
• We can include as many header files and we opt to use.
DISSECTING THE PROGRAM PARTS

• int main()
• This is the main function.
• The compiler will perform all tasks that are inside the main function.
• return 0;
• This is the return statement.
• This line means return the value 0 to the computer’s operating system to signal that
the program has completed successfully.
DISSECTING THE PROGRAM PARTS

• char name[20];
• This line is called the variable declaration or the declaration section.
• This signals the compiler that it should set aside enough memory to store the variable,
name[20] during the rest of the program execution.
• Variables should be declared before being used in the program.
IDENTIFIERS or VARIABLES

• As we have seen, C++ programs can be written using many English words.
• It is useful to think of words found in a program as being one of three types:
• Reserved Words – words such as if, int and else, which have a predefined meaning
that can’t be changed.
• Library Identifiers – These words are supplied default meanings by the programming
environment, and should only have their meanings changed if a programmer has
strong reasons for doing so. (e.g. cin, cout and sqrt).
• Programmer-supplied Identifiers – These words are “created” by the programmer,
and are typically variable names such as name, age, address
IDENTIFIERS or VARIABLES

• How do we define a valid identifier or variable?


• Identifiers cannot be any sequence of symbols.
• A valid identifier must start with a letter of the alphabet or an underscore (“_”)
• Must consist only of letters, digits, and underscores.
• How to declare a variable? (Syntax)
• <data_type> <variable_name>;
• e.g. int mynumber; or int mynumber = 50;
DATA TYPES

• Integers (int)
• Used to represent integers (whole numbers).
• Declaring a variable to be of type int signals to the compiler that it must associate enough
memory with the variable’s identifier to store an integer value.
• e.g. int age;
• Float (float) and Double (double)
• Used to represent real numbers.
• Treated almost exactly the same as integers except for the decimal places.
• Float stores up to 7 decimal places.
• Double stores up to 15-16 decimal places.
DATA TYPES

• Characters (char)
• Used to store character data.
• Char can only store single character data (could be a blank space).
• Can store multiple characters provided the size is declared.
• Perhaps the most common collection of characters is the ASCII character set.
• American Standard Code for Information Interchange
ASCII TABLE
DATA TYPES (Continued. . .)

• Constants (const)
• Constants are variables wherein the values cannot be changed all throughout the
program execution.
• Syntax: const <data_type> <variable_name> = <value>;
• e.g. const double PI = 3.141592635;
EXERCISE

• Create a simple program that displays the word “Hello World!”


• To add a twist, create a program that accepts inputs (First Name, Middle
Name, Last Name and Age) and display these with the format:

Hello World! My name is <firstname> <middlename> <lastname> and I am <age> years old!

Вам также может понравиться