.

Basic Syntax in C++

C++ is a general-purpose, case-sensitive and an object-oriented programming language.

C++ is an extension of C programming language. C++ includes C language and it has additional features for working with objects, classes, events and other object oriented concepts.

Most of the application program that comes in the category of:

  • Word Processing
  • Spreadsheet Program

Object

An object is a specific instance of a className; it contains real values instead of variables.

An object is an instance of a className. Objects have states and behaviours.

Example: A cat has states - colour, name, breed as well as behaviours – wagging the tail, shouting.

className

A classNameis a template definition of the method s and variable s in a particular kind of object.

The structure of a classNameand its subclasses is called the classNamehierarchy.

C++ Program Structure

C++ has defined program structure. The following example illustrates the structure

Example

The above program is explained as

#include is a header file that work with input and output objects such as cout.

Header files add functionality to C++ programs.

using namespace std means use names for objects and variables from the standard library.

An empty line in the C++ program ignores white space.

int main()is called as function. Any code inside its curly brackets{} will be executed.

cout is an object used to output/print text

Every C++ statement ends with a semicolon

return 0 ends the main function

Compile & Execute C++ Program

Compilation is done in two ways either with the menu or with shortcut

By Menu

Click on compile menu then click compile - compilation

Click the run menu then click run - run

By shortcut

Press ctrl +f9 automatically compile and run the C++ program.

Example

using namespace std;

cout is an object used to output/print text

The output is

Hello!

Welcome

Now press escape to return to turbo C++

Semicolons & Blocks in C++

C++ semicolon

The semicolon is a statement terminator in C++ to help the parser figure out where the statement ends since, by default, statements in C++ can have any number of lines.

In both C and C++, the semicolon is the same as the period in English. The semicolon is punctuation. There are general cases.

In classic for loops there are three statement and the semicolon and the end of the last statement is not needed

Example

    cout  “Welcome to C++”; 
    

Blocks (compound statements)

A block of statements, also called a compound statement, is a group of statements that is treated by the compiler as if it were a single statement.

Blocks begin with a symbol, end with a symbol, and the statements to be executed are placed in between. Blocks can be used any place where a single statement is allowed. No semicolon is needed at the end of a block.

C++ Keywords

Keyword is a predefined or reserved word in C++ library with a fixed meaning and used to perform an internal operation.

C++ Language supports more than 64 keywords. Every Keyword exists in lower case latter like auto, break, case, const, continue, int etc.

Use keyword as identifier in your C++ programs, its reserved words in C++ library and used to perform an internal operation.

Avoid using names from the C++ library

Asm

Else

new

this

Auto

Enum

operator

throw

Bool

Explicit

private

true

break

Export

protected

try

Case

Extern

public

typedef

Catch

False

register

typeid

Char

Float

reinterpret_cast

typename

className

For

return

union

const

Friend

short

unsigned

const_cast

Goto

signed

using

continue

If

sizeof

virtual

default

Inline

static

void

delete

Int

static_cast

volatile

Do

Long

struct

wchar_t

double

Mutable

switch

while

dynamic_cast

namespace

template

C++ Keywords

.