Skip to main content

Classes and Objects


INTRODUCTION

A class is a description of one or more similar objects. The class concept is similar to “type” in the conventional procedure-oriented languages. For example, if “integer” is a type (class), then “8” is an instance (objects) of type (class) “integer”. Similarly,”Person-1” and “Person-2” can be two instances (objects) of a class “Person”.

Multiple instances of a class can be created which are known as the objects of the class. Each object has its own local data and represents a different instance of its class. Since all the objects of a class share the same methods, the only different between two objects of the same class is the state of their local variables. Also note that there can be two categories of object variables-class variables and instance variables. Class variables are variables stored in the class whose values are shared by all instances (objects) of the class. Instance variables are variables for which local storage is available in the instances (objects).

Object-Oriented programming languages also support class hierarchies. Sub-classes of a given class are refinements of it, inheriting the functionality and local variables of the parent-class, or super-class. Sub-classes can add new local variables and methods and can modify or hide inherited methods.

You can make a new data type in C++ by declaring a class. A class is just a collection of variables—often of different types—combined with a set of related functions.

A class is a way to bind the data and its associated functions together. It allows the data and functions to be hidden, if necessary, from external use. When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type. Generally, a class specification has two parts:


The Class declaration describes the type and scope of its members.
The Class function definitions describe how the class functions are implemented.

The general form of a class declaration is:

class class_name
{
         private:                                                // visibility label /access modifier
                     variable declarations;             // data member
                     function declarations  ;           // function member
         public:                                                 // visibility label /access modifier
                     variable declarations;             // data member
                     function declarations  ;           // function member
};
                                     
The class declaration is similar to a struct declaration. The keyword class specifies, that what follows is an abstract data of type class name. The body of a class is enclosed within braces and terminated by a semicolon. The class body contains the declaration of variables and functions. These functions and variables are collectively called class members. They are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public. These keywords are followed by a colon.

The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside the class also. The data hiding (using private declaration) is the key feature of object-oriented programming. The use of the private keyword is optional. By default, the members of a class are private. If both labels are missing, then, by default, all the members are private. Such a class is completely hidden from the outside world and does not serve any purpose.

The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions have access to the private data members and private functions. However, the public members (both functions and data) can be accessed from outside the class. The binding of data and functions together into a single class-type variable is referred to as encapsulation.

Example:

class item
{
         int no;
         float cost;
public:
         void getdata( int a, float b);
         void putdata();
};
        


CREATING OBJECT

The declaration of an object is similar to that of a variable of any basic data type. The necessary memory space is allocated to an object at this stage. Once we had created the class we can declare any number of the objects as per need.
We can declare an object in two different ways:

Inside the main function

void main()
{
Item x;                                  // Item = class name, x = object name
………..
}

Outside of the class:

class item
{
…………..
}x;

This practice is seldom followed because we would declare the objects close to the place where they are used and not at the time of class definition.


ACCESSING CLASS MEMBERS

The private data of a class can be accessed only through the member functions of that class. The main() cannot contain statements that access number and cost directly.

Program 5:

#include<iostream.h>
#include<conio.h>

class item
{
         int number;
         float cost;
public:
         void getdata(int a, float b);
         void putdata()
         {
              cout << "Number = " << number;
              cout << "cost   = " << cost;
         }
}x;

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

void main()
{
         x.getdata(100,25.5);
         x.putdata();
         getch();
}


Made By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...