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
Post a Comment