C++



C++ is a multi-paradigm programming language that supports object-oriented programming (OOP), created by Bjarne Stroustrup in 1983 at Bell Labs, C++ is an extension(superset) of C programming and the programs are written in C language can run in C++ compilers.

Uses of C++
C++ is used by programmers to create computer software. It is used to create general systems softwaredrivers for various computer devices, software for servers and software for specific applications and also widely used in the creation of video games.

C++ is used by many programmers of different types and coming from different fields. C++ is mostly used to write device driver programssystem software, and applications that depend on direct hardware manipulation under real-time constraints. It is also used to teach the basics of object-oriented features because it is simple and is also used in the fields of research. Also, many primary user interfaces and system files of Windows and Macintosh are written using C++. So, C++ is a popular, strong and frequently used programming language of this modern programming era.

Object Oriented Programming and C++
C++ supports object-oriented programming (OOP), with four significant principles of object-oriented development:
1. Abstractions.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.
Features of Object Oriented C++
  • The main focus remains on data rather than procedures.
  • Object-oriented programs are segmented into parts called objects.
  • Data structures are designed to categorize the objects.
  • Data member and functions are tied together as a data structure.
  • Data can be hidden and cannot be accessed by external functions using access specifier.
  • Objects can communicate among themselves using functions.
  • New data and functions can be easily added anywhere within a program whenever required.
  • Since this is an object-oriented programming language, it follows a bottom up approach, i.e. the execution of codes starts from the main which resides at the lower section and then based on the member function call the working is done from the classes.
The object-oriented approach is a recent concept among programming paradigms and has various fields of progress. Object-oriented programming is a technique that provides a way of modularizing programs by creating memory area as a partition for both data and functions that can further be used as a template to create copies of modules on demand.
Some Facts About C++
  • C++ development began in 1979, four years before its release, and it did not start with the name C++; Its first name was C with classes.
  • In the late part of 1983, C with Classes was first used for AT&T's internal programming needs.
  • Its name was changed to C++ later in the same year.
  • C++ was not released commercially until the late part of 1985.
C++ implements data abstraction using a concept called classes, along with other features to allow object-oriented programming and is considered a high-level language. Classes help programmers with the organization of their code. They can also be beneficial in assisting programmers to avoid mistakes.
The original C++ compiler, called Cfront, was written in the C++ programming language. C++ compilation is considered efficient and fast. Its speed can be attributed to its high-level features in conjunction with its low-level components. When compared to other computer programming languages, C++ can be viewed as quite short. This is because C++ leans towards the use of special characters instead of keywords.
A C++ program involves the following section:
  • Documentation
  • Preprocessor Statements
  • Global Declarations
  • The main() function
    • Local Declarations
    • Program Statements & Expressions
  • User Defined Functions
Let's begin with a simple C++ program code.

C++ Program which Outputs a Line of Text

#include <iostream> 

int main()
{
 std::cout<<"This is my first C++ Program.";
 std::cout<<std::endl<<"and its very easy";
}

FOR PRACTICE CODE:
1.Find the Day of the Week You Were Born!

#include <iostream>

using namespace std;

int main(void)
{
    int DD = 32, MM = 13, YYYY = -1, NYYYY, NMM, IDAY, day, flag = 0;
    
    string month[] = {"January","February","March","April","May","June","July",
                      "August","September","October","November","December"};

    cin >> DD;
    cin >> MM;
    cin >> YYYY;
    
    //Error Message: User has given no input
    if(DD == 32 && MM == 13 && YYYY == -1)
    {
        cout << " Please enter your birthday in this " << endl;
        cout << "     format(including spaces):      " << endl;
        cout << "            DD MM YYYY              " << endl;
        cout << "            22 01 1997              " << endl;
        return -1;
    }
    
    if(DD <= 0)
    {
        //Error Message: User has given invalid input for "Days" & "Months" fields
        if(MM <= 0)
        {             
            cout << " We don't have negative or null days and months. " << endl;
            cout << "                   Try again!                    " << endl;
            return -1;
        }
        
        //Error Message: User has given invalid input for "Days" field
        cout << " We don't have negative or null days. " << endl;
        cout << "              Try again!              " << endl;
        return -1;
    }
    
    if(MM <= 0)
    {
        //Error Message: User has given invalid input for "Months" field
        cout << " We don't have negative or null months. " << endl;
        cout << "               Try again!               " << endl;
        return -1;
    }
    
    if(DD > 31 || MM > 12 || YYYY <= 0)
    {
        if(DD > 31 && MM > 12)
        {
            //Error Message: User has given invalid input for "Days", "Months" & "Years" fields
            if(YYYY <= 0)
            {
                cout << " We have 12 months, the days of a month are up to 31 " << endl;
                cout << "       and a year should be a positive number.       " << endl;
                cout << "                     Try again!                      " << endl;
            }
            //Error Message: User has given invalid input for "Days" & "Months" fields
            else
            {
                cout << "       We have 12 months and       " << endl;
                cout << " the days of a month are up to 31. " << endl;
                cout << "            Try again!             " << endl;
            }
                
        }
        else if(DD > 31 && MM <= 12)
        {
            //Error Message: User has given invalid input for "Days" & "Years" fields
            if(YYYY <= 0)
            {
                cout << " The days of a month are up to 31 and " << endl;
                cout << " a year should be a positive number.  " << endl;
                cout << "             Try again!               " << endl;
            }
            //Error Message: User has given invalid input for "Days" field
            else
            {
                cout << " The days of a month are up to 31. " << endl;
                cout << "            Try again!             " << endl;
            }
                
        }
        else if(DD <= 31 && MM > 12)
        {
            //Error Message: User has given invalid input for "Months" & "Years" fields
            if(YYYY <= 0)
            {
                cout << "        We have 12 months and        " << endl;
                cout << " a year should be a positive number. " << endl;
                cout << "             Try again!              " << endl;
            }
            //Error Message: User has given invalid input for "Months" field
            else
            {
                cout << " We have 12 months. " << endl;
                cout << "     Try again!     " << endl;
            }
                
        }
        //Error Message: User has given invalid input for "Years" field
        else if(DD <= 31 && MM <= 12 && YYYY <= 0)
        {
            cout << " A year should be a positive number. " << endl;
            cout << "             Try again!              " << endl;
        }
        return -1;
    }
    
    switch(MM)
    {
        case 2:
            
            if((YYYY % 400) == 0 || ((YYYY % 4) == 0 && (YYYY % 100) != 0))
            {
                //Error Message: User has requested an invalid day for the month "February"
                //                and the requested "Year" is a leap year.
                if(DD > 29)
                {
                    cout << "  The year " << YYYY << " is a leap year. " << endl;
                    cout << " So, February has up to 29 days.          " << endl;
                    cout << "           Try again!                     " << endl;
                    return -1;
                }
            }
            else
            {
                //Error Message: User has requested an invalid day for the month "February"
                //              and the requested "Year" is not a leap year.
                if(DD > 28)
                {
                    cout << " The year " << YYYY << " isn't a leap year. " << endl;
                    cout << " So, February has up to 28 days.            " << endl;
                    cout << "           Try again!                       " << endl;
                    return -1;
                }
            }
            break;
            
        case 4:
            //Error Message: User has requested an invalid day for the month "April"
            if(DD > 30)
            {
                cout << " April has up to 30 days. " << endl;
                cout << "        Try again!        " << endl;
                return -1;
            }
            break;
              
        case 6:
            //Error Message: User has requested an invalid day for the month "June"
            if(DD > 30)
            {
                cout << " June has up to 30 days. " << endl;
                cout << "       Try again!        " << endl;
                return -1;
            }
            break;
        
        case 9:
            //Error Message: User has requested an invalid day for the month "September"
            if(DD > 30)
            {
                cout << " September has up to 30 days. " << endl;
                cout << "          Try again!          " << endl;
                return -1;
            }
            break;
            
        case 11:
            //Error Message: User has requested an invalid day for the month "November"
            if(DD > 30)
            {
                cout << " November has up to 30 days. " << endl;
                cout << "         Try again!          " << endl;
                return -1;
            }
            break;
    }
    
    if(MM <= 2)
    {
        NYYYY = YYYY - 1;
        NMM = 0;
    }
    else
    {
        NYYYY = YYYY;
        NMM = (4 * MM + 23) / 10;
    }
    
    //Calculating the day
    IDAY = 365 * YYYY + DD + 31 * (MM - 1) - NMM + (NYYYY / 4) - ((3 * ((NYYYY / 100) + 1) / 4));
    
    day = IDAY % 7;
    
    //This 'flag' is used for displaying the right ending after the numbers 
    if(DD != 11 && DD != 12 && DD != 13)
        flag = DD % 10;
    
    switch(day)
    {
        case 0:
            cout << " You were born on Saturday, ";
            break;
            
        case 1:
            cout << " You were born on Sunday, ";
            break;
            
        case 2:
            cout << " You were born on Monday, ";
            break;
            
        case 3:
            cout << " You were born on Tuesday, ";
            break;
            
        case 4:
            cout << " You were born on Wednesday, ";
            break;
            
        case 5:
            cout << " You were born on Thursday, ";
            break;
            
        case 6:
            cout << " You were born on Friday, ";
            break;
    }
    
    if(flag == 1){
        cout << DD << "st of ";
    }
    else if(flag == 2){
        cout << DD << "nd of ";
    }
    else if(flag == 3){
        cout << DD << "rd of ";
    }
    else{
        cout << DD << "th of ";
    }
    
    cout << month[MM-1] << " of " << YYYY << "!" << endl;
    
    cout << "\n       And if you liked it,        " << endl;
    cout << " don't forget to give a (+1) like!   " << endl;
    cout << "            Thank you!               " << endl;
    
    return 0;
}


No comments:

Post a Comment