Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Objects and Classes

 
Category: C++
Comments (7)

C++ Objects and Classes

An Overview about Objects and Classes

In object-oriented programming language C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language, this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects.


Features of Class:

Classes contain data known as members and member functions. As a unit, the collection of members and member functions is an object. Therefore, this unit of objects make up a class.


How to write a Class:

In Structure in C programming language, a structure is specified with a name. The C++ programming language extends this concept. A class is specified with a name after the keyword class.


The starting flower brace symbol, {is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol} and concluded with a colon;.



class exforsys
{
   data;
   member functions;
   ……………
};



There are different access specifiers for defining the data and functions present inside a class.


Access specifiers:

Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language:


  • private
  • public
  • protected
  • A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class.
    .
  • Public members are accessible from outside the class.
    .
  • A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.

When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it.



class exforsys
{
   private:
   int x,y;
   public:
   void sum()
   {
      ………
      ………
   }
};



In the code above, the member x and y are defined as private access specifiers. The member function sum is defined as a public access specifier.


General Template of a class:

General structure for defining a class is:



class classname
{
   acess specifier:
   data member;
   member functions;


   acess specifier:
   data member;
   member functions;
};



Generally, in class, all members (data) would be declared as private and the member functions would be declared as public. Private is the default access level for specifiers. If no access specifiers are identified for members of a class, the members are defaulted to private access.



class exforsys
{
   int x,y;
   public:
   void sum()
   {
      ………
      ………
   }
};



In this example, for members x and y of the class exforsys there are no access specifiers identified. exforsys would have the default access specifier as private.


Creation of Objects:

Once the class is created, one or more objects can be created from the class as objects are instance of the class.


Juts as we declare a variable of data type int as:


int x;


Objects are also declared as:


class name followed by object name;


exforsys e1;


This declares e1 to be an object of class exforsys.


For example a complete class and object declaration is given below:



class exforsys
{
   private:
   int x,y;
   public:
   void sum()
   {
      ………
      ………
   }
};


main()
{
   exforsys e1;
   ……………
   ……………
}



The object can also be declared immediately after the class definition. In other words the object name can also be placed immediately before the closing flower brace symbol } of the class declaration.


For example



class exforsys
{
   private:
   int x,y;
   public:
   void sum()
   {
      ………
      ………
   }
}e1 ;




The above code also declares an object e1 of class exforsys.


It is important to understand that in object-oriented programming language, when a class is created no memory is allocated. It is only when an object is created is memory then allocated.



Read Next: How to Access C++ Class Members



 

 

Comments


flora said:

  how can create a header file for a program to move a dot on the screen horizontally
November 16, 2006, 4:49 am

chinnari said:

  how much memory will allocate for an object, if a class contains an integer variable and a user defined function?
January 30, 2007, 6:52 am

chinnari said:

  where the memory is allocated for an object i.e, either in stack or heap memory?
January 30, 2007, 6:55 am

Rahul Sharma said:

  what is word of int.
October 22, 2008, 6:39 am

Yousuf said:

  Please write proper and complete procedure of using objects of classes for learners.

January 22, 2009, 10:52 am

Jogendra kumar said:

  if we define a member function outside the class & then make it a inline function by writing 'inline' keyword &this will be changed into a function as it is defined inside the class, so sometimes why we define it outside & then make inline.
January 30, 2009, 7:35 pm

aparna said:

  Why it is required to bundle variables and functions within class?
March 21, 2009, 2:03 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape