2.8 C++ IS A BLOCK-STRUCTURED LANGUAGE

A compound statement consists of any number of statements put inside curly brackets3. Anything put inside the braces is treated by C++ as a block. Block is little more than a compound statement. Concept of block has come from old language called ALGOL. C++ allows you to declare variables inside a block4. The scope of such variable is restricted to that block only. Let us study the concept with help of a simple program.

Problem: Write a program to demonstrate block structure.

Solution: Here we will define one variable inside the block and see whether it is available outside the block. We will test whether we can declare a variable at a place other than the beginning of the block. See Program 2.6.

Program ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.