CLASSES AND OBJECTS Classes and Objects with syntax

  • Slides: 7
Download presentation
CLASSES AND OBJECTS Classes and Objects with syntax, explanation and examples. Submitted by :

CLASSES AND OBJECTS Classes and Objects with syntax, explanation and examples. Submitted by : MOHAMMED SAFAR NAFEEZ (BCA –A)

WHAT IS CLASS ? A class in C++ is a user-defined type or data

WHAT IS CLASS ? A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer. In C++, a class defined with the class keyword has private members and base classes by default. A structure is a class defined with the struct keyword. Its members and base classes are public by default. In practice, structs are typically reserved for data without functions.

MEMBER FUNCTION Member functions are operators and functions that are declared as members of

MEMBER FUNCTION Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static this is called a static member function. A member function that is not declared as static is called a nonstatic member function. The definition of a member function is within the scope of its enclosing class. The body of a member function is analyzed after the class declaration so that members of that class can be used in the member function body, even if the member function definition appears before the declaration of that member in the class member list. When the function add() is called in the following example, the data variables a, b and c can be used in the body of add()

NESTED MEMBER FUNCTION A nested class is a class which is declared in another

NESTED MEMBER FUNCTION A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. For example, program 1 compiles without any error and program 2 fails in compilation.

ARRAY OF OBJECTS The array of type class contains the objects of the class

ARRAY OF OBJECTS The array of type class contains the objects of the class as its individual elements. Thus, an array of a class type is also known as an array of objects. An array of objects is declared in the same way as an array of any built-in data type. The syntax for declaring an array of objects is class_name array_name [size] ;

STATIC OBJECT When we declare a member of a class as static it means

STATIC OBJECT When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator : : to identify which class it belongs to.