HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

CLASSES AND OBJECTS

CLASSES AND OBJECTS



This topic gives a brief introduction about the most important features of C++ - Classes and Objects, and proceeds to discuss them in detail. The real strength of C++ lies in the fact that it supports object-orientation. The fundamental idea behind object-orientation is to combine both data members(variables) and the functions that operate on the data into a single unit called an object.

In real-life, every object having an identity (name of the object), certain attribute a behavior, and a state. For example, a book has a name (identity). It has certain attributes like color, size, content, and no, of pages. It has certain functions that can be performed on it, line reading and writing the book. An object also has a state that is governed by its attributes. For example, on changing the contents of a book, the state of a book changes. The data and functions are encapsulated into a single entity called a book. This storage of data and functions together in one entity is called data encapsulation.

An object’s function provides a way to access and manipulate data in the class safely. Class data or functions are kept hidden and are safe from accidental alteration or use. This kind of protection of data from external alteration, except by the use of public functions, is called data hiding.

Data encapsulation and data hiding are the key terms used for describing an object-oriented language.

In C++, the functions of an object are called member functions. They are called methods in other object-oriented language Java.

CLASS:
The user-defined data type, class is a new data type that is created to solve a particular kind of problem. Once a class is created, anyone can use it without knowing the specifics of how it works or even how a class is built.

The class key word is used to declare a class. The braces are used to indicate the start and end of a class body. Member variables and member functions are declared inside the class body. A semicolon is used to end of the declaration.

Syn:
class <class_name>
{
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
};
void main()
{
<class_name> <object_name>;
; . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
}

Access Specifiers:
An access specifier is used to determine whether any other class or function can access the member variables and functions of a particular class. C++ supports three access specifiers.

• public
• private
• protected

Public Access Specifier:

The public access specifier allows a class to expose its member variables and member functions to other functions and objects.

// Ex-15: PGM ILLUSTRATES THE ACCESS SPECIFIER public

#include<iostream.h>
#include<conio.h>
class rainbow
{
public:
char *color;
void display()
{
gotoxy(27,10);
cout<<"What is Rainbow?: ";
cin>>color;
clrscr();
}

void displayout()
{
clrscr();
gotoxy(32,12);
cout<<color;
}
};

void main()
{
rainbow obj;
clrscr();
obj.display();
obj.displayout();
getch();
}

Note: The . operator is used to access member data and functions.

In the above example, the variable color can be accessed in any function. The function display(), would be accessible from anywhere in the program by any other function.

Private Access Specifier:
The private access specifier allows a class to hide its member functions from other class objects and functions.

// Ex-16: PGM ILLUSTRATES THE PRIVATE ACCESS SPECIFIER

#include<conio.h>
class rainbow
{
private:
char color[10];
public:
void accept()
{
cout<<"which one is a first color of the Rainbow?: ";
cin>>color;
}
void show()
{
cout<<"The first color of the Rainbow : "<<color<<endl;
}
};

void main()
{
rainbow vibgyor;
clrscr();
vibgyor.accept();
vibgyor.show();
/*cin>>vibgyor.color; (This stmt. gives an error, because private members
cannot be accessed outside the class definition.)*/
getch();
}

Note: In the above example, the functions accept() and show(), can be called from the object vibgyor, defined in main() function. Whereas the variable, color, cannot be accessed through the object vibgyor, since it is a private member variable.

The default access specifier for a class member is private, even though it has not been specified explicitly.

Ex:
class Rainbow
{
char color[10];
public:
. . . . . . .
. . . . . . .
};