Home » Learn C++ » Classes in C++

Classes in C++

Classes:

            The most important feature of C++ programming language is that it supports Object Oriented Programming (OOP). In OOP, the computer program is divided into objects. OOP language is an easy and flexible approach for designing and organizing the program. The program is designed by using classes. In this tutorial the concepts of objects and classes have been discussed.

 

Class:

            A class is a collection of data and functions. The data items and functions are defined within the class. The functions are written to work upon the data items and each function has a unique relationship with the data items of the class.

            Classes are defined to create user-defined data types. These are similar to built-in data types available in all programming languages. Definition of a data type does not create any space in the computer memory. When a variable of that data type is declared, a memory space is reserved for that variable. Similarly, when a class is defined, it does not occupy any space in the computer memory. It only defines the data items and the member function that can be used to work upon its data items. Thus defining a class only specifies its data members and the relationship between the data items through its functions.

            Classes and Structures are similar. The syntax of a structure and a class is also similar. But generally, structures are exclusively used to hold data and classes are used to hold both data and functions.

 

Defining a Class:

            A class is defined in a similar way as a structure is defined. The keyword “class” is used to define the class. The general syntax to define a class is:

class class_name
{
body of the class

};
class It is the keyword that is used to define a class.

class name It represents the name of class. The objects are created by this name. The rules to give the name of a class and a variable are same. Any words that can be a variable name in a C++ program can be a class name.

body of class The body of a class consists of the data items and the functions. These are called members of the class. These are written between braces.

semicolon (;) The body of a class ends with semicolon.

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.