Broad Network


Class Scope in C++

Scopes in C++ - Part 3

Forward: In this part of the series, I explain class scope in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 3 of my series, Scopes in C++. In this part of the series, I explain class scope in C++. As I said in the previous part of the series, scoping in C++ is focused on the C++ block, which consists of statements within curly brackets.

A Class
A class is a group of identifier objects and functions that work together as a unit. A typical class description begins with the reserved word, class, followed by the class name of your choice. Then you have a pair of curly brackets. Inside this block you have public, protected and private members. A member is either an object declaration (object identifier) or a function. These identifiers and functions work together as a unit. In a class description, the constructor and the methods (member function) are nested blocks. The object identifiers are called properties or member objects. The functions are called, methods or member functions.

Example
The following program has a class called, calculator:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {
                num1 = ident1;
                num2 = ident2;
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };


int main()
    {
        Calculator myObject(2,3);
        int result = myObject.add();
        cout << result;

        return 0;
    }

The class has two properties, one constructor function and one method. The constructor function and the add() method each comes as a nested block. I will use the properties and methods of this class to explain class scope in C++. When you come across a different class, you will make analogy with what you get here, to appreciate (understand) the scope features of the class (you come across).

The properties, num1 and num2 can be seen everywhere in the class, including the nested blocks. The constructor and add methods are functions and have function scope. You should read the previous part of the series to understand function scope. As I said, scoping in C++ focuses on the block.

As you can see there is a lot of similarity between the class scope, the function scope and the local scope. Local scope concerns, the if, the for, the while and the switch constructs. Function scope concerns ordinary functions and member functions.

Classes are very important in programming. It is said that, if you do not understand OOP and its classes, some employers will not employ you.

You still have to study the namespace scope. When we get there, you will see that the different scopes in C++ are similar, with focus on the block.

Well, let us stop here and continue in the next part of the series.

Chrys
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message