- Basic Structure Of C++ Program Ppt
- C Programming Ppt
- Structure Of C++ Program
- Structure Of C++ Program Ppt Presentations
Data Structure and Algorithms Lecture 1.PPT. Any C program is consists of 6 main sections. Below you will find a brief explanation of each of them. Pictorial Representation of Basic Struture of C Programming Basic Structure of C Program. Documentation Section; Link Section; Definition Section; Global Declaration Section; main Function Section; Subprogram Section.
This tutorial describes the program structure of the C++ program.
- Documentation
- Preprocessor Statements
- Global Declarations
- The main() function
- Local Declarations
- Program Statements & Expressions
- User Defined Functions
Let's begin with a simple C++ program code.
C++ Program which Outputs a Line of Text
Program Output:
The above example has been used to print text on the screen.
Let's look into various parts of the above C++ program.
/* Comments */ | Comments are a way of explaining what makes a program. The compiler ignores comments and used by others to understand the code. or This is a comment block, which is ignored by the compiler. Comment can be used anywhere in the program to add info about program or code block, which will be helpful for developers to easily understand the existing code in the future. |
#include <iostream> | This is a preprocessor directive. It tells the preprocessor to include the contents of iostream header file in the program before compilation. This file is required for input-output statements. |
int/void | int/void is a return value, which will be explained in a while. |
main() | The main() is the main function where program execution begins. Every C++ program must contain only one main function. or This is a main function, which is the default entry point for every C++ program and the void in front of it indicates that it does not return a value. |
Braces | Two curly brackets '{…}' are used to group all statements. or Curly braces which shows how much the main() function has its scope. |
std::cout<<'This is my first C++ Program';
The above line is a statement in C++. A statement must always terminate with a semicolon; otherwise, it causes a syntax error. This statement introduces two new features of C++ language, cout and << operator.
You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character, and a series of characters that are grouped is called a string. Strings must always be put between inverted commas.
We used std:: before cout. This is required when we use #include <iostream >.
It specifies that we are using a name cout which belongs to namespace std. A namespace is a new concept introduced by ANSI C++ which defines the scope of identifiers which are used in the program. std is the namespace where C++ standard libraries are defined.
Operator << is the insertion stream operator. It sends contents of the variable on its right to the object on its left. In our case, the right operand is the string 'This is my first c++ Program' and left operand is a cout object. So it sends the string to the cout object, and cout object then displays it on the output screen.
namespace
If you specify using namespace std then you don't have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.
Return Statement
return 0 | At the end of the main function returns value 0. |
- int main() instead of void main()
- After you import your headers you required to use using namespace std;
- There is no header file like iostream.h, you only required to use this as #include <iostream>
void main() and iostream.h is only valid for Turbo C++.
C provides us the feature of nesting one structure within another structure by using which, complex data types are created. For example, we may need to store the address of an entity employee in a structure. The attribute address may also have the subparts as street number, city, state, and pin code. Hence, to store the address of the employee, we need to store the address of the employee into a separate structure and nest the structure address into the structure employee. Consider the following program.
Output
The structure can be nested in the following ways.
- By separate structure
- By Embedded structure
1) Separate structure
Here, we create two structures, but the dependent structure should be used inside the main structure as a member. Consider the following example.
As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure. In this way, we can use Date structure in many structures.
2) Embedded structure
The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures. Consider the following example.
Basic Structure Of C++ Program Ppt
Accessing Nested Structure
We can access the member of the nested structure by Outer_Structure.Nested_Structure.member as given below:
C Nested Structure example
Let's see a simple example of the nested structure in C language.
C Programming Ppt
Structure Of C++ Program
Output:
Passing structure to function
Just like other variables, a structure can also be passed to a function. We may pass the structure members into the function or pass the structure variable at once. Consider the following example to pass the structure variable employee to a function display() which is used to display the details of an employee.