Introduction to C++



The major problem B.Tech student faces during the time of interview is clear understanding of C++. Even if they have good knowledge of C and perform well in it, lack of C++ understanding can ruin their interview. They don't have time to learn C++ when companies start coming. So here I am trying to make their learning of C++ easy in very short time, assuming they have understanding of basic C concepts and syntax knowledge. This will also help you to answer question related to difference in C and C++

  1. C++ provides built-in objects for general input/output operations without using any format specifier (%d, %f in printf, scanf functions are not needed yay! )
    1. cin -- keyboard
    2. cout - monitor (standard messages)
    3. cerr -- monitor (error message)
  2. Such objects are available inside iostream.h header file
  3. Use special operators << (Extraction Operator) and >> (Insertion operator)
  4. Use file extension as .cpp

Let us understand these basics by few examples below

Program that takes name and age of a person as input and check whether the person is eligible to be vote or not

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<iostream.h> //Needed for cin and cout objects
#include<conio.h> // Needed for clrscr() function

void main()
{
    char name[51];
    int age;
    clrscr();

    cout << "Enter your name : "; // Note use of extraction operator.
    cin >> name;
    cout << "Age ";
    cin >> age; // %d is not needed like scanf

    if(age>=18)
        cout << "Dear " << name << " you can vote";
    else
        cout << "Dear " << name << " you cannot vote";
}


  1. C++ allows to create two or more functions with the same name. The functions must have different number of arguments or type of argument.(That's polymorphism one of the OOP feature, C++ is object oriented, whereas C is not )


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// A simple program to calculate area that show use of polymorphism with multiple area functions

#include<conio.h>
#include<iostream.h>

void area(int side)  // Parameter type integer
{
    cout << "Area of square is " << side*side << endl; // End of the line makes other output to start from next line
}

void area(double r)  // Parameter type double
{
    cout << "Area of circle is " << 3.14*r*r << endl;
}

void area(int l, int w)  //There are two parameters
{
    cout << "Area of rectangle is " << l*w;
}

void main()
{
    clrscr();
    area(5);
    area(4.7);
    area(4,9);
// Return types does not make difference only parameter type or number of parameter unique combinations do.
}


  1. Functions in C++ allows default arguments. Let's see another example to understand this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<conio.h>
#include<iostream.h>

void area(int l=5, int w=6) // Default values incase they are not provided, so it makes three combinatins
{
    cout << "Area of rectangle is " << l*w;
}
void main()
{
    clrscr();
// Three combinations by which we can call above methods
    area();
    area(9);
    area(4,9);
}


  1. In C++, structures and unions are true data types. No typedef required to remove structure keyword from declarations.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
struct book
{
    int bookno;
    char title[50];
    int price;
};
void main()
{
    book b;
    clrscr();
    cout << "Book No : ";
    cin >> b.bookno;
    cout << "Title : ";
    cin>>b.title; 
//gets(b.title);
    cout << "Price : ";
    cin >> b.price;
    cout << "Book No is :" << b.bookno << ", Title is " << b.title;
}


  1. Anywhere declaration of variables
  2. C++ allows to create functions inside the structures

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream.h>
#include<conio.h>
struct number
{
    int n;
    void square()
    {
        cout << "Square of " << n << " is " << n*n << endl;
    }
    void cube()
    {
        cout << "Square of " << n << " is " << n*n*n << endl;
    }

};
void main()
{
    clrscr();
    cout << "Enter two numbers : ";
    number x;
    number y;
    cin >> x.n >> y.n;
    x.square();
    x.cube();
    y.square();
    y.cube();
}

I guess now you have little more confidence on C++. C++ is superset of C. After reading this small crisp tutorial you will be able to answer common interview question "What is the difference between C and C++ ?".