The Anatomy of C++ Program (Day 2) Week 1

 The Anatomy of C++ Program:


C++ programs consist of classes, variables, functions/Methods and other component

parts.We must see a complete working program.

Today, you will learn

• The parts of a C++ program

• How the parts work together

• What a function is and what it does


1. Simple Program

#include<iostream>

using namespace std;

int main()

{

cout<<"Hello Prem Singh Padya";

return 0;

}

**********************

2. Brief Look on cout :

Analysis: iostream library has to be included in the program to use cout and related functions.


3. Using the Standard Namespace :

1: //Use standard Namespace to avoid conflicts

2: #include <iostream>

3: int main()

4: {

5: using std::cout;

6: using std::endl;

7:

8: cout << “Hello this is Prem Singh Padya.\n”;

9: return 0;

10: }

Analysis:


First way:

  The only difference between Listing 2.3 and Listing 2.2 is that on lines 5 and 6, additional statements inform the compiler that two objects from the standard library will be used. This is done with the keyword using. After this has been done, you no longer need to qualify the cout and endl objects.

Second Way:

use std::cout<<“Hello this is Prem Singh Padya.\n”;

which says the use of cout object is from standard library that is being used.


3. Commenting your Program:

I want to be very clear about ehy it is happening with a tag in the program which hepls the learners to really understand and follow well for which we need to Comment the syntax information by using commenting .

Types of Comments:

Single Line Comment : 

cout<<"Prem Singh Padya"; //Prem Singh Padya will be Displayed//

Multiple Lines Comment:

Starts with /* and ends with */

/* this is a comment and it extends until the closing star-slash comment mark */

4. Functions:

main() is a function, it is an unusual one. To be useful, a function must be

called, or invoked, during the course of your program. main() is invoked by the operating system.

A program is executed line-by-line in the order it appears in your source code until a

function is reached. Then, the program branches off to execute the function. When the

function finishes, it returns control to the line of code immediately following the call to

the function.

A good analogy for this is sharpening your pencil. If you are drawing a picture and your

pencil point breaks, you might stop drawing, go sharpen the pencil, and then return to

what you were doing. When a program needs a service performed, it can call a function

to perform the service and then pick up where it left off when the function is finished

running.

int main()

 {

 std::cout << “In main\n” ;

 XYZFunction();

 std::cout << “Back in main\n”;

 return 0;

 }

*************************

int main()

{

std::cout<<"Exectes main Function";

int add()

{

int a,b;

cout<<"Enter a,b";

cin>>a;

cin>>b;

cout<<"Result: Sum of A+B "<<a+b;

};

cout<<"After Executing Branch (Add function then comes back to main Block of Statements and executes the other syntax statements)";

return 0;

}

5. Using Functions :

Functions consist of a header and a body. The header consists, in turn, of the return type, the function name, and the parameters to that function. The parameters to a function enable values to be passed into the function. Thus, if the function were to add two numbers, the numbers would be the parameters to the function. Here’s an example of a typical function header that declares a function named Sum that receives two integer values

(first and second) and also returns an integer value:

int Sum( int first, int second)

A parameter is a declaration of what type of value will be passed in; the actual value passed in when the function is called is referred to as an argument. Many programmers use the terms parameters and arguments as synonyms. Others are careful about the technical distinction. The distinction between these two terms is not critical to your programming C++, so you shouldn’t worry if the words get interchanged. The body of a function consists of an opening brace, zero or more statements, and a closing brace. The statements constitute the workings of the function.



Simple Add() Function Demonstration : 


1: #include <iostream>

2: int Add (int first, int second)

3: {

4: std::cout << “In Add(), received “ << first << “ and “ << second << “\n”;

5: return (first + second);

6: }

7: ;

8: int main()

9: {

10: using std::cout;

11: using std::cin;

12:

13:

14: cout << “I’m in main() Now!\n”;

15: int a, b, c;

16: cout << “Enter two numbers: “;

17: cin >> a;

18: cin >> b;

19: cout << “\nCalling Add()\n”;

20: c=Add(a,b);

21: cout << “\nBack in main().\n”;

22: cout << “c was set to “ << c;

23: cout << “\nExiting...\n\n”;

24: return 0;

25: }

Output: I am in main() then calling Add() function then again back to main() then exiting...


Analysis:

The function Add() is defined on line 2. It takes two integer parameters and returns an integer value. 

1. The program itself begins on line 8. 

2. The program prompts the user for two numbers (line 16).

3. The user types each number, separated by a space, and then presses the Enter key. 

4. The numbers the user enters are placed in the variables a and b on lines 17 and 18. 

5. On line 20, the main() function passes the two numbers typed in by the user as arguments to the Add() function. 

6. Processing branches to the Add() function, which starts on line 2. The values from a and

b are received as parameters first and second, respectively. 

7. These values are printed and then added. 

8. The result of adding the two numbers is returned on line 5, at which point the function returns to the function that called it—main(), in this case.

On lines 17 and 18, the cin object is used to obtain a number for the variables a and b.

Throughout the rest of the program, cout is used to write to the console. 

You will know more about variables soon in the next week.


Method Vs Functions :

Another name for function is a Method. More common term is the term Method. Method is simply another term for functions that are part of a class.




Link : DAY 3

WEEK 1 (Day 3)

Working with Variables and Constants

Comments