Lets learn The C++ Programming Language
C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.
What is C++?
“C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.”
In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Srroutrup in 1979.
Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today.
Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.
“Hello World!”
Your first C++ program will be a “Hello World!” program.
You might have noticed “Hello World!” being the first program while starting out with any programming language. This is because:
- It is a standard check to see whether everything is working fine or not.
- There will be very less code to start with.
- The less code makes it intuitive for the beginners to get acquainted with the language.
- The code is enough to learn the basic syntax and semantics of the language.
So, let’s get coding.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
The program prints
Hello World!
in the output screen.How the program works?
Now, let’s dissect the above code. The code is divided into six major parts:
- #include <iostream>
- using namespace std
- ;
- int main() { }
- cout << “Hello World!”;
- return 0;
- What is #include <iostream>?
If you’ve already written code in C language before, you might seen this line of code before. If you haven’t, don’t worry we’ll cover it now.
This statement includes the header file into the application so that you are able to use the operations included in them. Also, you can create your own header files and include them in your program using the#include
.
What is iostream?iostream
is what you call the header file. It is a standard C++ input/output library file.
It comes packaged with the compiler/IDE and contain mechanisms to get the information from the user and print same or added information to a file, screen or any other media.The#include
iostream file, into the program. This ensures that now you’re able to use the operations,iostream
operations (like: taking input from user, displaying output on the screen), in the program. - What is using namespace std;”?
The statement is intuitive in itself, you are “using” the “namespace” “std” in your file.
We use thenamespace std
to make it easier to reference operations included in that namespace.
If we hadn’t used the namespace, we’d have written std::cout instead of cout. This tells the compiler that every cout is actually std::cout.
What’s a namespace?
It’s a region where your code resides. It limits or expands the scope of your code to one or more files.
Why do you use namespace?
Like two persons can have the same name, variables and functions in C++ can have same names as well. The use of namespace is to avoid the confusion of which variables/functions you are referencing to.
What is std?std
is a standard namespace used in C++. - Semicolon ”;”
Ask any C++ programmer and they will tell you at least one horror story related to the semicolon ; .
The semicolon is a terminal. It terminates a statement. When missed or incorrectly used, it will cause a lot of issues. - int main() { }
As the name suggests, it is the main function of the program. The code inside { } is called the body and is executed first when you run your C++ program.
It is one code that is mandatory in a C++ program. If you just have this line of code alone, your program will be valid. - cout << “Hello World!”;
This statement prints “Hello World!” onto the output screen.
Thecout
is an object of standard output stream. What this means is, it outputs/prints the data after << , i.e.Hello World!
into a stream (in this case, the output screen).
What is a stream?
Stream is basically a sequence of objects, usually bytes. It can describe files, input/output terminal, sockets, etc.
What is <<?<<
is the insertion operator used to write formatted data into the stream. - What is return 0;?
This statement returns 0 ‘zero’.
This is called a return statement. It isn’t mandatory to return anything from the main() function but is rather a convention. If not return, the compiler returns a status automatically.
Why zero in return statement?
It denotes Exit status of the application that basically the tells system “The program worked fine.”
Basic Simple C++ Programs Based On Simple Input and Output.
#include <iostream> using namespace std;
int main()
{
cout << "Hello DIT Students";
return
0;
}
|
#include <iostream> using namespace std;
int main()
{
cout << "\n\n \t\t Hello DIT
Students";
return
0;
}
|
#include <iostream> using namespace std;
int main()
{
cout << "Hello DIT Students"; cout <<
“\n \n We are IBAains”;
return 0;
}
|
#include <iostream> using namespace std;
int main()
{
cout << "Hello DIT Students"<<endl; cout <<
“\t\t\t We are IBAains”;
return 0;
}
|
#include <iostream> using namespace std;
int main()
{
int a =100; int b = 200;
cout << "We are Initialized variables";
cout<<”a = “<<a<<endl; cout<<”b = “<<b<<endl;
cout
<< “The Sum of the numbers is
=”<<a+b;
return
0;
}
|
#include <iostream> using namespace std;
int main()
{
int a,b,c;
cout << "Input First Number"<<endl; cin>>a;
cout << "Input Second Number"<<endl; cin>>b;
cout << "Input Third Number"<<endl; cin>>c;
cout<<”The result is =
”<<a+b*c;
return
0;
}
|
#include <iostream> using namespace std;
int main()
{
char a =’Z’; char b = ‘D’;
cout <<
"We are Initialized variables";
cout<<”a =
“<<a<<endl;
cout<<”b = “<<b<<endl;
return 0;
}
|
#include <iostream> using namespace std;
int main()
{ char a; char b;
cout
<< "Enter any Character";
cin>>a;
cout
<< "Enter Another Character"; cin>>b;
return 0;
}
|
#include <iostream> using namespace std;
int main()
{
int a =5;
cout
<< "I am an integer my value is 5"; if(a>5){
cout<<”My Name is Ali ”<<endl; else
cout<<”My
Name is Aslam”<<endl;
}
return 0;
}
|
#include <iostream> using namespace std;
int main()
{ int a;
cout <<
"I am an integer give me value";
cin>>a; if(a>0){
cout<<”The number is
positive”<<endl; else
cout<<”The Number is
Negative”<<endl;
}
return 0;
}
|
#include <iostream> using namespace std;
int main()
{
int a =5;
cout << "I am an integer my
value is 5";
for(int i=1;i<=a;i++){
cout<<i<<” * “<<a<” =
“<<i*a<<endl;
}
return 0;
}
|
#include <iostream> using namespace std;
int main()
{ int a;
cout <<
"Enter an integer for its table";
cin>>a;
for(int i=1;i<=a;i++){
cout<<i<<” * “<<a<” =
“<<i*a<<endl;
}
return 0;
}
|
Nice peice of work really worth admiring
ReplyDeleteThankss
DeleteQuiet meaningful
ReplyDeleteITS very useful development for us for all students who those are learning c×× and its your best invention for students Mr suhail Ahmed many many congratulations to you May Allah Almighty give you more success in your life thanks
ReplyDeleteGreat dear...
ReplyDelete