Chapter 3: Language Structures of OOP: Basic Structure of C++ Program
Chapter 3: Language Structures of OOP: Basic Structure of C++ Program
OOP
Basic Structure of C++ Program
As C++ is a programming language so it follows a predefined structure. The program
is divided into many sections, it is important to know the need of every section. The
easiest way to understand the basic structure of c++ program is by writing a program.
The basic C++ program is as follows:
//simple c++ program
int main()
int a=10,b=34;
cout<<"hello world";
cout<<a<<b;
The basic structure of c++ program mentioned above can be divided into following
sections:
Documentation Section : This section comprises of comments. As the name
suggests, this section is used to improve the readability and understanding of the
program.// (Double Slash) represents comments in C++ program. Comments can
be of single line or multiple lines. Double Slash comments are used to represent
single line comments. For multiple line comment, you can begin with /* and end
with */. For example :
/* Text line number 1
Text line number 2
Text line number 3 */
In the above C++ program //simple c++ program represents single line comment.
Linking and Directives Section : The program written above begins with
#include<iostream>. <iostream> represents header file which includes the
functionalities of predefined functions. In linking section, the compiler in-built
functions such as cout<<, cin>> etc are linked with INCLUDE subdirectory‘s header
file <iostream>. The ‗#‘ symbols tells about ―address to‖ or ―link to‖. Iostream is
input/output stream which includes declarations of standard input-output library in c++.
main() Section : This is the section in which the program coding is written.
Basically, it acts as a container for c++ program. The execution of the c++ program
begins with main() function and it is independent of the location of main() function in
the program. main() is a function as represented by parenthesis ―()‖. This is because it
is a function declaration. The body of the main() function can be found right after these
parenthesis, the body is enclosed in braces ―{ }‖.
Body of main() Section : The body of the main() function begins with ―{―.
o Local Variable Declaration : In this the variables which are used in the body o
f the main() functions are declared. These are called the local variables as their
scope is limited within the main() function only, unless they are declared
globally outside the main() function. ‖ int a=10, b=34;‖ in the above program
represents local variables
o return 0; in the above program causes the function to finish and 0 represents
that function has been executed with zero errors. This is considered as most
usual way to end a C++ program.
2 which
3 explains
4 the structure of c++ program */
7 float f=10.2,j=4.5;
8 int main()
9 {
10 int a=10,b=34;
12 cout<<"hello world";
cout<<a<<b;
13
return 0; // returning no errors
14
}
15
/* basic example
which
explains
the structure of c++ program */ .
float f=10.2, j=4.5; are global variables which are declared outside the main()
function.
The statements written in the above mentioned programs can be written in a single line
for example :
int main(){int a=10,b=34; cout<<"simple c++ program \n";
cout<<"hello world"; cout<<a<<b; return 0;}
mathPrimer.h
// mathPrimer.h
// ChapterII.HeaderSourceFiles
//
// Created by Vlad Isan on 20/04/2013.
// Copyright (c) 2013 INNERBYTE SOLUTIONS LTD. All rights
reserved.
//
#ifndef mathPrimer_h
#define mathPrimer_h
#endif
First, let‘s take a look a little at the preprocessor directives in this file. You already
know what #define is, as we have covered it a little when talking about variables and
constants. Let‘s talk a little about the conditional compilation, #ifdef, #ifndef
and #endif.
The conditional compilation preprocessor directives, tell your compiler what should
be compiled or not and under what conditions.
The #ifdef preprocessor directive basically checks if something was previously defined
using the #define preprocessor directive. If this condition is met, then the code between
the #ifdef and the corresponding #endif is compiled, otherwise it is ignored by the
compiler.
The same goes for #ifndef, and, as you can already imagine, it is the complete opposite
of #ifdef, and it allows the compiler to check whether a name has not been defined
with #define.
So, why are we using these preprocessor directives in our header file? These
preprocessor directives in the header files are called header guards, and helps us to
avoid including the same declaration in multiple times.
This works by skipping the entire contents of the header file if it was already included
in some other place. In our example, when you first include our header
file, mathPrimer.h, the #ifndef condition is met, because we haven‘t defined the
name mathPrime_h by using the #define preprocessor directive until now. So, the
condition is met and everything inside the header file will be compiled, and most
importantly, the mathPrimer_h will be defined as well, by using the #define
mathPrimer_h right after the #ifndef. Now, when you include the header file a
second time, the #ifndef mathPrimer_h condition will not be met, because we have
already defined that name when we included the header file in some other place.
By using these header guards we are avoiding the complaint we could get from the
compiler when declaring the header contents twice, or even multiple times.
OK, now let‘s look at what is declared inside our mathPrimer.h header file. We have
two function declarations, add and subtract, both having two integer parameters and
an integer return type.
We have to also define our functions. Let‘s create another file called mathPrimer.cpp.
This file will contain the function definitions:
mathPrimer.cpp
//
// mathPrimer.cpp
// ChapterII.HeaderSourceFiles
//
// Created by Vlad Isan on 20/04/2013.
// Copyright (c) 2013 INNERBYTE SOLUTIONS LTD. All rights
reserved.
#include "mathPrimer.h"
#include <iostream>
#include "mathPrimer.h" /* including our header file */
int main()
{
return 0;
}
As you can see we are including our header file in here as well, so we can use
the functions declared in it, add and subtract. If you were to remove the #include
“mathPrimer.h” from here, you will see that you cannot compile the code, because the
compiler would not know anything about those functions, and it will complain about
―Undeclared identifiers―.
An important thing to note about header files is to never include variables in them,
unless they are constants. Header files should only be used for declarations. Also, you
should never include the definition for a function in the header file because it will
change the whole scope of having a header file, and it will make it hard to read.
Also, you should always split the parts of your code into separate header and source
files grouped by a certain criteria or functionality, because when only needing a part of
it, you do not need to include all of the declarations that reside in your program. As in
our example, we have called our header file mathPrime.h, as it will only contain basic
math functions. If we want to make some other helper functions, for example, for
printing to the screen and retrieving user input, we should make another pair
of header/source files, that we should only include when needed.
Where tag is optional and only needs to be present if no variable is present. The
members are variables declared as any C supported data type or composed data type. A
structure is a set of values that can be referenced collectively through a variable name.
The components of a structure are referred to as members of a structure. A structure
differs from an array in that members can be of different data types. A structure is
defined by creating a template. A structure template does not occupy any memory
space and does not have an address, it is simply a description of a new data type. The
name of the structure is the tag. The tag is optional when a variable is present and the
variable is optional when the tag is present. Each member-declaration has the form
A Class specified by the keyword class, is a user defined type that contains both data
members and member functions...
A union is a user-defined data or class type that, at any given time, contains only one
object from its list of members (although that object can be an array or a class type).
Program Example
Let's look at an example of how to declare an integer variable in the C++ language
and use it.
int main()
{
int age;
age = 10;
cout<<"The data in variable age is %d yrs.\n"<< age;
return 0;
}
This C program would print ―The data in variable age is 10 yrs."
int main()
{
String name;
int age;
cout<<"Enter your name plz:\n";
cin>>name;
cout<<"Enter your age plz:\n";
cin>>age;
return 0;
}
This C program would print "Your Name is KIM and your age is 10 yrs".
Scope of Variables
All the variables have their area of functioning, and out of that boundary they don't hold
their value, this boundary is called scope of the variable. For most of the cases its
between the curly braces,in which variable is declared that a variable exists, not outside
it. We will study the storage classes later, but as of now, we can broadly divide
variables into two main types,
Global Variables
Local variables
Global variables
Global variables are those, which ar once declared and can be used throughout the
lifetime of the program by any class or any function. They must be declared outside the
main() function. If only declared, they can be assigned different values at different
time in program lifetime. But even if they are declared and initialized at the same time
outside the main() function, then also they can be assigned any value at any point in the
program.
Example : Only declared, not initialized
include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;
}
Local Variables
Local variables are the variables which exist only between the curly braces, in which its
declared. Outside that they are unavailable and leads to compile time error.
Example :
include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
Both Type conversion and Type casting in C++ are used to convert one predefined type
to another type.
Type Conversion is the process of converting one predefined type into another type.
and type Casting is the converting one predefined type into another type forcefully.
Need of Type Conversion and Type Casting in C++
An Expression is composed of one or more operations and operands. Operands consists
of constants and variables. Constants and expressions of different types are mixed
together in an expression. so they are converted to same type or says that a conversion
is necessary to convert different types into same type.
Type Compatibility
In an assignment statement, the types of right types and left side of an assignment
should be compatible, so that conversion can take place. For example,
ch=x; (where ch is of char data type and x is of integer data type)
How and Why Information is loose ?
what is Big Endian ?? ⇒ refer to the link Click here
since the memory representation in Big-Endian, Let
int x=1417;
ch=x;
now, x will be 00000101 10001001 in binary.