Today in this tutorial, we will learn how to program a program in C++. I actually wanted to post this at first. But, I forgot to do so. So I am trying to post it by changing the date. I hope, you will find it at the right time.
There is a lot of confusion among people when they are about to start programming in C++.
Some of the common issues are, that they think, is C and C++ almost same?
Whose tutorials to follow to learn C++? Which is better for tutorials youtube or blogs?
After how much time will I be able to program a software? (very few think so,though!) And so on.
So, I am answering your issues from my experience. C++ is same as C and it is true. You just edit some syntax then the program is almost same, until you don’t use classes in your program.
But, once you start using class in programs, then things change. Class is a data type like structure in C.
And the another issue is about how to learn it? I am giving some definite steps to learn C++ from online source.
The online is filled with tons of knowledgeable information.
Though not all of them are equally useful, but most of them are. It is because, till you are not an expert in that subject, then you won’t find any success online.
To be successful in online youtube and blogging, you will need to do something that you are good at. So, my guess is that 8 in 10 online materials available are useful while 2 may be a total garbage.
And the selection of good and garbage is upto you.
So, how do you learn C++?
I will advise you strongly to follow my tutorials. It is not because it is my blog, but because I am sharing this knowledge of C++ in a way that is taught in engineering colleges.
I am presenting the classes of engineering, that you used to dream into an online world for free(though there are advertisements, you don’t pay me directly).
So, these tutorials will cover almost all things that are required to be a C++ engineer. So, be ready on your journey to master C++ with me.
I know that learning this only will only help you just to clear your exams and nothing else than that. So, in the future I will be doing some interesting and useful things, once I finish the study course of C++.
After we finish this course, we will decide what to do further to extend our knowledge of C++.
Likewise, the concepts understanding is important in programming.
I have seen in many blogs that they just put down the source code. I don’t do that. I will explain each and every line of program. I will explain how the program flows. I will also explain how to write the programs.
So, if we study programming in this way, then after learning one program, there will be no need to learn another program, there will be no need to memorize another program.
You will be able to see a pattern in programming and will start generating solutions of hard problems yourself. You will start creating some programs that seem harder for others with little to no effort after finishing reading these blogs about C++.
So, while following my blog tutorials, take a note copy. First read the blog carefully.
For eg-: you are reading about constructors. Then type the source code in your code::blocks.
Do some tweaks in it, see what happens. You will remember the source code if you write it in your copy. And after doing it, read the blog again for understanding how the program works.
Finally, your note copy will contain a source code of the program and definition of what you are doing. Next step is to revise it whenever you wish. Then, if there is homework, solve it and post in comment section.
There are some people who understand better through videos, than blogs. In the future, if the demand of people who want a video course on this becomes high, I may have to see…..
But, for time, I am working full hard to create a great C++ blog. That is my one and only motive for the time being.
Remember that we are using Code::Blocks IDE and I assume you know how to install codeblocks. At first, even installing codeblocks may be terrible for someone. But, later on, you will start to catch the technical things.
Let us start the C++ programming by writing a program using OOP to find the simple interest.
The program is listed as below-:
using namespace std;
cout<<"enter the principal,time and rate";
Its output will come as follows-:
So, let us try to understand what is going inside the program.
You may think if you can write int main() before defining functions. Yes, you can but it will make the code lengthier as you will have to write declaration first then define. And, if you don’t do that, the program will not run. SO, we will write the main function later.
It all started with the main function. I am going to repeat this a lot of times in my blog.
Then, it will make an object of interest I1 as interest I1. It is the syntax of creating objects of a class in C++.
Where is class here?
Class interest is a class. It is like a structure in C. We will discuss those things in detail, later on.
After creating object I1, that object will go and invoke the demo() function which will calculate interest as I=PTR/100.
So, this program runs like this!
I hope this one is very much clear to you.
Now, let’s go back to another problem.
This program is also about using the concept of OOP,but in a slightly different way. Here, we are going to calculate the distance between two points using the concept of OOP. SO, let’s see a program in C++ to understand the concept of OOP in detail.
using namespace std;
void calculate(distances d1,distances d2)
cout<<"\nThe required distance is\n"<<sqrt((d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y));
cout<<"\nenter the values of x and y\n";
cout<<"\nthen for y\n";
Its output will come like this-:
The above program will run like this. First everything will start from the int main function. Then, it will create objects d1,d2,d3 of class distances. Then, we will call the read_distance function which takes the distance values input, as x and y for objects d1 and d2.
Then, we will invoke the calculate function using d3 and pass d1 and d2. Inside d1 and d2 there are x,y and x,y respectively.
Inside the calculate function, we will use the formula for calculating distance, which is precisely.
And we are assuming two objects d1 and d2 instead of assuming x1 and x2.
It is easier in C++ when we can identify the common between two objects. Here, what is the common between the two objects, d1 and d2. It is actually that we are taking the x and y values of d1 and d2. And, the common is x and y.
So, we need d1 and d2 rather than x1 and x2.
If we had choosen x1 and x2, then we will require y1 and y2 also, which will require more memory. And more memory in software development=guarenteed failure of softwares.