Getting started with C++ (Basic Tutorial)

If you want to learn C++, I truly recommend for you , to read these articles first:

  1. Getting started with C [1]
  2. Getting started with C [2]
  3. Getting started with C [3]
  4. Getting started with C [4]

After you will read this you’ll know the basic things about C language, and you will see that C++ is not so different.  In the first two examples, I will make a comparison between languages​​, so that you can see what are the differences. So, if you are ready to learn C++, let’s see the first example.

Example 1

C language

#include "stdafx.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	int a = 0, b = 0;

	printf("a:");
	scanf("%d", &a);

	printf("b:");
	scanf("%d", &b);

	printf("%d + %d = %d", a,b, a+b);

	getchar();
}

C++ language

#include "stdafx.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	int a = 0, b = 0;

	cout<<"a:";
	cin>>a;

	cout<<"b:";
	cin>>b;

	cout<<a<<"+"<<b<<"="<<a+b<<endl;

	getchar();

}

As you can see ptinf() method is replaced by cout method and scanf() is replaced by cinmethod. This is the biggest difference here. If you read my previous tutorials about C language this is very easy to understand, I don’t think this first example, can bring problems. Ready for the second example?

Example 2

C language

#include <iostream>
#include <stdio.h>
#include "stdafx.h"

using namespace std;

void read(int l,int c, char mat[][10])
{
    for(int i = 0;i < l;i++)
    {
        for(int j = 0 ;j < c;j++)
        {
            printf("mat[%d][%d]=", i, j);
            scanf("%s", &mat[i][j]);
        }

    }
}

void display(int l,int c, char mat[][10])
{
     for(int i = 0;i < l;i++)
    {

        for(int j = 0 ;j < c;j++)
        {
            printf("\\t%c",mat[i][j]);
        }
        printf("\\n");

    }
}

void capital(int l,int c, char mat[][10])
{
     for(int i = 0;i < l;i++)
     {
        for(int j = 0 ;j < c;j++)
        {
            if(mat[i][j] >= 'a' && mat[i][j] <= 'z')
            {
                mat[i][j] -= 32;
            }

        }

    }

}

int main()
{
    int l, c;
    char mat[10][10];

    printf("Enter the number of lines:"); scanf("%d", &l);
    printf("Enter the number of columns:"); scanf("%d", &c);

	read(l,c,mat);
    printf("Matrix is:\\n");
	display(l,c,mat);
	capital(l,c,mat);
    printf("Uppercase matrix is:\\n");
	display(l,c,mat);

	getchar();
	getchar();

}

C++ language

#include <iostream>
#include <stdio.h>

using namespace std;

void read(int l,int c, char mat[][10])
{
    for(int i = 0;i < l;i++)
    {
        for(int j = 0 ;j < c;j++)
        {

			cout<<"mat["<<i<<"]["<<j<<"]:";
			cin>>mat[i][j];
        }

    }
}

void display(int l,int c, char mat[][10])
{
     for(int i = 0;i < l;i++)
    {

        for(int j = 0 ;j < c;j++)
        {
            cout<<"\t";
			cout<<mat[i][j];
        }
       cout<<endl;

    }
}

void capital(int l,int c, char mat[][10])
{
     for(int i = 0;i < l;i++)
     {
        for(int j = 0 ;j < c;j++)
        {
            if(mat[i][j] >= 'a' && mat[i][j] <= 'z')
            {
                mat[i][j] -= 32;
            }

        }

    }

}

int main()
{
    int l, c;
    char mat[10][10];

    cout<<"Enter the number of lines:";
	cin>>l;
    cout<<"Enter the number of columns:";
	cin>>c;

	read(l,c,mat);
    cout<<"Matrix is:\n";
	display(l,c,mat);
	capital(l,c,mat);
    cout<<"Uppercase matrix is:\n";
	display(l,c,mat);

	getchar();
	getchar();

}

C++ language

Actually, you can see that is not such a big difference between C Language and C++ language. This is a very simple example that I have done, in the beginning, you set the numbers of the rows and the column, then fill your matrix with letters after that a matrix is generated with the uppercase letters. In the end, I will show you a very simple but effective example – > How to solve a second-degree equation.

Example 3

C++ language


#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	float a,b,c,delta,x1,x2;

	cout<<"X^2 (a) = ";
	cin>>a;
	cout<<endl;

	cout<<"X (b) = ";
	cin>>b;
	cout<<endl;

	cout<<"(c) = ";
	cin>>c;
	cout<<endl;

	if(a == 0 && b == 0 && c == 0)
		cout<<"Indeterminate Equation"<<endl;

	if(a == 0 && b == 0)
		cout<<"Impossible equation"<<endl<<endl;

	if(a == 0)
		cout<<"Equation of degree I"<<endl;

	delta = (b * b) - (4 * a * c);

	if(delta > 0)
	{
		x1 = (-b + sqrt(delta)) / (2 *a);
		x2 = (-b - sqrt(delta)) / (2 *a);

		cout<<"x1:"<<x1<<endl;
		cout<<"x2:"<<x2<<endl;
	}
	else
	{
		cout<<"Complex roots"<<endl;
	}

	return 0;
}

This is really a very easy example, and is very simple to understand what is going on here. Please, read my previous tutorials about C language because is very important to know the basic things about C language first, and after that try to understand this tutorial. For those how had already read my previous tutorials, I hope this lesson it will be helpful, and of course came again and read my next tutorials. Do not forget to write me, you can ask me any kind of question, and I will be back to you, as soon as I can.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress SEO