A class in C++ is similar to structure in C. Using class or structure, a programmer can merge one or more dissimilar data types and a new custom data type can be created.
In C++, classes and structures contain member variables and member functions in their declarations with private and public access blocks that restrict the unauthorized use. The defined classes and structures further can be used as custom data type in the program to declare objects.
The private and public are new keywords in C++. The private keyword is used to protect specified data and functions from illegal use whereas the public keyword allows access permission.
The member function can be defined as private or public inside the class or outside the class.
To access private data members of a class, member functions are used.
The difference between member function and normal function is that the normal function can be invoked freely, whereas the member function can be invoked only using the object of the same class.
The static is a keyword used to preserve the value of a variable. When a variable is declared as static, it is initialized to zero. A static function or data element is only recognized inside the scope of the present compile.
When a function is defined as static, it can access only static member variables and functions of the same class. The static member functions are called using its class name without using its objects.
The member functions of a class can also be declared as constant using const keyword. The constant functions cannot modify any data in the class.
An object of a class can be passed to the function as arguments like variables of other data type. When an object is passed-by-value, then this method is called as pass-by-value, whereas when reference of an object is passed to the function then this method is called as pass-by-reference.
When classes are declared inside a function, then such classes are called as local classes. The local classes have access permission to global variables as well as static variables. The local classes should not have static data member and functions.
Test Your Skills
Test Your Skills Now!
This is only a practice test, it is designed to help you revise your concepts
The test contains questions, only 1 option is correct for each question
This is a timed test.
After you have finished the test, press on the 'Finish Test' button to know your score and get the correct answers