Navigation Menu

Approach to C++ Programming

What is Parser?

In computing technology, parser is a program, usually part of a compiler. Breaks data into smaller elements for easy translation into another language. Takes input in the form of a sequence of tokens or program instructions. Builds data structure in the form of a parse tree.

What is Compiler?

Compiler converts code written in programming language to machine language. Machine   understandable language is called Object code.


What is a programming language?

Programming language refers to high level languages called BASIC, C, C++, COBOL, PASCAL etc. Language provides the functionality of the application. Also called business logic of the application. Each language has own set of keywords to organize the logic.

C++ Programming language

C++ is an object-oriented programming language. Based on class and object concepts. Where C is a procedural-oriented, which is based on functions and structures.


C++ environment setup

  1. C++ application software (Code blocks, Turbo C++, etc.,). 
  2. C++ compiler (GNU GCC compiler).
  3. C++ debugger (GNU GDB).

Downloading and Installing C++ software, compiler and debugger

Step1: Go to http://www.codeblocks.org/downloads and click download the binary release.
   



Step2: Click on codeblocks-13.12mingw-setup.exe which downloads compiler and debugger.



Step3: Install the downloaded file.



Step4: Open the application downloaded.


Creating a file with simple program.

 Step1: Create a simple file with extension .cpp



 Step2: Write a basic to print Hello World.



 Step3: Compile ( Ctrl + Shift + F9 ) and Run the program ( Ctrl + F10 ).





Variable Declaration
  • Variable is a memory pointer declared to store some value.
  • It can be any name given but should not exceed 32 charecters.
  • Should start with character only.

   Syntax: Datatype Variable_Name;



Defining a Class and Object

  Class is a blue print and logical representation of object.
  Object is a physical representation of class. Takes all the members from class.

   Syntax to define class:   Class Class_Name;

   Syntax to define object: Class_Name object_name;

   Program to specify usage of Class and Object.   


Defining a Constructor and Destructor

Constructor gets called whenever an object is created and used to initialize the class members. Destructor is called after the program execution completed and will destroy all the current objects.

Sample program to demonstrate usage of constructor and destructor:


Looping Statements

Looping statements are used to iterate a block of statements until a specified condition becomes true.

Looping statements are : for(), while(), dowhile(), goto()

Iteration is an execution of block of statements for one time. When a sequence of instructions is executed in a repeated manner until condition becomes false, it is called a loop.

1.   for() statement:

 For statement is used whenever the number of iterations are fixed or known.

 Syntax: for(expression1;expression1;expression1)
             {
                       //block of statements
             }

 expression1: Initialization statement.

 expression2: Condition statement.

 expression3: Increment/ decrement statement.

 Example:

 




2.  While() statement:

  While statement is used whenever the number of iterations are not fixed.

  Syntax: While( condition )
              {
                   // block of statements
              }

 While statements executes the block of statements until the condition becomes false.

 Example:






3.  dowhile() statement:

  dowhile statement is used when condition needs to be checked after iteration.

  Syntax: do( condition )
              {
                   // block of statements
              }while ( condition );

 Example:






4. goto() statement:

Goto statement is used to execute set of statements repeatedly or to skip the flow of execution.

Syntaxgoto label_name;
             -------
             -------
             -------
             label_name:

Label name can be after goto statement or before based on requirement.

Example:







Reading a file

We can read a text document from c++ parser.
Need to include a header file called <iostream> which gives classes to read & write files. The classes are istream to read from file and ostream to write to file.
  
Example1: Basic example to read from a file.



  Text document added as a resource file to read.





 Compile and Execute the file.




Example2: Reading and displaying the text in order.




Text document added as a resource file to read and Output.




String Manipulation Functions

  1.    Including HeaderFile

    


  2. Declaring String variable

  


   3. Reading and displaying a string

    




4. String Concatenation
  


   

5. Searching a String ( find() function):

  Find function returns the position of a starting character of string in line.

                            Position  = str.find("string / character");






6. Substrings ( substr() ):

    Substring is a string function used to search and return a string.

   Syntax: substr( Starting_position, No_of_charecters )

   

   
  

7. Erasing and replacing string:

   erase() method erases the text based on starting index and no_of characters.

  replace() method replaces the text at specified position.
  

   


8. Comparing string ( strcmp() ): 

   strcmp() function compares two string and returns zero if equal.

  Syntax: strcmp(“string1”,”string2”).

  Compares string2 with string1.



    




















































2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Programming is unfortunately too difficult for me, but I have no problems using various types of native applications. The more that if I go to https://grapeup.com/cloudboostr/ I know that I can find there very comprehensive IT solutions for business. As a company owner, of course, I like to use such solutions.

    ReplyDelete