Broad Network


Some Features of C++ Entities

Some Features of C++ Entities – Part 1

Forward: In this series, I explain some of the important features not explained so far, in my volume, C++ Tutorials.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 1 of my series, Some Features of C++ Entities. In this series, I explain some of the important features not explained so far, in my volume, C++ Tutorials. After this series, you have three more series: one on what is called the preprocessor directives and the next on template container coding. The third will be on C++ namespace. After that I will explain the important features in the C++ standard library. And after that, you should have learned all the important aspects of C++. Then you will be on your own. You should be an expert at that level. At that level, so far as C++ is concerned, the world will be in front of you.

Back to the course:

C++ Entities
In C++, an entity is a value, object, array element, variable (identifier), function, enumerator, type, class member, template, or namespace. An entity occupies an area in memory.

The sizeof Operator
The sizeof operator behaves like a function. It returns an int. It determines the size of a fundamental object type or the size of an instantiated object.

The size of a fundamental object type depends on the computer, operating system and the compiler. So, from time to time you may have to use the sizeof operator to know the size of a fundamental object type, for your system. The following program displays the size of an, unsigned long int, type:

#include <iostream>
using namespace std;

int main()
    {

        int size = sizeof (unsigned long int);

        cout << size;

        return 0;
    }

The following program displays the size of an instantiated object:

#include <iostream>
using namespace std;

class TheClass
    {
        public:
        int int1;
        float float1;
    };

int main()
    {
        TheClass myObj;

        int size = sizeof (myObj);

        cout << size;

        return 0;
    }

The instantiated object is used as the operand (argument) of the sizeof operator.

The Class and the Struct
The class and the struct are similar. They are created in the same way. In their creations, if instead of using the reserved word, class, you use, struct, you would have a struct instead of a class. A struct is actually a kind of class where the data members (properties) are public, by default. The main difference between a struct and a class is that, a class can have member functions (methods) but a struct cannot, conventionally. Another difference between a struct and a class is that, a struct does not allow a user-defined constructor, conventionally.

The following program creates and uses a struct called, calculator:

#include <iostream>
using namespace std;

struct Calculator
    {
        int num1;
        int num2;
    };


int main()
    {
        Calculator myObject;
        myObject.num1 = 2;
        myObject.num2 = 3;

        cout << myObject.num1 << "\n";
        cout << myObject.num2 << "\n";

        return 0;
    }

The following program shows the above struct, turned into a class, with a member function (method) included:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

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


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

        return 0;
    }

That is it for this part of the series. As indicated above, in this series, I am giving you complementary topics that you will not normally find in C++ tutorials. We stop here and continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message