• 4 months ago
1. Skeleton of C++ Program
Transcript
00:00In the previous section, we have already learned how to download and install the compiler and
00:06the IDE required for developing C++ program.
00:10Hope you are ready with the setup.
00:11If you are not ready, I suggest you to go back to the previous section and watch the
00:16videos and get the environment ready on your PC.
00:21In this video, I am going to show you the skeleton or the basic programs of C++.
00:27In the following video, you will find a demo how to start a new project and how to develop
00:31the first program.
00:34First of all, let us start with the skeleton of a C++ program.
00:39This is a skeleton already I have kept it ready here.
00:43You have to start a new project.
00:44Let us say the project name is first and the program file is first.cpp.
00:49You have to start this one.
00:52Inside that this is what you have to return.
00:54Every C++ program basically looks like this.
00:58Let us understand what are these elements.
01:00I will start from this point.
01:02See this is the starting point of a C++ program main function.
01:07This is main and the round brackets are there so this is a function.
01:11This main function is the starting point of a C++ program.
01:15Then what is this int?
01:17This is called as return type of a function.
01:20We will be learning about the return types and all afterwards but right now as a formality
01:25you have to write it always and when you have return int there at this place then you have
01:30to write return 0 at the end of the main function.
01:36This is the body of main function that is the opening flower bracket and closing flower
01:41bracket.
01:42Now what all you have to write on?
01:44You have to write it here inside the body of main function.
01:50Then what does it mean by this hash include?
01:52I will explain it.
01:54First of all I will write something here inside the main function and show you.
01:59Here as a first program I want to give a welcome message so I will write hello world.
02:05I want this program to print on the screen hello world when I run the program.
02:12How to print something on the screen?
02:14For printing anything on the monitor that is on the screen from your C++ program there
02:20is a built in object available in C++ that is Cout.
02:26C means console out that is console out.
02:32Monitor is treated as a console that is output console then there is something that is keyboard
02:39that is useful for taking input.
02:42This is input console.
02:45It means for that there is Cin, Cout for monitor console out, Cin that is console in for keyboard
02:55that is taking any input from the keyboard.
02:58Let us use this Cout and print something on the monitor.
03:02As I said we will be printing hello world.
03:05We have to use this two angular brackets this is called as insertion operator.
03:13We have to use this one for printing anything then here inside double quotes I will say
03:20hello world.
03:22Whatever the message that you have to print you have to give it in double quotes.
03:27This is a string or a message this is given to Cout so it will be printed on the monitor.
03:35When you run the program hello world will appear here.
03:43Cout is an object used for printing anything on the monitor.
03:48If you see this double less than symbol, two less than symbols those are.
03:53On the keyboard you will find less than symbol hit it for two times.
03:57That is acting as an insertion operator.
04:00If you feel it, it looks like as if I am inserting this one inside Cout.
04:05Once I insert it, it will appear on a monitor.
04:09It gives the feeling that we are inserting something that is why it is called as insertion
04:13operator.
04:14This is how we can print.
04:17Where this Cout came from?
04:19We know that for every C++ program we should write down int main flower bracket close and
04:26return.
04:27We are writing here where this Cout came from.
04:30The Cout object as well as Cin object these are present inside this iostream header file.
04:39Actually this is a library.
04:42Library will contain the collection of built in objects or functions that a programmer
04:48can use and easily write the program.
04:51This is provided by C++ compiler when you install the compiler you will get the libraries.
04:57There are many libraries we will learn about them slowly.
05:00Whatever your requirement is depending on that you can include the library in your program.
05:05Everything from the library will be attached in your program and you can use it.
05:10Cout is present in this library iostream library.
05:14For including that one we include bytes name or bytes header file.
05:20In some compilers even you have to write on iostream.h.
05:25If this doesn't work in your compiler then you write iostream.h or else just iostream.
05:33Now I will remove these things and I will explain few more things about this one.
05:38This Cout as it is present there we cannot use it directly we have to write down namespace.
05:45I have to write std double colon this symbol you will find on the keyboard you hit it for
05:51two times so this is scope resolution colon and colon that is double colon this is called
05:56as scope resolution.
05:58So the correct method is write down std scope resolution then cout and whatever you want
06:04to give.
06:05This is how we use that cout object.
06:08Now this is a perfect program.
06:10If you run it you will get hello world on the screen.
06:15Now one more thing if you are writing a very lengthy program or bigger program and you
06:19have to use cout many times then instead of writing it like this there is another way
06:24of doing this using that cout as well as cin object.
06:29The other method is here we can write using namespace and this namespace name.
06:40So when we say that we are using this namespace so we don't have to say this one we can directly
06:46write cout.
06:47It means that we are using that namespace means whatever is there inside that namespace
06:52we are using it.
06:53Now what does it mean by namespace?
06:55See all the built in things available in this header file that is library are grouped under
07:00one name that is std.
07:03So for using that we have to say using namespace.
07:07Now more about namespace we will learn it in this course you will find a topic there
07:12is a video available it is a separate topic.
07:14So everything about the namespace is discussed there.
07:18Now writing std here is better than saying that we are using namespace because we are
07:25not using everything from the namespace we want just cout so writing std colon and cout
07:30is better but it is easy for the beginners to just write cout so I may be using cout
07:36many times.
07:37On the top I will be writing using namespace and here I will write cout just cout.
07:44So that's all is the first program.
07:46Let us quickly look at the things that we have learned.
07:49See we have learned that every C program will have a main function main function should
07:52return integer and as it is returning so here we should write down return 0 then inside
07:58this flower brackets that is body whatever you want you can write down here.
08:02So in my program I am just displaying it so for that cout is an object that is already
08:06available I am using it for printing something on the screen.
08:10This cout is present in the header file so I should include the header file and this
08:15header file is having this namespace so I should also use that namespace just including
08:20the header file is not sufficient you have to use that namespace inside that one.
08:24If you don't want to use it then you can write down std scope resolution here.
08:29The next video I will show you some simple programs of C++.

Recommended