.

Classes And Objects in C++

className

The building blocks of c++ turn to object oriented programming is called a className. It is user defined data type that holds the address of the another variable and its member and member function. A classNameis a blueprint of an object.

Example

Car is an classNamewhich defines the number of properties.

Object

The object is the instance of the className. When the classNameis defined there is no memory allocation but allocation is initiated. A classNameis defined within the keyword classNamefollowed by a classNamename. the body of the classNamewill be defined within the curly brackets.

Syntax – Declaration

classname objectname;

Object and classNamein detail

classNameAccess Modifier

The data member and the member functions of all the classNamecan be accessed using the dot operator with the object. The public data member is accessed in the same way as the private data members are not allowed to access the object directly. Accessing the data member depends on the control of data member.

There are three different types of access modifier. They are

The Public Members

The Private Members

The Protected Members

The Public Members

All the members that are declared under public will be accessible to everyone. The data members and the member functions may be declared public will be accessed by the other classes also. They can be used anywhere in the program.

Example

The Private Members

The classNamemembers declared as private can be accessed by the respective function which is used inside the className. This member function cannot be accessed by any other function in the same program or for some other programs. This function cannot be accessed by other function outside the className.

Example

The Protected Members

This member is similar to private access specifier, and the difference is that the classNamemember which is declared as protected can be accessed only within the member function.

C++ program to demonstrate

Constructors and Destructors

Constructors

A Constructor is a member function present in the classNamethat initializes object of a className. It is automatically called when the object is created. It is special member function of the className.

A constructor has same name as that of constructor name. Constructor doesn’t have return type. It is called automatically when the object is created. If the constructor is not specified then c++ generates the default constructor.

There are three different types of constructors. They are

Default constructor

Copy Constructor

Parameter constructor

Default Constructor

The default constructor is the constructor which does not have any arguments. It is defined with no parameters.

Example

Parameterized Constructors

The constructor with arguments are called parameterized constructor. When the constructor is created, arguments help initializes the object to add the parameter to other functions.

Example

Copy Constructor

A copy constructor is s member function that initializes an object using another object of the same className. When an object of the classNameis returned by the value. When an object of the classNameis passed within the value as an argument. When the compiler compiles and generates the temporary object, copy constructor is called.

Example

Destructor

A destructor is a special member function of a classNamewhich is executed when an object of the classNamego out of scope. A destructor will have the same name as that of constructor. The destructor is represented as (~) tilde symbol and neither return value nor parameter can take it.

Syntax

Friend Function

A friend function of the classNameis defined outside the classNamebut it has rights to access all the private and protected className. Sometimes it allows the particular classNameto access the private member of the className.

Example

Inline Function

C++ is an inline function which is the powerful concept which is used in className. The compiler places a code of function at each point during compilation called compile time. Any change in the inline function requires the function to be recompiled by the computer to replace the codes once again will continue with the functionality.

A function definition in a classNameis defined as an inline function without using inline specifier.

Example

This Pointer

This pointer has a parameter to all member functions. Every object access the address to other pointer called as this pointer. This pointer may be used to refer the invoking objects. Friend function doesn’t have this pointer because friends are not members of a className.

Example

Pointer to C++ Classes

The pointer is a variable that holds the address of another variable. A classNamein c++ is a object oriented programming concept that enables programmer to create the user defined complex data types and the functions that can be operated on the data. A pointer in c++ uses the pointer to structure the access members of a pointer to a className.

classes and objects img1

Figure : Pointer to c++ classes

Example

Static Member of a className

A static member function will access static data member and on the other side the static member function may access from outside the className. Static member functions have scope of the classNameand do not have access to this pointer of the className. A static member is shared by the object of the className. The static member is initialized to 0 when the first object is created.

Example

Static Function Members

A static function member is a special member function that is used to access only the static data members where other normal data member cannot accessed through the static member function. The static member function may be defined using classNamename and function name.

syntax

class_name:: function_name(perameter);

.