Sie sind auf Seite 1von 18

DEFINITION AND DECLARATION OF A CLASS

A class in C++ combines related data and functions together. It makes a data type
which is used for creating objects of this type.
Classes represent real world entities that have both data type properties
(characteristics) and associated operations (behavior).

The syntax of a class definition is shown below :
Class name_of _class
{
private : variable declaration; // data member
Function declaration; // Member Function (Method) protected: Variable
declaration;
Function declaration;
public : variable declaration;
Function declaration;
};


Here, the keyword class specifies that we are using a new data type and is
followed by the class name.
The body of the class has two keywords namely :
(i) private (ii) public
In C++, the keywords private and public are called access specifiers.
The data hiding concept in C++ is achieved by using the keyword private.
Private data and functions can only be accessed from within the class
itself. Public data and functions are accessible outside the class also.
Data hiding not mean the security technique used for protecting computer
databases. The security measure is used to protect unauthorized users
from performing any operation
(read/write or modify) on the data.
The data declared under Private section are hidden and safe from
accidental manipulation. Though the user can use the private data but not
by accident.
The functions that operate on the data are generally public so that they
can be accessed from outside the class but this is not a rule that we must
follow.



MEMBER FUNCTION DEFINITION


The class specification can be done in two part :
(i) Class definition. It describes both data members and member
functions.
(ii) Class method definitions. It describes how certain class member
functions are coded.


In C++, the member functions can be coded in two ways :
(a) Inside class definition
(b) Outside class definition using scope resolution operator (::)

Inside Class Definition
When a member function is defined inside a class, we do not
require to place a membership label along with the function
name. We use only small functions inside the class definition
and such functions are known as inline functions.

In case of inline function the compiler inserts the code of the
body of the function at the place where it is invoked (called)
and in doing so the program execution is faster but memory
penalty is there.



Outside Class Definition Using Scope Resolution Operator (::)


In this case the functions full name (qualified_name) is written as shown:
Name_of_the_class :: function_name
The syntax for a member function definition outside the class definition is
:
return_type name_of_the_class::function_name (argument list)
{
body of function
}
Here the operator::known as scope resolution operator helps in defining
the member function outside the class.

DECLARATION OF OBJECTS AS
INSTANCES OF A CLASS
The objects of a class are declared after the class definition. One must remember
that a class definition does not define any objects of its type, but it defines the
properties of a class. For utilizing the defined class, we need variables of the class
type. For example,
Largest ob1,ob2; //object declaration
will create two objects ob1 and ob2 of largest class type. As mentioned earlier, in
C++ the variables of a class are known as objects.
These are declared like a simple variable i.e., like fundamental data types.

ACCESSING MEMBERS FROM
OBJECT(S)
After defining a class and creating a class variable i.e., object we can
access the data members and member functions of the class.
Because the data members and member functions are parts of the class,
we must access these using the variables we created.
For functions are parts of the class, we must access these using the
variable we created.
Class student
{
private:
char reg_no[10];
char name[30];
int age;
char address[25];

public :
void init_data()
{
- - - - - //body of function
- - - - -
}
void display_data()
}
};
student ob; //class variable (object) created
- - - - -
- - - - -
Ob.init_data(); //Access the member function
ob.display_data(); //Access the member function
- - - - -
- - - - -

Here, the data members can be accessed in the member functions as
these have private scope, and the member functions can be accessed
outside the class i.e., before or after the main() function.

STATIC CLASS MEMBERS
Data members and member functions of a class in C++, may be qualified
as static. We can have static data members and static member function in
a class
Static Data Member: It is generally used to store value common to
the whole class. The static data member differs from an ordinary data
member in the following
ways :
(i) Only a single copy of the static data member is used by all the
objects.
(ii) It can be used within the class but its lifetime is the whole program.
For making a data member static, we require :
(a) Declare it within the class.
(b) Define it outside the class.

For example
Class student
{
Static int count; //declaration within class
-----------------
-----------------
-----------------
};
The static data member is defined outside the class as :

int student :: count; //definition outside class

The definition outside the class is a must.

We can also initialize the static data member at the time of its definition as:

int student :: count = 0;

If we define three objects as : sudent obj1, obj2, obj3;

#include <iostream> using namespace std;
class Box
{
public: static int objectCount; // Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h; // Increase every time object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
private: double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl; return 0;
}
When the above code is compiled and executed, it produces following result:
Constructor called. Constructor called.
Total objects: 2


Static Function Members

By declaring a function member as static, you make it independent of any
particular object of the class.
A static member function can be called even if no objects of the class exist and
the static functions are accessed using only the class name and the scope
resolution operator ::.
A static member function can only access static data member, other static member
functions and any other functions from outside the class.

Class student
{
Static int count;
-----------------
public :
-----------------
-----------------
static void showcount (void) //static member function
{
Cout<<count=<<count<<\n;

}
};
int student ::count=0;
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount; // Constructor
definition Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h; // Increase every time object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box }; // Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{ // Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}

Das könnte Ihnen auch gefallen