Classes – Object Oriented Programming

Object Oriented Programming

If we say that a particular programming language is Object Oriented then it means, the programming language must have the facility to define classes in it. A class is an expanded concept of a data structure i.e instead of containing data only, it can contain both data and functions (operations on data).

What is a Class?

A class is an organization of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object.

In non-technical language, we can say that a class is a collection of similar object containing a set of shared characteristics. For example , mango, apple and orange are members of the class fruit. In a way, a class and its objects have the relationship of a data type and variables. A class is simply a template for holding objects. A class is abstract but objects are real. Simply by defining a class we don’t create an object just like the mere declaration of a data ”
does not create variables. One or more classes grouped together constitute a program. Once a class has been defined, we can create any number of objects belonging to that class. For example , the syntax used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit. A class is declared in the beginning of a program. A class can contain any number of data members and any number of member functions. A class can also have only data members.

How to declare a class in C++ Language

Here is an example of how to declare a class in C++ and create the objects of that class.

class Line {
public:
    Draw();
private:
    int x1;
    int x2;
    int y1;
    int y2;
};
Line line1,line2;

Both line1 and line2 are the objects of Line class.

Classes

A class is an organization of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object. In non-technical language, we can say that a class is a collection of similar object containing a set of shared characteristics. For example , mango, apple and orange are members of the class fruit In a way, a class and its objects have the relationship of a data type and variables. A class is simply a template for holding objects. A class is abstract but objects are real. Simply by defining a class we don?t create an object just like the mere declaration of a data ” does not create variables. One or more classes grouped together constitute a program. Once a class has been defined, we can create any number of objects belonging to that class. For example , the syntax used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit. A class is declared in the beginning of a program. A class can contain any number of data members and any number of member functions. A class can also have only data members.

The first thing to do in object oriented programming is decide on a class hierarchy – that is, a hierarchy of kinds of objects, from very general ones to very specific kinds. In this simple example we can have a general class of ‘shapes’ and more specific classes for ‘circles’ and ‘rectangles’ (a realistic program would of course have many more such classes). A particular circle with specific size, colour etc will be an instance of the circle class. We can now decide on which operations and data fields can be defined for the most general class (shape). The following gives the type definition, and an example method (assuming that the type “ColourType” is defined somewhere).

class Shape {
public:
   Move(int x, int y);
   SetColour(ColourType c);
private:
   int xpos,ypos;
   ColourType colour;
};

Shape::SetColour(ColourType c)
{
  colou
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post