Skip to main content

C++ Basic Syntax

In a C++ program, objects communicate by invoking each other's methods, forming a cohesive system. Let's delve into the meanings of classes, objects, methods, and instance variables.

Object:

  • Objects have states and behaviours. For example, a dog can have states like colour, name, and breed, and behaviours like wagging, barking, and eating. Essentially, an object is an instance of a class.

Class:

  • A class serves as a blueprint or template that defines the states and behaviours that its objects will have.

Methods:

  • Methods define behaviours in a class. A class can contain multiple methods, where the logic is implemented, data is manipulated, and actions are carried out.

Instance Variables:

  • Each object has a unique set of instance variables. The state of an object is determined by the values assigned to these variables.

 

Structure of a C++ Program

Let's look at a simple example that prints "Hello World":

#include <iostream>

using namespace std;

 

// Program execution begins here.

int main()

{

   cout << "Hello World"; // Prints Hello World

   return 0;  

}

 

Components of the Program

  • Header Files: C++ defines several headers that provide essential functionalities. For this program, we include the <iostream> header.
  • Namespace: using namespace std; tells the compiler to use the standard namespace, which helps avoid naming conflicts.
  • Comments: The line // main() is where program execution begins. is a single-line comment. Single-line comments in C++ start with //.
  • Main Function: int main() defines the main function where execution starts.
  • Output Statement: cout << "Hello World"; prints "Hello World" to the screen.
  • Return Statement: return 0; ends the main function and returns 0, signaling successful execution.

 

Compiling and Running a C++ Program in Dev-C++

Here’s how to save, compile, and run the program using Dev-C++:

  1. Open Dev-C++ and create a new source file.
  2. Write the code shown above.
  3. Save the file as hello.cpp.
  4. Click on the "Execute" menu and select "Compile & Run" (or press F11).

You should see Hello World printed in the output window.

   


Semicolons and Blocks in C++

In C++, statements end with a semicolon (;), marking the conclusion of one logical statement. For instance:

a = b;

b = b + 1;

sum(a, b);

 

A block is a set of statements enclosed in braces ({}). For example:

{

   cout << "Hello World"; // Prints Hello World

   return 0;

}

 

C++ does not consider the end of a line as a statement terminator, so statements can be written on the same line:

a = b; b = b + 1; sum(a, b);

 

Identifiers in C++

Identifiers are names for variables, functions, classes, modules, and other user-defined items. They must begin with a letter (A-Z or a-z) or an underscore (_) and can be followed by letters, digits (0-9), or underscores. Identifiers are case-sensitive.

Examples of valid identifiers:

ram

mohan

john

variable_name

x_123

Example15

_temp

y

z9value

returnValue

 

Keywords in C++

The following are reserved words in C++, meaning they cannot be used as identifiers:

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

class

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

 

        

Whitespace in C++

Whitespace includes blanks, tabs, newline characters, and comments, and it separates parts of a statement to help the compiler distinguish between elements like int and value:

int value;

In the following example, whitespace is not necessary but can improve readability:

total = apples + oranges;   // Calculate the total fruit

This deeper understanding of C++ fundamentals will aid in writing, compiling, and executing C++ programs effectively.

 

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed).

How to Read Analog Input & Use PWM pin as Analog Output

  Analog Input: An analog signal can take on any number of values. To measure the value of analog signals, Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. There is an inbuilt function to read Analog value; analogRead(pin_number). This function converts the value of the voltage on the analog input pin and returns a digital value ranges from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). This function has only one parameter, which is the pin number.     Analog Output: The Arduino does not have any built-in digital-to-analog converter (DAC), but it can do pulse-width modulation (PWM); a digital signal to achieve some of the functions of an analog output. The function analogWrite(pin, value) is used to output a PWM signal. In the function ‘pin’ is the pin number used for the PWM output. ‘value’ is a number proportiona