Learn C++ Online

Class Objects as Function Arguments (Parameters)

Like any other data-type, an object may be used as a function argument. This can be done in two ways: A copy of the entire object is passed to the function Only the address of the object is transferred to the function The first method is called pass-by-value. Since a copy of the object is passed to the function, any changes made to the object inside the function do not affect he object used to call the function. The second method is called pass-by-reference. When an address of the object is passed, the called function works directly on the actual object used in the call. This means that nay changes made to the object inside the function will reflect in the actual object. The pass-by-reference method is more efficient since it requires to pass only the address of the object and not the entire object. Program given below illustrates the...

Static Member Functions of Class in C++

Like static member variable, we can also have static member functions in a class. A member function that is declared static has the following properties: A static member function can have access to only other static members (functions or variables) declared in the same class A static member function can be called using the class name (instead of its objects) as follows: class-name :: function-name; Program below illustrates the implementation of these characteristics: #include <iostream.h> class test { private: int code; static int count; //static member variable public: void setcode() { code = ++count; } void showcode() { cout << “Object number: ” << code << endl; } static void showcount() //static member function { cout << “Count: ” << count << endl; } }; int test :: count; void main() { test t1, t2; t1.setcode(); t2.setcode(); test :: showcount(); //accessing static function test t3; t3.setcode(); test :: showcount(); //accessing...

Static Data Members in C++

A data member of a class can be qualified as static (storage-class). The properties of a static member variable are similar to that of a C static variable. Read more about storage classes in C (Click here) In addition, a static member variable (of a class) has certain special characteristics which are detailed as follows: It is initialized to zero when the first object of its class is created. No other initialization is permitted Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created It is visible only within the class, but its lifetime is the entire program Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurrences of all the objects....

Data Types in C++

Data types supported by C++ can be broadly divided into six heads as stated follows: int data-type char data-type float data-type double data-type bool (boolean) data-type enum (enumeration) data-type 1) int data-type: int data-type represents the whole number (i.e., integer) quantities. Integers are required not to contain a decimal point or an exponent. int data-type can also be further sub-divided into two broad categories, viz, short int and long int signed int and unsigned int short int and long int The qualifier short, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly small integer values. On the other hand, the qualifier long, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly large integer values. signed int and unsigned int In case...

Comment Statements in C++

Unlike C, C++ supports two types of comments. C programmers are familiar with the /*..*/ style of commenting. Anything lying within the /*..*/ pair is ignored by the compiler. C plus plus (C++) additionally supports the // notation of commenting. Here, everything following // till the end of line is treated as comment. Usually /*..*/ style is used for commenting out of a block code, whereas, // is used for single-line comments. Example of single-line comment: // This is a single line comment Example of multiple-line comment: /*This is a multiple line comment*/