C++: Classes & Objects- 1

Structures in C++:

it provides a facility to hide the data which is one of the main percepts of OOP.

It also supports Inheritance, a mechanism  by which one type can inherit characteristics from other types.

It can have both variables and functions as members.
__________________________________________
Specifying a Class:

A class is a way to bind the data and its associated functions together.

When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type.

The general form of class declaration is:

    class class_name{

            private:
                    variable declaration;
                    function declaration;
            public:
                    variable declaration;
                    function declaration;
     };

The data hiding(using private declaration) is the key feature of Object Oriented Programming.

By default, the members of a class are private.

The variables declared inside the class are known as data members and the functions are known as member functions.

Only the member functions can have access to the private data members and private functions.

The binding of data and functions together into a single class type variable is known as encapsulation.
                        
Data cannot be accessed by any function that is not a member of the class.

Creating Objects:

class_name object_name; //memory for object is created

Accessing Class Members:

object_name.function_name(actual arguments);
__________________________________________

Defining Member Functions:

Outside the class definition

    return-type class-name :: function-name(argument declaration){
                  Function body
      }

A member function can call another member function directly, without using dot operator.

Inside the class definition

When a function is defined inside a class, it is treated as an inline function.
__________________________________________

Making an outside function inline:

eg
     class item {
          ............
          ............
       public:
               void getdata(int a, float b); //declaration
      };

    inline void item:: getdata(int a, float b)//definition
      {
          number=a;
          cost=b;
       }

                 < Happy Coding >

Reference:
OOP by BALAGURUSWAMY

In Association with Amazon.in

Comments

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

What is "namespace" and why do we use it?