4. Defined constants (#define)
symbolic constant
syntax : #define identifier replacement-text
This is a compiler directive used to define symbolic constant, it asks the compiler
to search the file for the identifier and replace the macro with the replacement text
specified.
The constant is MAX (usually
#define MAX 5 written in capital letters)
int main() { NO semicolon (;)
cout<< MAX;
...
The text defined
for (i=0; i<MAX; i++)
for the MAX is : 5
{ ... }
... IT IS NOT RECOMMENDED
} TO USE define 4
TCP1231 Computer Programming I
5. Definition of own types (typedef)
syntax : typedef type newtype
define a new user-defined data type
typedef int mytype;
int main ()
{ 2 3
mytype a=2,b=3;
cout << a << 't' << b << endl; 5 3
a=a + b;
cout << a << 't' << b << endl;
6 2
a++;
b--;
cout << a << 't' << b << endl; IT IS STRONGLY
RECOMMENDED
return 0;
} TO USE typedef
TCP1231 Computer Programming I 5
6. Constants (const)
A constant is any expression that has a fixed value. They can be
divided in Integer Numbers, Floating-Point Numbers, Characters
and Strings
const int width = 100;
const char tab = 't';
int main ()
{
cout << width << tab << width / 2;
100 50
return 0;
}
With the const prefix you can declare constants with a specific
type exactly as you would do with a variable
TCP1231 Computer Programming I 6
7. Enumerations (enum)
Enumerations (defines a set of constants) serve to create data
types to contain something different that is not limited to either
numerical or character constants nor to the constants true and
false. Its form is the following:
enum model_name { value1, value2, . . . } ;
enum colors {black, blue, green, red, yellow, white};
int main ()
{
colors mycolor = blue;
if (mycolor==black)
cout <<"Yes";
else
cout <<"No"; No
}
TCP1231 Computer Programming I 7
8. Enumerations example
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef enum Month{January,February,March,April,May};
main () {
Month mont;
int n;
cout<<"Enter the number of the month==>";
cin>>n;
mont =Month(n); Enter the number of the month==>4
switch(mont){
This month is May
case 0: cout<<"this month is January";break;
case 1: cout<<"this month is February";break;
case 2: cout<<"this month is March";break;
case 3: cout<<"this month is April";break;
case 4: cout<<"this month is May";break;
default: cout<<"this month is not in the list";
}
cout<<endl<<endl;
system("pause");
return 0;
}
TCP1231 Computer Programming I 8
9. Scope of variables
int x; Global variable
...
main(){
int k;
...
Local variable
...
{
int k;
...
Local
... variable
}
...
}
TCP1231 Computer Programming I 9
10. Scope of variables
All the variables that we are going to use must be previously declared. in C++
we can declare variables anywhere in the source code, even between two
executable sentences, and not only at the beginning of a block of instructions.
int i=1, j=1; Global variables can be referred to anywhere in the
float f; code, within any function, whenever it is after its
declaration.
int main ()
{ The scope of the local variables is limited to the code
int integer=3; level in which they are declared. If they are declared at
float flt; the beginning of a function (like in main) their scope is
cout << i << 't' << j << endl; the whole main function. In the example, this means that
if another function existed in addition to main(), the local
int j=3; variables declared in main could not be used in the other
function and vice versa.
cout << i << 't' << j << endl;
j++; 1 1
i++; 1 3
cout << i << 't' << j << endl; 2 4
}
TCP1231 Computer Programming I 10
11. Scope example
#include <iostream>
#include <stdlib.h>
using namespace std;
main () {
int j=4;
cout<<"First list of i value==>"; First list of i values==>0 1 2 3 4 5
for (int i=0;i<=5;i++) Second list of i values==>10 9 8 7 6 5 4 3 2 1 0
cout<<i<<" ";
cout<<endl;
cout<<"Second list of i values==>";
{
int j=10;
for(int i=j;i>=0;i--)
cout<<i<<" ";
}
cout<<endl<<endl;
system("pause");
return 0;
}
TCP1231 Computer Programming I 11
12. Structure (struct)
• A structure can be viewed as an object.
• A structure is a set of diverse types of data that may have
different lengths grouped together under a unique declaration.
Its form is the following:
model_name is a name for the model of the
struct model_name { structure type and the optional parameter.
type1 element1;
type2 element2; object_name (optional) is a valid identifier (or
type3 element3; identifiers) for structure object instantiations.
.. Within curly brackets { } they are the types
.. and their sub-identifiers corresponding to the
} object_name; elements that compose the structure.
TCP1231 Computer Programming I 12
13. #include <iostream>
#include <conio.h> A student has a name, age,
using namespace std; ID, and average
struct student{ cout << "n------ From data -----------n";
string name; cout << data.ID << 't' << data.avg << 't' << data.name << endl;
int age;
int ID; cout << "n------ From info ------------n";
float avg; info=data;
} info; cout << info.ID << 't' << info.avg << 't' << info.name << endl;
int main () cout << "n *****************************n";
{ info.avg++;
student data;
student test={"ABC", 19, 111, 5.5}; cout << "n------ From data -----------n";
cout << "Enter a student ID ==> "; cout << data.ID << 't' << data.avg << 't' << data.name << endl;
cin >> data.ID; cout << "n------ From info ------------n";
cin.ignore(); cout << info.ID << 't' << info.avg << 't' << info.name << endl;
cout << " the student name ==> "; cout << "n*****************************n";
getline(cin, data.name); cout << test.ID << 't' << test.avg << 't' << test.name << endl;
getch();
cout << "student average ==> "; return 0;
cin >> data.avg; }
TCP1231 Computer Programming I 13
14. Enter a student ID ==> 123
Enter the student name ==> amer
Enter the student average ==> 2.2
------ From data -----------
123 2.2 amer
------ From info ------------
123 2.2 amer
*****************************
------ From data -----------
123 2.2 amer
------ From info ------------
123 3.2 amer
*****************************
111 5.5 ABC
TCP1231 Computer Programming I 14